Skip to content

Commit

Permalink
Added forced sample count (MSAA sample pattern) for voxelization RS
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Mar 15, 2018
1 parent c497a78 commit cf9d940
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion MAGE/Rendering/src/direct3d11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//-----------------------------------------------------------------------------
#pragma region

#include <d3d11.h>
#include <d3d11_3.h>
#include <DXGI1_2.h>

#pragma endregion
Expand Down
29 changes: 26 additions & 3 deletions MAGE/Rendering/src/renderer/pass/voxelization_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions MAGE/Rendering/src/renderer/pass/voxelization_pass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ namespace mage::rendering {
// Member Methods
//---------------------------------------------------------------------

void SetupRasterizerState(ID3D11Device& device);

/**
Sets up the voxel grid of this voxelization pass.
Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 3 additions & 3 deletions MAGE/Rendering/src/rendering_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
28 changes: 6 additions & 22 deletions MAGE/Shaders/shaders/voxelization/voxelization_GS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

0 comments on commit cf9d940

Please sign in to comment.