pixel shader multiple render targets dx11

Started by
7 comments, last by rouncer 12 years, 8 months ago
how do you define a pixel shader so it outputs to more than one render target, im trying this->

struct PS_P_OUTPUT
{
float4 col0 : SV_Target0;
float4 col1 : SV_Target1;
};


but its not working, what have i got wrong?
Advertisement
That is correct... So you're either not correctly binding the render targets or something is wrong in your pixel shader...

-How are you binding multiple render targets?

-Can you post your pixel shader?
shader->

struct V2S_INPUT
{
float4 pc : TEXCOORD0;
// float2 c : TEXCOORD1;
};

struct P2S_INPUT
{
float4 pos : SV_POSITION;
float3 nor : TEXCOORD0;
float3 wpos : TEXCOORD1;
};

struct G2S_INPUT
{
float4 pos : POSITION;
float3 nor : TEXCOORD0;
};

struct PS_P_OUTPUT
{
float4 col0 : SV_Target0;
float4 col1 : SV_Target1;
};


G2S_INPUT VS_P(V2S_INPUT Input)
{
G2S_INPUT Output;

Output.pos = float4(float3(Input.pc.x*255,Input.pc.y*255,Input.pc.z*255)*voxel_size+chunk_pos,1);
Output.nor= float3(Input.pc.w,Input.pc.w,Input.pc.w);

return Output;
}



[maxvertexcount(4)]
void GS_P(point G2S_INPUT In[1], inout TriangleStream<P2S_INPUT> TriStream)
{
P2S_INPUT Out;

Out.nor.xyz = In[0].nor.xyz;


float3 up=float3(view._12,view._22,view._32);
float3 right=float3(view._11,view._21,view._31);

float3 ppos;

ppos=In[0].pos.xyz+up*voxel_size*1.25f;
Out.pos=mul(float4(ppos,1),wvp);
Out.wpos=In[0].pos.xyz;
TriStream.Append(Out);
ppos=In[0].pos.xyz+right*voxel_size*1.25f+up*voxel_size*1.25f;
Out.pos=mul(float4(ppos,1),wvp);
Out.wpos=In[0].pos.xyz;
TriStream.Append(Out);
ppos=In[0].pos.xyz;
Out.pos=mul(float4(ppos,1),wvp);
Out.wpos=In[0].pos.xyz;
TriStream.Append(Out);
ppos=In[0].pos.xyz+right*voxel_size*1.25f;
Out.pos=mul(float4(ppos,1),wvp);
Out.wpos=In[0].pos.xyz;
TriStream.Append(Out);
TriStream.RestartStrip();
}

PS_P_OUTPUT PS_P(P2S_INPUT Input)
{
PS_P_OUTPUT pspo;

pspo.col0=float4(Input.nor,1);
pspo.col1=float4(Input.wpos,1);

return pspo;
}



binding render targets->

//set multiple render targets
RTV mrt[2];

mrt[0]=rtv;
mrt[1]=ws_r;


dc->OMSetRenderTargets(2,mrt,dsv);
dc->ClearRenderTargetView(rtv, ClearColor);
dc->ClearRenderTargetView(ws_r, ClearColor);
dc->ClearDepthStencilView(dsv, D3D11_CLEAR_DEPTH, 1.0f, 0);




its actually rendering the first render target, but not the second... if it switch them around it draws the opposite colour, but for some reason it wont draw the second target...
do you have to make the render target views any differently?
The code you posted looks correct... How are you creating the render targets?

Also, can you use the [.source] or [.code] tags (without the dot)? So your source looks like this:
[source]
PS_P_OUTPUT PS_P(P2S_INPUT Input)
{
PS_P_OUTPUT pspo;

pspo.col0=float4(Input.nor,1);
pspo.col1=float4(Input.wpos,1);

return pspo;
}
[/source]

P.S: Why are you trying to store the world position?
i need worldspace cause im writing a brush that paints 3d points, so i need the world position to place the brush sphere on the surface.
note i could just render the whole thing again, but it would be very computational as its a point cloud (rendered as lots of billboards, hence the gs)...

heres the second render target
i wrote this myself.
scw and sch are the screen dimensions, the same as the main render target.

[source]
if(1)
{
int size_x=scw;
int size_y=sch;
ID3D11Texture2D * pTexture2D = NULL;

D3D11_TEXTURE2D_DESC desc;
memset(&desc,0,sizeof(D3D11_TEXTURE2D_DESC));
desc.Width = (UINT)size_x;
desc.Height = (UINT)size_y;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
desc.CPUAccessFlags = NULL;//D3D11_CPU_ACCESS_WRITE;

D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;

srvDesc.Format = desc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = desc.MipLevels;

D3D11_RENDER_TARGET_VIEW_DESC rtvdesc;
memset(&rtvdesc,0,sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
rtvdesc.Format=desc.Format;
rtvdesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;



// Create the shader resource view.
dev->CreateTexture2D(&desc, NULL, &pTexture2D);
dev->CreateShaderResourceView( pTexture2D, &srvDesc, &ws);
dev->CreateRenderTargetView(pTexture2D, &rtvdesc, &ws_r);
}

[/source]



heres the first.
its different because i created this when i created the device (copy paste from sample)
you get it from the swap chain (sc)

[source]

// Create a render target view
TEX pBackBuffer = NULL;
hr = sc->GetBuffer(0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer);

hr = dev->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
pBackBuffer->Release();

[/source]
so as you can see, i am without a doubt confused... i actually did it mostly right... there must be something small stopping it from working.

note, this is my first time ever using mrt, so its understandable i stuffed it up in a small way.

thanks for the help tho, TiagoCosta.
hang on! it might be the blend state... ill get back to you.
yes that was it, i didnt set render target[1] variables in the blend state... ahh thanks man!

This topic is closed to new replies.

Advertisement