loading bitmaps into memory D2D

Started by
2 comments, last by kkrz 12 years, 2 months ago
this question pertains to loading bitmaps into memory with Direct2D. basically i am planning to have a huge bitmap be the terrain for my 2d world. it's going to be too massive to load the entire thing into memory at once.

i know that you can load a large bitmap and then use source rectangles to only render a certain section of it, but i believe that still requires loading the entire bitmap at once.

i'm currently using this technique to load a bitmap from a file into a ID2D1Bitmap*:
http://msdn.microsof...v=vs.85%29.aspx

Is it possible to only load a small part of the bitmap into memory at once? How would I go about doing this?
Advertisement
Is it really needed to have the texture in one large bitmap?

Why not chunk it into that kind of parts that you can handle well (from a performance view)?

When drawing/using the bitmap you simply use the chunks.


I don't know a way to do this with WIC, maybe doing a special copy or so (telling WIC about the big image, which isn't loaded at that time, then copying a part of it where WIC has to decode/open it anyway)... but I don't think that you can get WIC to a point where it doesn't load the whole image into memory, especially think about how e.g. jpeg works... or are we still talking about raw bitmaps?

If you are only using raw bitmaps there is an option, you can bypass the file loading of WIC and manually load you WIC Image (later used for D2D, where you may make a copy to a d2d hardware render target for better performance, since everything with WIC and D2D uses d2d software). So manually loading the WIC image can be done with the stream com interface (at least), you simply have to load the bitmap by yourself. That shouldn't be that problem. You read the file by yourself (look up bitmap format), then you get the pixels (only that part that you want, this is your primary requirement) into your WIC image (maybe you will lock the image instead of using the stream, which may be better) and finally use it with d2d. When using the stream you have to manually adding the correct bitmap header (this may also be easily possible, but some additional work). With lock or so it should be straightforward I think.

Sorry if my thoughts are a little bit confusing, just some ideas that I got when writing the text.

Tell us what you have chosen and we may be able to help you with the implementation.

Vertex
It doesn't have to be one large bitmap, I'm still trying to figure out the best way to accomplish this.

The Direct2D documentation says:
Video cards typically have a minimum memory allocation size. If an allocation is requested that is smaller than this, a resource of this minimum size is allocated and the surplus memory is wasted and unavailable for other things. ... We recommend that you try to keep most bitmaps to at least 64 KB and limit the number of bitmaps that are smaller than 4 KB.

I understand this to mean that if the image is over 64 KB memory will not be wasted. What is the advantage to breaking down the images into parts? For example, let's say I have 200 PNG files representing the terrrain and load them separately as so:


ID2D1Bitmap* m_terrainBitmap[200];

HRESULT hr = LoadBitmapFromFile(
m_renderTarget,
m_WICFactory,
L"terrain0.png",
0,
0,
&m_terrainBitmap[0]);

hr = LoadBitmapFromFile(
m_renderTarget,
m_WICFactory,
L"terrain1.png",
0,
0,
&m_terrainBitmap[1]);

//Repeat for all 200 of the terrain PNG files.
//...



LoadBitmapFromFile is the same exact one shown in the MSDN samples:


HRESULT LoadBitmapFromFile(
ID2D1RenderTarget *pRenderTarget,
IWICImagingFactory *pIWICFactory,
PCWSTR uri,
UINT destinationWidth,
UINT destinationHeight,
ID2D1Bitmap **ppBitmap
)
{
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;

HRESULT hr = pIWICFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);

if (SUCCEEDED(hr))
{
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr))
{

// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = pIWICFactory->CreateFormatConverter(&pConverter);

}


if (SUCCEEDED(hr))
{
// If a new width or height was specified, create an
// IWICBitmapScaler and use it to resize the image.
if (destinationWidth != 0 || destinationHeight != 0)
{
UINT originalWidth, originalHeight;
hr = pSource->GetSize(&originalWidth, &originalHeight);
if (SUCCEEDED(hr))
{
if (destinationWidth == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationHeight) / static_cast<FLOAT>(originalHeight);
destinationWidth = static_cast<UINT>(scalar * static_cast<FLOAT>(originalWidth));
}
else if (destinationHeight == 0)
{
FLOAT scalar = static_cast<FLOAT>(destinationWidth) / static_cast<FLOAT>(originalWidth);
destinationHeight = static_cast<UINT>(scalar * static_cast<FLOAT>(originalHeight));
}

hr = pIWICFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr))
{
hr = pScaler->Initialize(
pSource,
destinationWidth,
destinationHeight,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr))
{
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
}
else // Don't scale the image.
{
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
}
if (SUCCEEDED(hr))
{

// Create a Direct2D bitmap from the WIC bitmap.
hr = pRenderTarget->CreateBitmapFromWicBitmap(
pConverter,
NULL,
ppBitmap
);
}

SafeRelease(&pStream);
SafeRelease(&pScaler);
SafeRelease(&pConverter);
SafeRelease(&pSource);
SafeRelease(&pDecoder);

return hr;
}


Won't they still all be loaded into memory the same as if I had just used one huge PNG file? I would only like to load them into memory as needed (like when the player reaches the edge of the screen and I need to load a different area of the terrain.)

To tell you the truth, I really don't know much about WIC. I've only learned to load an image this way using the MSDN tutorials. I'm not an expert with image formats, but I would like to have a high quality image for the terrain, and I've been told that PNG is a high-quality image format.

Do you think that manually reading the bytes of the image file and processing it myself would be the only way to accomplish what I need?
Another concern I have is that the Direct2D documentation also states:

Resource creation and deletion on hardware are expensive operations because they require lots of overhead for communicating with the video card.

Let's assume that I created many different PNG files for the terrain using the code shown above. When the player gets the edge of the screen, I would need to load in another bitmap and free the memory being used by the old bitmap. Wouldn't all of this creation/deletion be expensive and inefficient?

This must be a common problem for people who create 2D games where a player moves around a world. Could anyone shed some light on the best way to go about this?

This topic is closed to new replies.

Advertisement