Jump to content
  • Advertisement
Sign in to follow this  
cozzie

DX11 Rendering depth complexity (luna d3d11 ch10 ex8)

This topic is 1048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I've been struggling to do one of the exercises of the d3d11 book I'm working my way through.

The goal:

 

1 - render a scene first only to the stencil buffer, using STENCIL_OP_INCR and COMPARISON_ALWAYS, that way for each pixel I count the number of times something is written to that pixel on the stencil buffer

(i.e. when a specific pixel is increased 3 times, that pixel would have been 'overdrawn' 2 times)

2 - render the depth complexity of the scene to the render target, using the data on the stencil buffer.

By using a depthstencilstate with STENCIL_OP_KEEP, so the stencil buffer isn't modified anymore.

Using COMPARISON_EQUAL should enable me to draw the different levels of complexity, using colored quads.
I.e.: a red quad for all pixels with stencil ref/ depth complexity 1

green quad for stencil ref/ depth complexity 2 etc.

 

In the end it should look something like the screenshot below.

 

So far, I managed to render the scene only to the stencil buffer (not to render target) and create a depthstencil state that takes care of the 'counting'. After that I've set back the renderstate and depth stencil state to render to the render target again.

 

From here I'm stuck, how should I proceed to render the 'quads' using the depthstencilstate and the 'content' currently on the stencil bufer?

 

The code:

