Can't create texture from raw data

Started by
1 comment, last by viper110110 11 years ago

I am getting raw data from stb_image.c for an image I want to load. I am then trying to turn it into a drawable texture. I haven't been able to find any tutorials on how to load an image from raw data. I have assembled some code from what I could gather from the documentation. The image drawing code works because I use different code to set the same resource view with a dds texture and it works. All the results are S_OK.


int x, y, n;
	unsigned char *data = stbi_load(charFilename, &x, &y, &n, 0);

	D3D11_TEXTURE2D_DESC desc;
	desc.Width = x;
	desc.Height = y;
	desc.MipLevels = 1;
	desc.ArraySize = 1;
	desc.Format = DXGI_FORMAT_R8G8B8A8_UINT;
	desc.SampleDesc.Count = 1;
	desc.SampleDesc.Quality = 0;
	desc.Usage = D3D11_USAGE_DEFAULT;
	desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	desc.CPUAccessFlags = 0;
	desc.MiscFlags = 0;

	/*D3D11_SUBRESOURCE_DATA subres;
	subres.pSysMem = data;
	subres.SysMemPitch = x * n;
	subres.SysMemSlicePitch = 0;*/

	ID3D11Texture2D *texture2D = 0;

	HRESULT result = device->CreateTexture2D(&desc, NULL, &texture2D);
	if (FAILED(result))
	{
		DebugConsole::writeLine("Failed to create Texture2D");
	}

	D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
	resourceViewDesc.Format = desc.Format;
	resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	resourceViewDesc.Texture2D.MostDetailedMip = 0;
	resourceViewDesc.Texture2D.MipLevels = 1;

	result = device->CreateShaderResourceView(texture2D, &resourceViewDesc, &m_texture);
	if (FAILED(result))
	{
		DebugConsole::writeLine("Failed to create resource view");
	}

Advertisement

As far as I can tell it might be because you're never actually using subres (at least in the snippet), the following might work:


HRESULT result = device->CreateTexture2D(&desc, &subres, &texture2D);

You can find a small example at the bottom of the documentation:

http://msdn.microsoft.com/en-us/library/ff476521(VS.85).aspx

As far as I can tell it might be because you're never actually using subres (at least in the snippet), the following might work:


HRESULT result = device->CreateTexture2D(&desc, &subres, &texture2D);

You can find a small example at the bottom of the documentation:

http://msdn.microsoft.com/en-us/library/ff476521(VS.85).aspx

Ya, I took it out because I saw it out in an unrelated sample, but it never occured to me that I was actually putting the data in there. I have now tried putting it back in and I am still having trouble. No errors, just no sprite.

This topic is closed to new replies.

Advertisement