Texture2D Load only returns red channel

Started by
4 comments, last by trevex 11 years, 3 months ago

I am currently planning to implement a few Post-Process Effects. The first thing I did was rendering into a texture, when I have a look at the
texture in PixWin it looks totally fine.

The texture has the format "DXGI_FORMAT_R8G8B8A8_UNORM".

The next step was to render the texture to the screen, but the output is simply "red" only the red channel is rendered.


float4 VSMain(in float3 Position : POSITION) : SV_Position 
{ 
	return float4(Position, 1.0f); 
}

float PSDisabledMain(in float4 screenPos : SV_Position) : SV_Target0 
{ 
	int3 sampleIndices = int3(screenPos.xy, 0);
	float3 color = BackBufferMap.Load(sampleIndices).xyz;
	return color;
}

I played around with the shader code but nothing seems to fix it. I am new to directx coming from a opengl background and seem to have overseen something.

Thanking you in anticipation,
Nik

P.S. If you need more informations or anything else let me know

EDIT:

My assumption is it has something todo with the texture format but unsure how to fix it...

Advertisement

How did you define BackBufferMap? Please note that Load() gets texels without filtering and it uses integer offset, not UVs (pretty much raw access to memory).
Also you return only float, which is single number, wonder why that code even compiled for you.
If you're simply rendering fullscreen quad it's normally done like this:


SamplerState BackBufferSampler : register(s0);
Texture2D BackBufferMap : register(t0);

float4 PSDisabledMain(in float4 screenPos : SV_POSITION) : SV_TARGET0 {
    return BackBufferMap.Sample(BackBufferSampler, screenPos.xy);
}

I think that Zaoshi Kaba is on the right track here - you're returning a single float from your shader, whereas for colour you would normally return float4 instead; have you checked your shader compiler output for any warnings?

Also note the comment about using Load - this may seem fine for simpler effects, but if you're going to be doing anything that distorts or warps the texture, you will definitely be wanting a linear Sample instead.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I am totally confused right now when I use sample the screen is simply red, but the general problem persists.

Why is Load only returning the red value?

the color variable always just receives the red value and the rest are underscores in pix...

The shader with vertex shader


float4 VSMain(in float3 Position : POSITION) : SV_Position 
{ 
	return float4(Position, 1.0f); 
}

float PSDisabledMain(in float4 screenPos : SV_Position) : SV_Target0 
{ 
	//BackBufferMap.Sample(TexureSampler, screenPos.xy);
	int3 sampleIndices = int3(screenPos.xy, 0);
	float4 color = BackBufferMap.Load(sampleIndices);
	return color;
}

The vertices used:


glm::vec3 vertices[] =
{
	glm::vec3(-1.0f, -1.0f,  0.0f),
	glm::vec3(-1.0f,  1.0f,  0.0f),
	glm::vec3( 1.0f, -1.0f,  0.0f),
	glm::vec3( 1.0f,  1.0f,  0.0f),
};	

The vertices and Load function is simply the same I am using in my deferred rendering shaders...

And here's how I create the backbuffer:



	D3D11_TEXTURE2D_DESC backTextureDesc;
	D3D11_RENDER_TARGET_VIEW_DESC backTargetViewDesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC backResourceViewDesc;


	ZeroMemory(&backTextureDesc, sizeof(backTextureDesc));
	backTextureDesc.Width = width;
	backTextureDesc.Height = height;
	backTextureDesc.MipLevels = 1;
	backTextureDesc.ArraySize = 1;
	backTextureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	backTextureDesc.SampleDesc.Count = 1;
	backTextureDesc.Usage = D3D11_USAGE_DEFAULT;
	backTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	backTextureDesc.CPUAccessFlags = 0;
	backTextureDesc.MiscFlags = 0;

	HR(m_d3dDevice->CreateTexture2D(&backTextureDesc, NULL, &m_BackTargetTexture));
	
	backTargetViewDesc.Format = backTextureDesc.Format;
	backTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
	backTargetViewDesc.Texture2D.MipSlice = 0;

	HR(m_d3dDevice->CreateRenderTargetView(m_BackTargetTexture, &backTargetViewDesc, &m_BackTargetView));

	backResourceViewDesc.Format = backTextureDesc.Format;
	backResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	backResourceViewDesc.Texture2D.MostDetailedMip = 0;
	backResourceViewDesc.Texture2D.MipLevels = 1;

	HR(m_d3dDevice->CreateShaderResourceView(m_BackTargetTexture, &backResourceViewDesc, &m_BackResourceView));

See this:


float PSDisabledMain

It means that your pixel shader is just returning a single float. Change that "float" to "float4".

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Oh god can't believe I didn't notice that! Thank you so much!

This topic is closed to new replies.

Advertisement