// the different states
	// BLEND state: Transparent
	D3D11_BLEND_DESC transparentDesc = {0};
	transparentDesc.AlphaToCoverageEnable = false;
	transparentDesc.IndependentBlendEnable = false;

	transparentDesc.RenderTarget[0].BlendEnable		= true;								// PRACTICE: EXERCISE 2
	transparentDesc.RenderTarget[0].SrcBlend		= D3D11_BLEND_SRC_ALPHA;		
	transparentDesc.RenderTarget[0].DestBlend		= D3D11_BLEND_INV_SRC_ALPHA;		
	transparentDesc.RenderTarget[0].BlendOp			= D3D11_BLEND_OP_ADD;				
	transparentDesc.RenderTarget[0].SrcBlendAlpha		= D3D11_BLEND_ONE;			
	transparentDesc.RenderTarget[0].DestBlendAlpha		= D3D11_BLEND_ZERO;
	transparentDesc.RenderTarget[0].BlendOpAlpha		= D3D11_BLEND_OP_ADD;			
	transparentDesc.RenderTarget[0].RenderTargetWriteMask	= D3D11_COLOR_WRITE_ENABLE_ALL;

	HR(device->CreateBlendState(&transparentDesc, &mTransparentBS));

	// BLEND state: Transparent => NO WRITING TO RENDER TARGET
	D3D11_BLEND_DESC transparentNoWriteDesc = {0};
	transparentNoWriteDesc.AlphaToCoverageEnable = false;
	transparentNoWriteDesc.IndependentBlendEnable = false;

	transparentNoWriteDesc.RenderTarget[0].BlendEnable	= true;								// PRACTICE: EXERCISE 2
	transparentNoWriteDesc.RenderTarget[0].SrcBlend		= D3D11_BLEND_SRC_ALPHA;
	transparentNoWriteDesc.RenderTarget[0].DestBlend	= D3D11_BLEND_INV_SRC_ALPHA;
	transparentNoWriteDesc.RenderTarget[0].BlendOp		= D3D11_BLEND_OP_ADD;
	transparentNoWriteDesc.RenderTarget[0].SrcBlendAlpha	= D3D11_BLEND_ONE;
	transparentNoWriteDesc.RenderTarget[0].DestBlendAlpha	= D3D11_BLEND_ZERO;
	transparentNoWriteDesc.RenderTarget[0].BlendOpAlpha	= D3D11_BLEND_OP_ADD;	
	transparentNoWriteDesc.RenderTarget[0].RenderTargetWriteMask	= 0;

	HR(device->CreateBlendState(&transparentNoWriteDesc, &mTransparentNoWriteBS));

	// BLEND STATE: NO WRITING TO THE RENDER TARGET
	D3D11_BLEND_DESC noRenderTargetDesc = {0};
	noRenderTargetDesc.AlphaToCoverageEnable = false;
	noRenderTargetDesc.IndependentBlendEnable = false;

	noRenderTargetDesc.RenderTarget[0].BlendEnable    = false;
	noRenderTargetDesc.RenderTarget[0].SrcBlend       = D3D11_BLEND_ONE;
	noRenderTargetDesc.RenderTarget[0].DestBlend      = D3D11_BLEND_ZERO;
	noRenderTargetDesc.RenderTarget[0].BlendOp        = D3D11_BLEND_OP_ADD;
	noRenderTargetDesc.RenderTarget[0].SrcBlendAlpha  = D3D11_BLEND_ONE;
	noRenderTargetDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
	noRenderTargetDesc.RenderTarget[0].BlendOpAlpha   = D3D11_BLEND_OP_ADD;
	noRenderTargetDesc.RenderTarget[0].RenderTargetWriteMask = 0;

	HR(device->CreateBlendState(&noRenderTargetDesc, &mNoRenderTargetWriteBS));

	// DEPTH STENCIL STATE: FOR REFERENCE COUNTING OF DEPTH COMPLEXITY THROUGH STENCIL BUFFER
	D3D11_DEPTH_STENCIL_DESC stencilOnlyDesc;
	stencilOnlyDesc.DepthEnable      = true;
	stencilOnlyDesc.DepthWriteMask   = D3D11_DEPTH_WRITE_MASK_ZERO;
    	stencilOnlyDesc.DepthFunc        = D3D11_COMPARISON_LESS; 
    	stencilOnlyDesc.StencilEnable    = true;
    	stencilOnlyDesc.StencilReadMask  = 0xff;
    	stencilOnlyDesc.StencilWriteMask = 0xff;

    	stencilOnlyDesc.FrontFace.StencilFailOp      = D3D11_STENCIL_OP_KEEP;
	stencilOnlyDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
	stencilOnlyDesc.FrontFace.StencilPassOp      = D3D11_STENCIL_OP_INCR;		// counter for depth complexity per pixel
	stencilOnlyDesc.FrontFace.StencilFunc        = D3D11_COMPARISON_ALWAYS;		// count all pixels

		// these don't matter, because rendering only frontfacing polys
   	stencilOnlyDesc.BackFace.StencilFailOp       = D3D11_STENCIL_OP_KEEP;
	stencilOnlyDesc.BackFace.StencilDepthFailOp  = D3D11_STENCIL_OP_KEEP;
	stencilOnlyDesc.BackFace.StencilPassOp       = D3D11_STENCIL_OP_REPLACE;
	stencilOnlyDesc.BackFace.StencilFunc         = D3D11_COMPARISON_ALWAYS;

	HR(device->CreateDepthStencilState(&stencilOnlyDesc, &mComparisonAlwaysDSS));

	// DEPTH STENCIL STATE: FOR RENDERING THE DEPTH COMPLEXITY 
	D3D11_DEPTH_STENCIL_DESC stencilEqualsDesc;
	stencilEqualsDesc.DepthEnable      = true;
	stencilEqualsDesc.DepthWriteMask   = D3D11_DEPTH_WRITE_MASK_ZERO;
    	stencilEqualsDesc.DepthFunc        = D3D11_COMPARISON_EQUAL; 
    	stencilEqualsDesc.StencilEnable    = true;
    	stencilEqualsDesc.StencilReadMask  = 0xff;
    	stencilEqualsDesc.StencilWriteMask = 0xff;

    	stencilEqualsDesc.FrontFace.StencilFailOp      = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.FrontFace.StencilPassOp      = D3D11_STENCIL_OP_KEEP;		// no changes needed
	stencilEqualsDesc.FrontFace.StencilFunc        = D3D11_COMPARISON_EQUAL;	// only the matching ones

		// these don't matter, because rendering only frontfacing polys
    	stencilEqualsDesc.BackFace.StencilFailOp       = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.BackFace.StencilDepthFailOp  = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.BackFace.StencilPassOp       = D3D11_STENCIL_OP_REPLACE;
	stencilEqualsDesc.BackFace.StencilFunc         = D3D11_COMPARISON_ALWAYS;

	HR(device->CreateDepthStencilState(&stencilEqualsDesc, &mComparisonEqualsDSS));

