Jump to content

  • Log In with Google      Sign In   
  • Create Account

DirectX Render to Texture problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 01 July 2012 - 06:06 AM

i was trying to render to a texture to use it for post-processing. I've read a lot of tutorials about this stuff und have tried it for myself now, but apparently i am doing something wrong.
So first of all these are the error message im getting over and over:

D3D11: ERROR: ID3D11DeviceContext::OMSetRenderTargets: The RenderTargetView at slot 0 is not compatable with the DepthStencilView. DepthStencilViews may only be used with RenderTargetViews if the effective dimensions of the Views are equal, as well as the Resource types, multisample count, and multisample quality. The RenderTargetView at slot 0 has (w:400,h:300,as:1), while the Resource is a Texture2D with (mc:1,mq:0). The DepthStencilView has (w:1280,h:720,as:1), while the Resource is a Texture2D with (mc:1,mq:0). D3D11_RESOURCE_MISC_TEXTURECUBE factors into the Resource type, unless GetFeatureLevel() returns D3D_FEATURE_LEVEL_10_1 or greater. [ STATE_SETTING ERROR #388: OMSETRENDERTARGETS_INVALIDVIEW ]
	D3D11: ERROR: ID3D11DeviceContext::Draw: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT ]

And:

	D3D11: ERROR: ID3D11DeviceContext::Draw: Vertex Shader - Geometry Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (RADIUS,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
	D3D11: ERROR: ID3D11DeviceContext::Draw: Vertex Shader - Geometry Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (TEXTUREINDEX,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
	D3D11: ERROR: ID3D11DeviceContext::Draw: Geometry Shader - Pixel Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (POSITION,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND ]
	D3D11: ERROR: ID3D11DeviceContext::OMSetRenderTargets: The RenderTargetView at slot 0 is not compatable with the DepthStencilView. DepthStencilViews may only be used with RenderTargetViews if the effective dimensions of the Views are equal, as well as the Resource types, multisample count, and multisample quality. The RenderTargetView at slot 0 has (w:400,h:300,as:1), while the Resource is a Texture2D with (mc:1,mq:0). The DepthStencilView has (w:1280,h:720,as:1), while the Resource is a Texture2D with (mc:1,mq:0). D3D11_RESOURCE_MISC_TEXTURECUBE factors into the Resource type, unless GetFeatureLevel() returns D3D_FEATURE_LEVEL_10_1 or greater. [ STATE_SETTING ERROR #388: OMSETRENDERTARGETS_INVALIDVIEW ]

I am pretty sure that i am misunterstanding a major aspect of this rendertarget switching and stuff.
When my DirectX Device is created i am also creating this:

D3D11_TEXTURE2D_DESC textureDesc;
	 D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
	 D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;

	 ZeroMemory(&textureDesc, sizeof(textureDesc));
	
	 textureDesc.Width = 800/2;
	 textureDesc.Height = 600/2;
	 textureDesc.MipLevels = 1;
	 textureDesc.ArraySize = 1;
	 textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	 textureDesc.SampleDesc.Count = 1;
	 textureDesc.Usage = D3D11_USAGE_DEFAULT;
	 textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
	 textureDesc.CPUAccessFlags = 0;
	 textureDesc.MiscFlags = 0;
	
	
	 pDevice->CreateTexture2D(&textureDesc, NULL, &renderTargetTextureMap);
	
	 renderTargetViewDesc.Format = textureDesc.Format;
	 renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
	 renderTargetViewDesc.Texture2D.MipSlice = 0;
	
	 .
	 pDevice->CreateRenderTargetView(renderTargetTextureMap, &renderTargetViewDesc, &renderTargetViewMap);
  
	 shaderResourceViewDesc.Format = textureDesc.Format;
	 shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	 shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
	 shaderResourceViewDesc.Texture2D.MipLevels = 1;
	
	 pDevice->CreateShaderResourceView(renderTargetTextureMap, &shaderResourceViewDesc, &shaderResourceViewMap);
	
	 D3DVERTEX quad[4] = {
	 {D3DXVECTOR3(0,0,0), D3DXVECTOR2(0,0)},
	 {D3DXVECTOR3(1,1,0), D3DXVECTOR2(1,1)},
	 {D3DXVECTOR3(1,0,0), D3DXVECTOR2(1,0)},
	 {D3DXVECTOR3(0,1,0), D3DXVECTOR2(0,1)}};
	
	 HRESULT hr;
	
	 D3D11_SUBRESOURCE_DATA InitData;
	 InitData.pSysMem = &quad[0];
	 InitData.SysMemPitch = sizeof(D3DVERTEX);
	 InitData.SysMemSlicePitch = 0;
	
	 D3D11_BUFFER_DESC bufferDesc;
	 bufferDesc.Usage			= D3D11_USAGE_DEFAULT;
	 bufferDesc.ByteWidth		= 4 * InitData.SysMemPitch;
	 bufferDesc.BindFlags		= D3D11_BIND_VERTEX_BUFFER;
	 bufferDesc.CPUAccessFlags   = 0;
	 bufferDesc.MiscFlags		= 0;
	
	 V_RETURN(pDevice->CreateBuffer( &bufferDesc, &InitData, &m_pVertexBuffer ));
	
	 const D3D11_INPUT_ELEMENT_DESC layout[] =
	 {
	 { "POSITION",	0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	 { "TEXCOORD",	0, DXGI_FORMAT_R32G32_FLOAT,	0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	 };
	 UINT numElements = sizeof( layout ) / sizeof( layout[0] );
	 D3DX11_PASS_DESC passDesc;
	 m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->GetDesc(&passDesc);
	
	 V_RETURN( pDevice->CreateInputLayout( layout, numElements, passDesc.pIAInputSignature,
	   passDesc.IAInputSignatureSize, &m_pInputLayout ) );

So i am creating a ShaderResourceView and a RenderTargetView aswell as a cube with a vertex buffer to draw this cube where the texture will be drawn on.

Now in every frame update after ALL the other stuff was drawn im using this:


pDevice->GetImmediateContext(&pd3dImmediateContext);
	 pd3dImmediateContext->OMSetRenderTargets( 1, &renderTargetViewMap, pDSV );
	
	 pd3dImmediateContext->ClearRenderTargetView(renderTargetViewMap, g_ClearColor);
	 ID3D11Buffer* vbs[] = { m_pVertexBuffer, };
		unsigned int strides[] = { sizeof(D3DVERTEX), }, offsets[] = { 0, };
	
	 pd3dImmediateContext->IASetVertexBuffers(0,1, vbs, strides, offsets);
	 pd3dImmediateContext->IASetInputLayout(m_pInputLayout);
	 pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
	
	 //TODO: Move to header file.
	 ID3DX11EffectShaderResourceVariable* g_SpriteEV;
	
	 g_SpriteEV = m_pEffect->GetVariableByName("g_PostProcessTex")->AsShaderResource();
	
	 g_SpriteEV->SetResource(shaderResourceViewMap);
	 m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->Apply(0, pd3dImmediateContext);
	
	 pd3dImmediateContext->Draw(4, 0);

I have not explicitly created a depthStencilView which i would guess is my main error here. But i have no idea how to create something like this right.
I am pretty much confused about how this all is working in the background...maybe someone cares to explain this.
Am i doing anything right here or is this just wrong? Posted Image

Thanks for anyone who cares to help.

Sponsor:

#2 Martin Perry   Members   -  Reputation: 1324

Like
0Likes
Like

Posted 01 July 2012 - 12:19 PM

I see several problems with posted snipsets.
1) If you render to texture, that is smaller than your standard render target, you must change viewport to texture dimensions. (maybe last error is due to this)
2) Before you render to texture, save your targets. After you render to texture, dont forget to unbind render targets from device and restore original ones (aka. classic "screen")
3) If you render "classic way" to the screen, can you see some errors ? Because errors with shaders are not render-to-texture relevant. In DX11 rendering geometry to texture is exactly the same as classic rendering, except for render targets changes.

Do you know this tutorial: http://www.rastertek.com/dx11tut22.html
It´s a good start and my renderer to texture is based on it. Everything works correctly.

#3 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 01 July 2012 - 01:04 PM

Hey, thanks for your advices.
I've reworked my whole Render to Texture process based on this tutorial and it gives me no errors and it seems like that i can work with the texture.
The only problem left now ist, that it seems like i cannot draw a simple square to draw my texture on anymore. This is how it looks like at the moment (with a black/ white shader):
http://imgur.com/HrKXR

this is the code AFTER RenderToTexture() and RenderScene():



Vertex v[] =
{
Vertex( -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f ),
Vertex( -0.5f,  0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ),
Vertex(  0.5f,  0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f ),
Vertex(  0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ),
};

DWORD indices[] = {
0, 1, 2,
0, 2, 3,
};

HRESULT hr;

D3D11_BUFFER_DESC indexBufferDesc;
ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );

indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(DWORD) * 2 * 3;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;

D3D11_SUBRESOURCE_DATA iinitData;

iinitData.pSysMem = indices;
pDevice->CreateBuffer(&indexBufferDesc, &iinitData, &m_pIndexBuffer);

// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &v[0];
InitData.SysMemPitch = sizeof(Vertex);
InitData.SysMemSlicePitch = 0;

D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage			= D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth		= 4 * InitData.SysMemPitch;
bufferDesc.BindFlags		= D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags   = 0;
bufferDesc.MiscFlags		= 0;

V_RETURN(pDevice->CreateBuffer( &bufferDesc, &InitData, &m_pVertexBuffer ));

const D3D11_INPUT_ELEMENT_DESC layout[] = // http://msdn.microsoft.com/en-us/library/bb205117%28v=vs.85%29.aspx
{
{ "POSITION",	0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
//{ "TEXCOORD",	0, DXGI_FORMAT_R32G32_FLOAT,	0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = sizeof( layout ) / sizeof( layout[0] );
D3DX11_PASS_DESC passDesc;
m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->GetDesc(&passDesc);

V_RETURN( pDevice->CreateInputLayout( layout, numElements, passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize, &m_pInputLayout ) );


ID3D11Buffer* vbs[] = { m_pVertexBuffer, };
	unsigned int strides[] = { sizeof(D3DVERTEX), }, offsets[] = { 0, };

pd3dImmediateContext->IASetVertexBuffers(0,1, vbs, strides, offsets);
pd3dImmediateContext->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
pd3dImmediateContext->IASetInputLayout(m_pInputLayout);
pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

//TODO: Move to header file.
ID3DX11EffectShaderResourceVariable* g_SpriteEV;

g_SpriteEV = m_pEffect->GetVariableByName("g_PostProcessTex")->AsShaderResource();

g_SpriteEV->SetResource(m_shaderResourceView);
m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->Apply(0, pd3dImmediateContext);

pd3dImmediateContext->DrawIndexed( 6, 0, 0 );

g_SpriteEV->SetResource(NULL);
m_pEffect->GetTechniqueByName("Render")->GetPassByName("P0_PP")->Apply(0, pd3dImmediateContext);

If you want me to post all the code for the different render processes just let me know.

Edited by pulo, 01 July 2012 - 01:06 PM.


#4 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 01 July 2012 - 03:26 PM

Okay...i found one of my problems..the stride of the vertex buffer was set to an "old" value.. Now i've set up the square with these values:

Vertex v[] =
{
  Vertex( -1.f, -1.f, 0.f ),
  Vertex(  1.f, -1.f, 0.f ),
  Vertex(  1.f,  1.f, 0.f ),
  Vertex( -1.f,  1.f, 0.f ),
};
DWORD indices[] = {
  0, 1, 2,
  0, 2, 3,
};

But it produces this:

http://imgur.com/EsmV3

so basically it splits the whole thing in 4 parts. There are some things i've noticed aswell.
1. The Texture on the square seems to be exaclty mirrored (this could be fixed through the shader though)
2. The 4 tiles are not present at the beginning - i have to move my camera just a little bit then it appears.
3. Its disappearing in random intervals.

Edited by pulo, 01 July 2012 - 03:26 PM.


#5 Martin Perry   Members   -  Reputation: 1324

Like
1Likes
Like

Posted 02 July 2012 - 12:53 AM

Do you have correct TEXCOORDs ? Also try to turn off DepthBuffer and DepthWrites.

I am setting my full screen quad this way:
float startX = -1;
float startY = -1;
float endX = 1;
float endY = 1;

MyMath::Vector3 vecA = MyMath::Vector3(startX, startY, 0);
MyMath::Vector3 vecB = MyMath::Vector3(endX  , startY, 0);
MyMath::Vector3 vecC = MyMath::Vector3(startX, endY, 0);
MyMath::Vector3 vecD = MyMath::Vector3(endX  , endY, 0);

std::vector<QuadVertex> pos;
QuadVertex a = {vecA.X, vecA.Y, vecA.Z, 0, 1, 0};
QuadVertex b = {vecB.X, vecB.Y, vecB.Z, 1, 1, 1};
QuadVertex c = {vecC.X, vecC.Y, vecC.Z, 0, 0, 3};
QuadVertex d = {vecD.X, vecD.Y, vecD.Z, 1, 0, 2};

std::vector<short> ind;
ind.push_back(0); ind.push_back(1); ind.push_back(2);
ind.push_back(2); ind.push_back(1); ind.push_back(3);


#6 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 02 July 2012 - 08:07 AM

Hey,

thanks for your help. I really appreciate it. Your hint with the TexCoords was a hit. I was using an old calculation for them. Now i can render the whole texture over my scene with a black and white shader. I have a few questions / problems left though:

1. I understand that for the coordinate system for rendering 0,0 is the center and -1,-1 is the lower left corner, isnt it? But what about the texture coordinates. If i understand the numbers correctly there 0,0 is the upper left corner? Why is there such a difference?
2. My shader seems not to be active at first startup , i have to move my camera a bit before it comes active.
3. The shader effect seems to disappear from time to time (random)

I have tried to disable the depth for the Stencil with something like this:

D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
ID3D11DepthStencilState* m_depthDisabledStencilState;
depthDisabledStencilDesc.DepthEnable = false;
depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthDisabledStencilDesc.StencilEnable = true;
depthDisabledStencilDesc.StencilReadMask = 0xFF;
depthDisabledStencilDesc.StencilWriteMask = 0xFF;
depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
pd3dDevice->CreateDepthStencilState(&depthDisabledStencilDesc, &m_depthDisabledStencilState);
pd3dImmediateContext->OMSetDepthStencilState(m_depthDisabledStencilState, 1);


g_PostProcessing->RenderPostProcess(pd3dDevice, g_Camera, g_ClearColor);

depthDisabledStencilDesc.DepthEnable = true;
pd3dDevice->CreateDepthStencilState(&depthDisabledStencilDesc, &m_depthDisabledStencilState);
pd3dImmediateContext->OMSetDepthStencilState(m_depthDisabledStencilState, 1);

The renderpostprocess call is rendering the quad with the texture on the screen after RenderToTexture() and RenderScene() was called.

#7 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 02 July 2012 - 12:36 PM

I was able to solve all the problems by just setting the DepthStencilState to DepthEnable = false in the shader file :)
Thanks for your help!

#8 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 04 July 2012 - 03:46 PM

Hello again,

now i have researched some stuff and there are some open questions in my mind which i hope some of you can answer me. I am now trying to downsample the rendered texture to the 4th of the original size. After that i want to use a bloom effect or something and (upsample?) it again to merge it with the original rendered scene.
Now my question is fairly simple: how can i let my original texture (full screen size) walk through some shader passes so at the end i will have a downsampled texture with effects and the original texture to work with?
i dont understand how to work with multiple passes. How do i pass the texture from one pass to another and at the end merge the original one with the edited one?
Thanks in advance

#9 mhagain   Crossbones+   -  Reputation: 7959

Like
0Likes
Like

Posted 04 July 2012 - 04:59 PM

You basically create two rendertargets. Size doesn't matter - they just need to be big enough for the largest (typically your screen dimensions).

So, in pass 1 you draw from target 0 to target 1 as a half-sized quad (the size of the quad doesn't have to be the same as your render target size).
In pass 2 you draw from 1 to 0 as a half-sized quad (halving again the half size from above).
Repeat until you get it down to the size you want, flipping the render targets each pass.

After the final pass you set back your default render target, then draw from the target set in the final pass to this.

Edit: silly me, I forgot that with D3D11 you can autogen mipmaps from a render target. I've never used this, but it does present another option for you that may be faster.

Edited by mhagain, 04 July 2012 - 05:49 PM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 05 July 2012 - 04:00 AM

I think i understood this. But i cant really image how to draw from one target to another? is it a simple draw() call with a fullscreen quad textured by the texture i want to pass and the render target i want to use next?

#11 pulo   Members   -  Reputation: 228

Like
0Likes
Like

Posted 06 July 2012 - 03:59 PM

The only thing i dont understand is you i pass one texture from pass to pass. Is this a simple Draw() call with just the render target set, or is there a "better" way?

#12 mhagain   Crossbones+   -  Reputation: 7959

Like
0Likes
Like

Posted 06 July 2012 - 04:28 PM

Draw with the rendertarget set, yes.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS