Invalid arguments when creating texture.

Started by
4 comments, last by unbird 10 years, 9 months ago

I'm trying to create a texture for screen capture so I can grab the 1x1 mipmap to get the average color of the screen.

However, when I try to create the texture, it tells me I'm passing an invalid parameter (See error handling below), and I can't quite figure out what I'm missing here.

For those who know, it should be quick to spot.


	//Create Texture
	D3D10_TEXTURE2D_DESC	tBufferDesc;
	ID3D10Texture2D			*tBuffer = NULL;
	DXGI_SAMPLE_DESC		iBufferSamples = {1,0};
	tBufferDesc.Width		=	iScreenSizeX;
	tBufferDesc.Height		=	iScreenSizeY;
	tBufferDesc.MipLevels	        =	0;
	tBufferDesc.ArraySize	        =	1;
	tBufferDesc.Format		=	DXGI_FORMAT_R8G8B8A8_UINT;
	tBufferDesc.SampleDesc	        =	iBufferSamples;
	tBufferDesc.Usage		=	D3D10_USAGE_DEFAULT;
	tBufferDesc.BindFlags	        =	D3D10_BIND_SHADER_RESOURCE | D3D10_BIND_RENDER_TARGET;
	tBufferDesc.CPUAccessFlags      =       D3D10_CPU_ACCESS_READ;
	tBufferDesc.MiscFlags	        =	D3D10_RESOURCE_MISC_GENERATE_MIPS;
	
	HRESULT tBufferResult = pDevice->CreateTexture2D(&tBufferDesc, NULL , &tBuffer);
	switch(tBufferResult)
	{
	case E_FAIL:
		::MessageBoxA(NULL, "Attempted to create a device with the debug layer enabled and the layer is not installed.", "E_FAIL", NULL);
		return E_FAIL;
	case D3D10_ERROR_FILE_NOT_FOUND:
		::MessageBoxA(NULL, "The file was not found.", "D3D10_ERROR_FILE_NOT_FOUND", NULL);
		return E_FAIL;
	case D3D10_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS:
		::MessageBoxA(NULL, "There are too many unique instances of a particular type of state object.", "D3D10_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS", NULL);
		return E_FAIL;
	case E_INVALIDARG:
		::MessageBoxA(NULL, "An invalid parameter was passed to the returning function.", "E_INVALIDARG", NULL);
		return E_FAIL;
	case E_OUTOFMEMORY:
		::MessageBoxA(NULL, "Direct3D could not allocate sufficient memory to complete the call.", "E_OUTOFMEMORY", NULL);
		return E_FAIL;
	case S_FALSE:
		::MessageBoxA(NULL, "Alternate success value, indicating a successful but nonstandard completion (the precise meaning depends on context).", "S_FALSE", NULL);
		return E_FAIL;
	case S_OK:
		::MessageBoxA(NULL, "No error occurred.", "S_OK", NULL);
		break;

	}
Advertisement
Create the device with D3D10_CREATE_DEVICE_DEBUG and watch the debug output. It usually tells you what's going bad with great detail.

My bet is the format. INT formats can't be sampled which is probably necessary for auto-mipmaps.

Debug mode doesn't work, I guess it's because of VS Express? Debugging information could not be found or does not match.

But I tried several formats to no avail.

Nah, I use Express as well. Sounds like your DX SDK isn't installed properly.

Another problem is D3D10_CPU_ACCESS_READ. That usually works with staging resources only.

Really get that debug layer working, one can not work sensibly without IMO.

Yeah, no kidding. I was so used to all of those tools and then they stopped working when i started using 2012.

The thing is, it has never worked. Ever since I first started. Even without the DXSDK.

One thing I AM trying to do lately is avoid the D3DX files. I would rather avoid dependency on the standalone SDK whenever possible, now that MS has shied away from it.

I'll try tweaking that setting real quick...

Oh wow. That did it! Thanks!

Of course, the intent is to be able to access the 1x1 mipmap level to output the color, will removing CPU readability affect it? If so, I think I have a workaround, but I would prefer all the speed I can get.

Edit: Okay, now I'm curious as to how you grab the front or back buffer. Do I have to create a swap chain first? And if so, would I still be able to get data that includes the windows GUI?

Hmmm, sounds like you've fallen prey to this little niceness. Uninstalling helped me (use at your own risk, I don't know how well it behaves in conjunction with VS 2012, I'm still using 2010).

Of course, the intent is to be able to access the 1x1 mipmap level to output the color, will removing CPU readability affect it? If so, I think I have a workaround, but I would prefer all the speed I can get.

Edit: Okay, now I'm curious as to how you grab the front or back buffer. Do I have to create a swap chain first? And if so, would I still be able to get data that includes the windows GUI?


You cannot read back the texture directly. You need a staging texture (of same format and dimensions) and copy the content from your other with ID3D10Device::CopyResource. Then you ID3D10Texture2D::Map with D3D10_MAP_READ.

Now since I haven't touched D3D10 myself (I'm using D3D11) I might miss some details, but I think that's the gist of it. Maybe you could also use CopySubresourceRegion only, since you really don't need to copy the whole texture, just that one pixel.

This topic is closed to new replies.

Advertisement