// the rendering part
void BlendApp::DrawScene()
{
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};

	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Black));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	md3dImmediateContext->IASetInputLayout(InputLayouts::mBasic32);
    	md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	//DrawSceneNormal();

	/**** 1: RENDER THE SCENE FIRST ONLY TO THE STENCIL BUFFER *****/
	md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonAlwaysDSS, 0);
	md3dImmediateContext->OMSetBlendState(RenderStates::mNoRenderTargetWriteBS, blendFactor, 0xffffffff);

	DrawSceneToStencil();
	HR(mSwapChain->Present(0, 0));

	/**** 2: RENDER SCENE TO RENDER TARGET USING STENCIL D3DOP_COMPARISON_EQUAL ****/
	/**** ONE QUAD PER DEPTH COMPLEXITY, DIFFERENT COLORS *****/
	md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonEqualsDSS, 0);
	md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);

	DrawSceneDepthComplexity();

	HR(mSwapChain->Present(0, 0));
}

// render only to the stencil buffer
void BlendApp::DrawSceneToStencil()
{
	XMMATRIX view  = XMLoadFloat4x4(&mMatView);
	XMMATRIX proj  = XMLoadFloat4x4(&mMatProj);
	XMMATRIX viewProj = view*proj;

	// per frame constants - update
	Effects::mBasicFX->SetDirLights(mDirLights);
	Effects::mBasicFX->SetEyePosW(mEyePosW);
	Effects::mBasicFX->SetFogColor(Colors::Silver);
	Effects::mBasicFX->SetFogStart(15.0f);
	Effects::mBasicFX->SetFogRange(175.0f);
	
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};

	UINT stride = sizeof(Vertex::Basic32);
    	UINT offset = 0;
	
	ID3DX11EffectTechnique* boxTech = Effects::mBasicFX->mLight3TexAlphaClipTech;
	ID3DX11EffectTechnique* landAndWavesTech = Effects::mBasicFX->mLight3TexTech;

	D3DX11_TECHNIQUE_DESC techDesc;

	// draw box with alpha clipping
	boxTech->GetDesc(&techDesc);
	for(UINT p=0;p<techDesc.Passes;++p)
    	{
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mBoxIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// per object constants - update
		XMMATRIX world = XMLoadFloat4x4(&mMatWorldBox);
		XMMATRIX worldInvTranspose = CMathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world*view*proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMMatrixIdentity());
		Effects::mBasicFX->SetMaterial(mMatBox);
		Effects::mBasicFX->SetDiffuseMap(mBoxMapSRV);

		// render state: no culling (inside and outside!)
		md3dImmediateContext->RSSetState(RenderStates::mNoCullRS);
		boxTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(36, 0, 0);

		// restore default render state
		md3dImmediateContext->RSSetState(0);
	}

	// draw hills and waves (no alpha clipping)
	landAndWavesTech->GetDesc(&techDesc);
    	for(UINT p=0;p<techDesc.Passes;++p)
    	{
		// hills
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mLandVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mLandIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// per object constants - update
		XMMATRIX world = XMLoadFloat4x4(&mMatWorldLand);
		XMMATRIX worldInvTranspose = CMathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world*view*proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMLoadFloat4x4(&mGrassTexTransform));
		Effects::mBasicFX->SetMaterial(mMatLand);
		Effects::mBasicFX->SetDiffuseMap(mGrassMapSRV);

		landAndWavesTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(mLandIndexCount, 0, 0);

		// waves
		md3dImmediateContext->IASetVertexBuffers(0, 1, &mWavesVtxBuffer, &stride, &offset);
		md3dImmediateContext->IASetIndexBuffer(mWavesIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

		// per object constants - update
		world = XMLoadFloat4x4(&mMatWorldWaves);
		worldInvTranspose = CMathHelper::InverseTranspose(world);
		worldViewProj = world*view*proj;
		
		Effects::mBasicFX->SetWorld(world);
		Effects::mBasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::mBasicFX->SetWorldViewProj(worldViewProj);
		Effects::mBasicFX->SetTexTransform(XMLoadFloat4x4(&mWaterTexTransform));
		Effects::mBasicFX->SetMaterial(mMatWaves);
		Effects::mBasicFX->SetDiffuseMap(mWavesMapSRV);

		// blending state: transparent	=> no render target writing
		md3dImmediateContext->OMSetBlendState(RenderStates::mTransparentNoWriteBS, blendFactor, 0xffffffff);

		landAndWavesTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);
		md3dImmediateContext->DrawIndexed(3*mWaves.TriangleCount(), 0, 0);

		// restore default blending state
		md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);

		// RESTORE STATES
		md3dImmediateContext->OMSetDepthStencilState(0, 0);	
		md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);
	}

}

