From cf9d940beb313f9933abaf76747a3c4cbe0a306a Mon Sep 17 00:00:00 2001 From: REDDRAGON Date: Thu, 15 Mar 2018 18:17:19 +0100 Subject: [PATCH] Added forced sample count (MSAA sample pattern) for voxelization RS --- MAGE/Rendering/src/direct3d11.hpp | 2 +- .../src/renderer/pass/voxelization_pass.cpp | 29 +++++++++++++++++-- .../src/renderer/pass/voxelization_pass.hpp | 7 +++++ MAGE/Rendering/src/rendering_manager.cpp | 6 ++-- .../shaders/voxelization/voxelization_GS.hlsl | 28 ++++-------------- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/MAGE/Rendering/src/direct3d11.hpp b/MAGE/Rendering/src/direct3d11.hpp index 621ca012d..d0ed72a0d 100644 --- a/MAGE/Rendering/src/direct3d11.hpp +++ b/MAGE/Rendering/src/direct3d11.hpp @@ -14,7 +14,7 @@ //----------------------------------------------------------------------------- #pragma region -#include +#include #include #pragma endregion diff --git a/MAGE/Rendering/src/renderer/pass/voxelization_pass.cpp b/MAGE/Rendering/src/renderer/pass/voxelization_pass.cpp index aa11bfc65..c41249b75 100644 --- a/MAGE/Rendering/src/renderer/pass/voxelization_pass.cpp +++ b/MAGE/Rendering/src/renderer/pass/voxelization_pass.cpp @@ -24,10 +24,14 @@ namespace mage::rendering { : m_device_context(device_context), m_state_manager(state_manager), m_resource_manager(resource_manager), + m_rs(), m_vs(CreateVoxelizationVS(resource_manager)), m_gs(CreateVoxelizationGS(resource_manager)), m_cs(CreateVoxelizationCS(resource_manager)), - m_voxel_grid(MakeUnique< VoxelGrid >(device, 1u)) {} + m_voxel_grid(MakeUnique< VoxelGrid >(device, 1u)) { + + SetupRasterizerState(device); + } VoxelizationPass::VoxelizationPass(VoxelizationPass&& pass) noexcept = default; @@ -36,6 +40,26 @@ namespace mage::rendering { VoxelizationPass& VoxelizationPass ::operator=(VoxelizationPass&& pass) noexcept = default; + void VoxelizationPass::SetupRasterizerState(ID3D11Device& device) { + D3D11_RASTERIZER_DESC2 desc = {}; + desc.CullMode = D3D11_CULL_NONE; + desc.FillMode = D3D11_FILL_SOLID; + desc.DepthClipEnable = TRUE; + desc.MultisampleEnable = TRUE; + desc.ForcedSampleCount = 8u; + + ComPtr< ID3D11Device > device0(&device); + ComPtr< ID3D11Device3 > device3; + { + // Get the ID3D11Device3. + const HRESULT result = device0.As(&device3); + ThrowIfFailed(result, + "ID3D11Device3 creation failed: %08X.", result); + } + + device3->CreateRasterizerState2(&desc, m_rs.ReleaseAndGetAddressOf()); + } + void VoxelizationPass::SetupVoxelGrid(size_t resolution) { if (m_voxel_grid->GetResolution() != resolution) { ComPtr< ID3D11Device > device; @@ -54,8 +78,7 @@ namespace mage::rendering { // GS: Bind the geometry shader. m_gs->BindShader(m_device_context); // RS: Bind the rasterization state. - m_state_manager.get().Bind(m_device_context, - RasterizerStateID::NoCulling); + Pipeline::RS::BindState(m_device_context, m_rs.Get()); // OM: Bind the depth-stencil state. m_state_manager.get().Bind(m_device_context, DepthStencilStateID::DepthNone); diff --git a/MAGE/Rendering/src/renderer/pass/voxelization_pass.hpp b/MAGE/Rendering/src/renderer/pass/voxelization_pass.hpp index 2d71a7536..f88af1c4c 100644 --- a/MAGE/Rendering/src/renderer/pass/voxelization_pass.hpp +++ b/MAGE/Rendering/src/renderer/pass/voxelization_pass.hpp @@ -121,6 +121,8 @@ namespace mage::rendering { // Member Methods //--------------------------------------------------------------------- + void SetupRasterizerState(ID3D11Device& device); + /** Sets up the voxel grid of this voxelization pass. @@ -187,6 +189,11 @@ namespace mage::rendering { */ std::reference_wrapper< ResourceManager > m_resource_manager; + /** + A pointer to the rasterizer state of this voxelization pass. + */ + ComPtr< ID3D11RasterizerState2 > m_rs; + /** A pointer to the vertex shader of this voxelization pass. */ diff --git a/MAGE/Rendering/src/rendering_manager.cpp b/MAGE/Rendering/src/rendering_manager.cpp index 07f21b9c1..bdae879e0 100644 --- a/MAGE/Rendering/src/rendering_manager.cpp +++ b/MAGE/Rendering/src/rendering_manager.cpp @@ -359,13 +359,13 @@ namespace mage::rendering { // Get the D3D11Device. const HRESULT result = device.As(&m_device); ThrowIfFailed(result, - "D3D11Device creation failed: %08X.", result); + "D3D11Device creation failed: %08X.", result); } { // Get the D3D11DeviceContext. const HRESULT result = device_context.As(&m_device_context); - ThrowIfFailed(result, - "D3D11DeviceContext creation failed: %08X.", result); + ThrowIfFailed(result, + "D3D11DeviceContext creation failed: %08X.", result); } } diff --git a/MAGE/Shaders/shaders/voxelization/voxelization_GS.hlsl b/MAGE/Shaders/shaders/voxelization/voxelization_GS.hlsl index cb3e90a78..9adc2cfe7 100644 --- a/MAGE/Shaders/shaders/voxelization/voxelization_GS.hlsl +++ b/MAGE/Shaders/shaders/voxelization/voxelization_GS.hlsl @@ -47,8 +47,9 @@ void GS(triangle GSInputPositionNormalTexture input[3], break; } - // [m_view] * [voxels/m_view] -> [voxels] - output[i].p.xy *= g_voxel_inv_size; + // [m_view] * [voxels/m_view] -> [voxels] + // [voxels] * [2 m_ndc/voxels] -> [-1,1] + output[i].p.xy *= g_voxel_inv_size * 2.0f * g_voxel_grid_inv_resolution; #ifdef DISABLE_INVERTED_Z_BUFFER output[i].p.zw = float2(0.0f, 1.0f); #else // DISABLE_INVERTED_Z_BUFFER @@ -58,28 +59,11 @@ void GS(triangle GSInputPositionNormalTexture input[3], output[i].n_world = input[i].n_world; output[i].tex_material = input[i].tex_material; output[i].tex_geometry = input[i].tex_geometry; - } - - // For each projected triangle, a slightly larger bounding triangle ensures - // that any projected triangle touching a pixel will necessarily touch the - // center of this pixel and thus will get a fragment emitted by the rasterizer. - const float2 delta_10 = normalize(output[1].p.xy - output[0].p.xy); - const float2 delta_21 = normalize(output[2].p.xy - output[1].p.xy); - const float2 delta_02 = normalize(output[0].p.xy - output[2].p.xy); - // [voxels] * [2 m_ndc/voxels] -> [-1,1] - const float voxel_to_ndc = 2.0f * g_voxel_grid_inv_resolution; - // Move vertices for conservative rasterization. - output[0].p.xy = (output[0].p.xy + normalize(delta_02 - delta_10)) * voxel_to_ndc; - output[1].p.xy = (output[1].p.xy + normalize(delta_10 - delta_21)) * voxel_to_ndc; - output[2].p.xy = (output[2].p.xy + normalize(delta_21 - delta_02)) * voxel_to_ndc; - - // Output a triangle strip of three vertices. - [unroll] - for (uint j = 0u; j < 3u; ++j) { + // Output a vertex. - output_stream.Append(output[j]); + output_stream.Append(output[i]); } - + // End the current triangle strip. output_stream.RestartStrip(); } \ No newline at end of file