Jump to content
  • Advertisement
Sign in to follow this  
Funkymunky

DX12 DX12 SkyBox

This topic is 1065 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

In a previous iteration of my code, I would have done the following

  • Clear Color
  • Render SkyBox
  • Clear Depth
  • Render Scene

But it strikes me that with DX12, I could just create a PipelineState that disables the depth write.  First of all, is that a better way to do things now?  And second, I get a crash on CreateGraphicsPipelineState when trying to use a shader with 0 descriptor heaps (it's a basic shader that has 3 floats for position.  No textures, buffers, samplers at the moment.  It gives me E_INVALIDARGS:

	ComPtr<ID3DBlob> signature, error;
	CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc = CD3DX12_ROOT_SIGNATURE_DESC(D3D12_DEFAULT);
	ThrowIfFailed(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
	ThrowIfFailed(device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature)));

	const D3D12_INPUT_ELEMENT_DESC VertexElements[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
	};
	D3D12_INPUT_LAYOUT_DESC inputLayoutDesc;
	inputLayoutDesc.pInputElementDescs = VertexElements;
	inputLayoutDesc.NumElements = _countof(VertexElements);

	D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
	psoDesc.VS = { reinterpret_cast<UINT8*>(vsBlob->GetBufferPointer()), vsBlob->GetBufferSize() };
	psoDesc.PS = { reinterpret_cast<UINT8*>(psBlob->GetBufferPointer()), psBlob->GetBufferSize() };
	psoDesc.InputLayout = inputLayoutDesc;
	psoDesc.pRootSignature = rootSignature.Get();
	psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
	psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
	psoDesc.DepthStencilState.DepthEnable = FALSE;
	psoDesc.DepthStencilState.StencilEnable = FALSE;
	psoDesc.SampleMask = UINT_MAX;
	psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
	psoDesc.NumRenderTargets = 1;
	psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
	psoDesc.SampleDesc.Count = 1;
	ThrowIfFailed(device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pipelineState)));

Share this post


Link to post
Share on other sites
Advertisement

Okay, but I'd still like to know:

 

  • Is it expensive to switch pipeline states?
  • Is there any obvious reason why my call to CreateGraphicsPipelineState crashes with E_INVALIDARGS?
  • How are you proposing that I should render the sky?
Edited by Funkymunky

Share this post


Link to post
Share on other sites
&nbsp;

<p>

Okay, but I'd still like to know:
&nbsp;

  • Is it expensive to switch pipeline states?
  • Is there any obvious reason why my call to CreateGraphicsPipelineState crashes with E_INVALIDARGS?
  • How are you proposing that I should render the sky?

1 - Expensive is a "relative" term, it's more expensive than not switching them, so you should avoid doing it if you can without adding too much extra cost to do that. On the other hand setting states and drawing things is much more efficient with d3d12 compared to d3d11 so you have more of a margin there before hitting hard limits. If objects share the same pipeline states try drawing them together if you can. But you do not have to go out of your way to merge different pipeline states by artificial means (not unless you find that is becoming a problem).

2 - I'd suggest turning on the debug runtime.

3 - Try to render the sky as the last opaque thing. (you could also use a Z only pass). The culling of pixels (not rendering unnecessary pixels) is more efficient if opaque objects are rendered front to back (in a rough order but you know that the sky is always the background so it's not complicated to find out when to draw it). The z-only pass helps if you can't for some reason guarantee a very good front to back order. But it can be more expensive if you don't gain any more pixel culling after it (it's two passes instead of one).

We realize that some advice can appear contradictory. It's because you always have to make trade-offs. The preferred order for X is different than the perfect order for Y. It's why you always profile (if possible with your final load) and before you can profile you build things that are more flexible and/or with good guesstimates. See for example this benchmark : http://www.anandtech.com/show/9112/exploring-dx12-3dmark-api-overhead-feature-test/3 it shows that the engine can render 300k draw calls per frame. Grouping one more draw call the wrong way is not necessarily going to affect performance that much (especially if you are far from the theoretical limit). On the other hand if your sky is expensive to draw per pixel then drawing it after everything makes sense.

Share this post


Link to post
Share on other sites
If your problem is to "how in practice" draw the skybox as the last thing (without it intersecting other geometry) you probably have many solutions to choose from.


You can choose some range to draw the main scene and some other range to draw the skybox. You can modify your vertex shader to make sure your skybox always end up near the max z value (not exactly the max z-value as that could result in z-fighting and clipping on some hardware). You can also render your skybox as a quad near the max z value (a cube has a varying depth unless you use previous tip, the quad can be drawn at constant depth) and use texture coordinates to point to the right texel in the cubemap and so on.


If you're unsure on how to modify your vertex shader, remember that the W value that you write determines the proper position on the screen (perspective correction and X/W Y/W screen coordinates after a conic projection) and that the Z/W value is what ends up being written in the depth buffer and/or tested against the depth buffer.

Share this post


Link to post
Share on other sites

I haven't done any D3D 12 yet but I see no reason why my usual advice for a skybox won't work - set a depth range (minz/maxz in your viewport) of 1,1, draw it last and draw it as a small cube (how small depends on znear in your projection) centered on the viewpoint.  The depth range will ensure that the skybox is always behind other geometry, drawing it as a small cube will ensure that you don't need to worry about positioning and sizing relative to other geometry and it will never z-fight, and the two work together to give the desired result.  If you can, use a cubemap for the skybox texture rather than 6 individual textures.

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!