?

void BlendApp::DrawSceneDepthComplexity()
{
	// defining the colors for visualizing depth complexity
	XMVECTORF32 Colors[5]	=	{1.0f, 0.0f, 0.0f, 1.0f},		// RED
								{0.0f, 1.0f, 0.0f, 1.0f},		// GREEN
								{0.0f, 0.0f, 1.0f, 1.0f},		// BLUE
								{1.0f, 1.0f, 0.0f, 1.0f},		// YELLOW
								{0.0f, 1.0f, 1.0f, 1.0f};		// CYAN

	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Black));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	md3dImmediateContext->IASetInputLayout(InputLayouts::mBasic32);
    md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	for(int i=0;i<5;++i)
	{
		md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonAlwaysDSS, i);
		
		// draw the quad using stencil buffer data?

	}
}

Any input is appreciated.

The end result should be something like this:

Fig10-13.jpg

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement

Yes, looping over all colors/values, incrementing the stencil reference value and using a simple pixel shader where you can set the output color.

 

It should be noted that with the appropriate formats one can sample the stencil like a texture directly  (e.g. R24G8_TYPELESS for depth/stencil texture, D24_UNORM_S8_UINT for the depth stencil view and X24_TYPELESS_G8_UINT for the stencil shader resource view), so this can be done drawing one full-screen quad/triangle only.

Share this post


Link to post
Share on other sites

Thanks. I think I understand what you mean, but don't understand how I would achieve that.

I've updated the thread with how far I've gotten.

 

I can create a new technique calling a new pixel shader that will render a pixel to a specified color, which I can change with a extern color variable in the CBuffer (shader constant). Do I have to create an 'object' with 4 vertices to draw the fullscreen quad, or is there a better/ easier way for this purpose?

cbuffer cbPerColor
{
	float4	gColor;
}; 

float4 PScolorOnly(VertexOut pin) : SV_Target
{
	float4 Color = gColor;
    return Color;
}


technique11 ColorSpecified
{
    pass P0
    {
        SetVertexShader(CompileShader( vs_5_0, VS()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, PScolorOnly()));
    }
}


Edited by cozzie

Share this post


Link to post
Share on other sites
You can, but for full-screen drawing (post-process and the like) a vertex shader generated triangle does the job (page 14 of Vertex Shader Tricks). No buffers, no input layout needed. (Edit: You wouldn't even need tex-coords in this case).
 

//draw the quad using stencil buffer data?

 
Not sure if there's a misunderstanding. You don't use the data directly. Pixels will only get drawn where the stencil value is equal to the reference value. By the way: You want to use mComparisonEqualsDSS now. Also: It's probably better to disable depth test and write for that DSS completely. Edited by unbird

Share this post


Link to post
Share on other sites

Thanks, I think I'm getting there.

Things done:

- technique and pixel shader created and tested individually (using DrawSceneNormal), that works fine

- changed the DSS as you said, disabled the depth test/write there

- nearly there with the DrawSceneDepthComplexity function

	// DEPTH STENCIL STATE: FOR RENDERING THE DEPTH COMPLEXITY 
	D3D11_DEPTH_STENCIL_DESC stencilEqualsDesc;
	stencilEqualsDesc.DepthEnable      = false;
	stencilEqualsDesc.DepthWriteMask   = D3D11_DEPTH_WRITE_MASK_ZERO;
    stencilEqualsDesc.DepthFunc        = D3D11_COMPARISON_EQUAL; 
    stencilEqualsDesc.StencilEnable    = true;
    stencilEqualsDesc.StencilReadMask  = 0xff;
    stencilEqualsDesc.StencilWriteMask = 0xff;

    stencilEqualsDesc.FrontFace.StencilFailOp      = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.FrontFace.StencilPassOp      = D3D11_STENCIL_OP_KEEP;		// no changes needed
	stencilEqualsDesc.FrontFace.StencilFunc        = D3D11_COMPARISON_EQUAL;		// only the matching ones

		// these don't matter, because rendering only frontfacing polys
    stencilEqualsDesc.BackFace.StencilFailOp       = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.BackFace.StencilDepthFailOp  = D3D11_STENCIL_OP_KEEP;
	stencilEqualsDesc.BackFace.StencilPassOp       = D3D11_STENCIL_OP_REPLACE;
	stencilEqualsDesc.BackFace.StencilFunc         = D3D11_COMPARISON_ALWAYS;

	HR(device->CreateDepthStencilState(&stencilEqualsDesc, &mComparisonEqualsDSS));

void BlendApp::DrawSceneDepthComplexity()
{
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
	md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonEqualsDSS, 0);
	md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);

	// defining the colors for visualizing depth complexity
	XMVECTORF32 Colors[5]	=	{(1.0f, 0.0f, 0.0f, 1.0f),		// RED
								 (0.0f, 1.0f, 0.0f, 1.0f),		// GREEN
								 (0.0f, 0.0f, 1.0f, 1.0f),		// BLUE
								 (1.0f, 1.0f, 0.0f, 1.0f),		// YELLOW
								 (0.0f, 1.0f, 1.0f, 1.0f)};		// CYAN

	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Black));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);			// DON'T CLEAR THE STENCIL BUFFER

	md3dImmediateContext->IASetInputLayout(InputLayouts::mBasic32);
    md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	ID3DX11EffectTechnique* quadTech = Effects::mBasicFX->mColorOnlyTech;
	D3DX11_TECHNIQUE_DESC techDesc;

    for(UINT p=0;p<techDesc.Passes;++p)
	{
		for(int i=0;i<5;++i)
		{
			md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonAlwaysDSS, i);
	
			XMVECTORF32 tstColor =	Colors[i];
			Effects::mBasicFX->SetQuadColor(tstColor);


			quadTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);

			// draw the quad using stencil buffer data?
			// ....
		}
	}
}

The 'only' thing I don't understand yet I have to actually 'draw'

Share this post


Link to post
Share on other sites
Use that vertex shader I linked to and call md3dImmediateContext->Draw(3,0). Again: No vertex/index buffers bound to the input assembler stage (explicitly set them to null), set input layout to null and topology to triangle list. (Code is on page 13 by the way). Edited by unbird

Share this post


Link to post
Share on other sites
Got it. I'll post the result tomorrow night. Now some sleep and work tomorrow.
Thanks again for taking the time.

Share this post


Link to post
Share on other sites

Just a short update, after introducing the new VS.

- I understand the principle and approach

- new VS added and technique changed to use the new VS

-- two things not solved yet

 

1. the new VS needs a "uint id: SV_VERTEXID" as input, what should I pass as parameter in the technique?

(I tried '0', but that didn't work out/ compile)

2. my currect vertex output structure has a float3 for position (world pos) and has no color component.

So I've removed the . w value from the VS and the assignment of the color. Is this OK or should I make a new definition for the VS output for this specific case?

 

Here's the code so far.

Rendering the depth complexity:

void BlendApp::DrawSceneDepthComplexity()
{
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};
	md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonEqualsDSS, 0);
	md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);

	// defining the colors for visualizing depth complexity
	XMVECTORF32 Colors[5]	=	{(1.0f, 0.0f, 0.0f, 1.0f),		// RED
								 (0.0f, 1.0f, 0.0f, 1.0f),		// GREEN
								 (0.0f, 0.0f, 1.0f, 1.0f),		// BLUE
								 (1.0f, 1.0f, 0.0f, 1.0f),		// YELLOW
								 (0.0f, 1.0f, 1.0f, 1.0f)};		// CYAN

	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Black));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);			// DON'T CLEAR THE STENCIL BUFFER

	md3dImmediateContext->IASetInputLayout(NULL);
    md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	ID3DX11EffectTechnique* quadTech = Effects::mBasicFX->mColorOnlyTech;
	D3DX11_TECHNIQUE_DESC techDesc;

    for(UINT p=0;p<techDesc.Passes;++p)
	{
		for(int i=0;i<5;++i)
		{
			md3dImmediateContext->IASetVertexBuffers(0, 0, NULL, NULL, NULL);
			md3dImmediateContext->IASetIndexBuffer(NULL, (DXGI_FORMAT)0, 0);		
			
			md3dImmediateContext->OMSetDepthStencilState(RenderStates::mComparisonAlwaysDSS, i);
	
			XMVECTORF32 tstColor =	Colors[i];
			Effects::mBasicFX->SetQuadColor(tstColor);

			quadTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);

			md3dImmediateContext->Draw(3, 0);
		}
	}
}

The VS, PS and technique (with now the 2 remaining issues described above):

VertexOut VSfullscreenQuad(uint id:SV_VERTEXID)
{
	VertexOut output;
	
	// generate clip space position
	output.PosW.x = (float)(id / 2) * 4.0 - 1.0f;
	output.PosW.x = (float)(id % 2) * 4.0 - 1.0f;
	output.PosW.z = 0.0f;
	//output.PosW.w = 1.0f;

	// texture coordinates
	output.Tex.x = (float)(id / 2) * 2.0f;
	output.Tex.x = 1.0f - (float)(id % 2) * 2.0f;
	
	// color
	//output.color = float4(1, 1, 1, 1);

	return output;
}

float4 PScolorOnly(VertexOut pin) : SV_Target
{
	float4 Color = gColor;
    return Color;
}


technique11 ColorSpecified
{
    pass P0
    {
        SetVertexShader(CompileShader(vs_5_0, VSfullscreenQuad()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, PScolorOnly()));
    }
}

Any thoughts?

Share this post


Link to post
Share on other sites


Just a short update, after introducing the new VS.
- I understand the principle and approach
- new VS added and technique changed to use the new VS
-- two things not solved yet
 
1. the new VS needs a "uint id: SV_VERTEXID" as input, what should I pass as parameter in the technique?
(I tried '0', but that didn't work out/ compile)
2. my currect vertex output structure has a float3 for position (world pos) and has no color component.
So I've removed the . w value from the VS and the assignment of the color. Is this OK or should I make a new definition for the VS output for this specific case?

 

1. You don't pass in an argument for the id parameter, it just comes to you from the gpu like magic.

2. SV_Position is always a float4. The most basic way to do this is to leave z as 0 and w as 1. The gpu is going to divide xyz by w, and if w is 1 then it's not doing anything.

Share this post


Link to post
Share on other sites

Update: stupid, I was doing .X twice, instead of .X and .Y (in the VS), post updated.

 

Thanks for the quick response, I think I've solved problem 2.

Problem one though I didn't manage yet, thanks to your answer I now know I don't have to pass anything when calling the VS.

 

Result so far:

struct VSout
{
	float4 Pos     : SV_POSITION;
	float2 Tex     : TEXCOORD;
};


VSout VSfullscreenQuad(uint id:SV_VERTEXID)
{
	VSout output;
	
	// generate clip space position
	output.Pos.x = (float)(id / 2) * 4.0 - 1.0f;
	output.Pos.y = (float)(id % 2) * 4.0 - 1.0f;
	output.Pos.z = 0.0f;
	output.Pos.w = 1.0f;

	// texture coordinates
	output.Tex.x = (float)(id / 2) * 2.0f;
	output.Tex.y = 1.0f - (float)(id % 2) * 2.0f;
	
	// color
	//output.color = float4(1, 1, 1, 1);

	return output;
}

float4 PScolorOnly(VSout pin) : SV_Target
{
	float4 Color = gColor;
    return Color;
}


technique11 ColorSpecified
{
    pass P0
    {
        SetVertexShader(CompileShader(vs_5_0, VSfullscreenQuad()));
		SetGeometryShader( NULL );
        SetPixelShader(CompileShader(ps_5_0, PScolorOnly()));
    }
}

No errors and warnings remaining in the compilation of the PS/ VS/ technique.

 

I don't see visible result yet, but the program runs, now I'll have to dig and find what I'm doing wrong.

Edited by cozzie

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!