From 186baceec1b5dfdfb0d3f289e222c869d565deb8 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Fri, 7 Jun 2019 22:42:34 +0200 Subject: [PATCH 01/12] [CHANGE] Adding transparency. Committing because I need to merge master into the branch --- resources/shaders/dxr_reflection_entries.hlsl | 56 +++++++ resources/shaders/dxr_shadow_entries.hlsl | 56 +++++++ src/d3d12/d3d12_acceleration_structure..cpp | 11 +- src/d3d12/d3d12_functions.hpp | 3 +- src/d3d12/d3d12_state_object.cpp | 21 ++- src/d3d12/d3d12_structs.hpp | 9 +- src/engine_registry.cpp | 9 +- src/imgui_graphics_settings.hpp | 7 + .../d3d12_build_acceleration_structures.hpp | 40 ++++- src/rt_pipeline_registry.hpp | 3 +- tests/demo/demo.cpp | 3 +- tests/demo/demo_frame_graphs.hpp | 2 +- tests/demo/scene_transparency.hpp | 150 ++++++++++++++++++ tests/demo/scene_viknell.hpp | 3 +- 14 files changed, 349 insertions(+), 24 deletions(-) create mode 100644 tests/demo/scene_transparency.hpp diff --git a/resources/shaders/dxr_reflection_entries.hlsl b/resources/shaders/dxr_reflection_entries.hlsl index 2c88a07a..0f92865b 100644 --- a/resources/shaders/dxr_reflection_entries.hlsl +++ b/resources/shaders/dxr_reflection_entries.hlsl @@ -142,4 +142,60 @@ void ReflectionMiss(inout ReflectionHitInfo payload) payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0); } +[shader("anyhit")] +void ReflectionAnyHit(inout ReflectionHitInfo payload, in Attributes attr) +{ +#ifndef FALLBACK + // Calculate the essentials + const Offset offset = g_offsets[InstanceID()]; + const Material material = g_materials[offset.material_idx]; + const float index_offset = offset.idx_offset; + const float vertex_offset = offset.vertex_offset; + + // Find first index location + const uint index_size = 4; + const uint indices_per_triangle = 3; + const uint triangle_idx_stride = indices_per_triangle * index_size; + + uint base_idx = PrimitiveIndex() * triangle_idx_stride; + base_idx += index_offset * 4; // offset the start + + uint3 indices = Load3x32BitIndices(g_indices, base_idx); + indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start + + // Gather triangle vertices + const Vertex v0 = g_vertices[indices.x]; + const Vertex v1 = g_vertices[indices.y]; + const Vertex v2 = g_vertices[indices.z]; + + //Get data from VBO + float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; + uv.y = 1.0f - uv.y; + + OutputMaterialData output_data = InterpretMaterialDataRT(material.data, + g_textures[material.albedo_id], + g_textures[material.normal_id], + g_textures[material.roughness_id], + g_textures[material.metalicness_id], + g_textures[material.emissive_id], + g_textures[material.ao_id], + 0, + s0, + uv); + + float alpha = output_data.alpha; + + if (alpha < 0.5f) + { + IgnoreHit(); + } + else + { + AcceptHitAndEndSearch(); + } +#else + payload.color = float3(0.0f, 0.0f, 0.0f); +#endif +} + #endif //__DXR_REFLECTION_ENTRIES_HLSL__ \ No newline at end of file diff --git a/resources/shaders/dxr_shadow_entries.hlsl b/resources/shaders/dxr_shadow_entries.hlsl index 4ca76efc..9963e7d7 100644 --- a/resources/shaders/dxr_shadow_entries.hlsl +++ b/resources/shaders/dxr_shadow_entries.hlsl @@ -21,4 +21,60 @@ void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) hit.is_hit = false; } +[shader("anyhit")] +void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) +{ +#ifndef FALLBACK + // Calculate the essentials + const Offset offset = g_offsets[InstanceID()]; + const Material material = g_materials[offset.material_idx]; + const float index_offset = offset.idx_offset; + const float vertex_offset = offset.vertex_offset; + + // Find first index location + const uint index_size = 4; + const uint indices_per_triangle = 3; + const uint triangle_idx_stride = indices_per_triangle * index_size; + + uint base_idx = PrimitiveIndex() * triangle_idx_stride; + base_idx += index_offset * 4; // offset the start + + uint3 indices = Load3x32BitIndices(g_indices, base_idx); + indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start + + // Gather triangle vertices + const Vertex v0 = g_vertices[indices.x]; + const Vertex v1 = g_vertices[indices.y]; + const Vertex v2 = g_vertices[indices.z]; + + //Get data from VBO + float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; + uv.y = 1.0f - uv.y; + + OutputMaterialData output_data = InterpretMaterialDataRT(material.data, + g_textures[material.albedo_id], + g_textures[material.normal_id], + g_textures[material.roughness_id], + g_textures[material.metalicness_id], + g_textures[material.emissive_id], + g_textures[material.ao_id], + 0, + s0, + uv); + + float alpha = output_data.alpha; + + if (alpha < 0.5f) + { + IgnoreHit(); + } + else + { + AcceptHitAndEndSearch(); + } +#else + hit.is_hit = false; +#endif +} + #endif //__DXR_SHADOW_ENTRIES_HLSL__ \ No newline at end of file diff --git a/src/d3d12/d3d12_acceleration_structure..cpp b/src/d3d12/d3d12_acceleration_structure..cpp index d1fc6258..1ddac003 100644 --- a/src/d3d12/d3d12_acceleration_structure..cpp +++ b/src/d3d12/d3d12_acceleration_structure..cpp @@ -238,7 +238,8 @@ namespace wr::d3d12 [[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, - std::vector geometry) + std::vector geometry, + bool allow_transparency) { AccelerationStructure blas = {}; @@ -266,7 +267,13 @@ namespace wr::d3d12 geometry_descs[i].Triangles.VertexCount = geom.m_num_vertices; geometry_descs[i].Triangles.VertexBuffer.StartAddress = geom.vertex_buffer->m_buffer->GetGPUVirtualAddress() + (geom.m_vertices_offset * geom.m_vertex_stride); geometry_descs[i].Triangles.VertexBuffer.StrideInBytes = geom.m_vertex_stride; - geometry_descs[i].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; + geometry_descs[i].Flags = allow_transparency ? D3D12_RAYTRACING_GEOMETRY_FLAG_NONE : D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; + + if (GetRaytracingType(device) == RaytracingType::FALLBACK) + { + geometry_descs[i].Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE; + } + } D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS build_flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE; diff --git a/src/d3d12/d3d12_functions.hpp b/src/d3d12/d3d12_functions.hpp index eb56744f..602f45a9 100644 --- a/src/d3d12/d3d12_functions.hpp +++ b/src/d3d12/d3d12_functions.hpp @@ -281,7 +281,8 @@ namespace wr::d3d12 [[nodiscard]] AccelerationStructure CreateBottomLevelAccelerationStructures(Device* device, CommandList* cmd_list, DescriptorHeap* desc_heap, - std::vector geometry); + std::vector geometry, + bool allow_transparency); [[nodiscard]] AccelerationStructure CreateTopLevelAccelerationStructure(Device* device, CommandList* cmd_list, diff --git a/src/d3d12/d3d12_state_object.cpp b/src/d3d12/d3d12_state_object.cpp index 32835432..54d54715 100644 --- a/src/d3d12/d3d12_state_object.cpp +++ b/src/d3d12/d3d12_state_object.cpp @@ -39,12 +39,25 @@ namespace wr::d3d12 // Hitgroup { - for (auto def : desc.m_hit_groups) + for (auto& def : desc.m_hit_groups) { + if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) + { + def.m_anyhit = std::nullopt; + } + auto hitGroup = n_desc.CreateSubobject(); - hitGroup->SetClosestHitShaderImport(def.second.c_str()); - hitGroup->SetHitGroupExport(def.first.c_str()); - hitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES); + + hitGroup->SetHitGroupExport(def.m_hitgroup.c_str()); + + hitGroup->SetClosestHitShaderImport(def.m_closesthit.c_str()); + + if (def.m_anyhit.has_value()) + { + hitGroup->SetAnyHitShaderImport(def.m_anyhit.value().c_str()); + } + + hitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES);; } } diff --git a/src/d3d12/d3d12_structs.hpp b/src/d3d12/d3d12_structs.hpp index 83bb9898..63d22302 100644 --- a/src/d3d12/d3d12_structs.hpp +++ b/src/d3d12/d3d12_structs.hpp @@ -110,11 +110,18 @@ namespace wr::d3d12 std::uint32_t m_vertex_stride = 0u; }; + struct HitGroupDesc + { + std::wstring m_hitgroup; + std::wstring m_closesthit; + std::optional m_anyhit; + }; + struct StateObjectDesc { Shader* m_library = nullptr; std::vector m_library_exports; - std::vector> m_hit_groups; // first = hit group | second = entry + std::vector m_hit_groups; // hitgroup, closesthit and anyhit std::uint32_t max_payload_size = 0u; std::uint32_t max_attributes_size = 0u; diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index a382ba9b..a1b7f04d 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -1105,7 +1105,8 @@ namespace wr lib.exports.push_back(L"ShadowRaygenEntry"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); - lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + lib.exports.push_back(L"ShadowAnyHitEntry"); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry", L"ShadowAnyHitEntry" }); return lib; }(); @@ -1135,10 +1136,12 @@ namespace wr lib.exports.push_back(L"ReflectionRaygenEntry"); lib.exports.push_back(L"ReflectionHit"); lib.exports.push_back(L"ReflectionMiss"); + lib.exports.push_back(L"ReflectionAnyHit"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); - lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit" }); - lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + lib.exports.push_back(L"ShadowAnyHitEntry"); + lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit", L"ReflectionAnyHit" }); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry", L"ShadowAnyHitEntry" }); return lib; }(); diff --git a/src/imgui_graphics_settings.hpp b/src/imgui_graphics_settings.hpp index 87248e4f..245046b5 100644 --- a/src/imgui_graphics_settings.hpp +++ b/src/imgui_graphics_settings.hpp @@ -84,6 +84,13 @@ namespace wr::imgui::window ImGui::Checkbox("Disable rebuilding", &as_build_user_settings.m_runtime.m_rebuild_as); + ImGui::Checkbox("Allow transparency", &as_build_user_settings.m_runtime.m_allow_transparency); + + if(ImGui::Button("Rebuild BLAS")) + { + as_build_user_settings.m_runtime.m_rebuild_bot_level = true; + } + ImGui::End(); frame_graph->UpdateSettings(as_build_user_settings); } diff --git a/src/render_tasks/d3d12_build_acceleration_structures.hpp b/src/render_tasks/d3d12_build_acceleration_structures.hpp index 20614d25..7e4dccb8 100644 --- a/src/render_tasks/d3d12_build_acceleration_structures.hpp +++ b/src/render_tasks/d3d12_build_acceleration_structures.hpp @@ -19,6 +19,8 @@ namespace wr struct Runtime { bool m_rebuild_as = false; + bool m_allow_transparency = false; + bool m_rebuild_bot_level = false; }; Runtime m_runtime; }; @@ -152,7 +154,7 @@ namespace wr return obj; } - inline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair mesh_material, ASBuildData& data, std::uint32_t frame_idx) + inline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair mesh_material, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) { auto n_model_pool = static_cast(model->m_model_pool); auto vb = n_model_pool->GetVertexStagingBuffer(); @@ -168,7 +170,7 @@ namespace wr d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH - auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }); + auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, settings.m_runtime.m_allow_transparency); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); @@ -181,7 +183,7 @@ namespace wr AppendOffset(data, n_mesh, material_id); } - inline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) + inline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_material_handles.clear(); @@ -228,7 +230,7 @@ namespace wr d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH - auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }); + auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, settings.m_runtime.m_allow_transparency); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); @@ -346,7 +348,7 @@ namespace wr return needs_reconstruction; }*/ - inline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) + inline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_offsets.clear(); @@ -398,7 +400,7 @@ namespace wr d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); - BuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, frame_idx); + BuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, settings, frame_idx); d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER); @@ -458,9 +460,31 @@ namespace wr d3d12::DescriptorHeap* out_heap = cmd_list->m_rt_descriptor_heap->GetHeap(); + if (settings.m_runtime.m_rebuild_bot_level) + { + data.out_init = true; + settings.m_runtime.m_rebuild_bot_level = false; + + fg.UpdateSettings(settings); + } + // Initialize requirements if (data.out_init) { + if (data.current_frame_index != data.previous_frame_index) + { + for (int i = 0; i < data.old_blasses[data.current_frame_index].size(); ++i) + { + d3d12::DestroyAccelerationStructure(data.old_blasses[data.current_frame_index][i]); + } + data.old_blasses[data.current_frame_index].clear(); + + d3d12::DestroyAccelerationStructure(data.old_tlas[data.current_frame_index]); + + data.old_tlas[data.current_frame_index] = {}; + } + + std::vector> model_pools = n_render_system.m_model_pools; // Transition all model pools for accel structure creation for (auto& pool : model_pools) @@ -470,7 +494,7 @@ namespace wr } // List all materials used by meshes - internal::BuildBLASList(device, cmd_list, scene_graph, data, frame_idx); + internal::BuildBLASList(device, cmd_list, scene_graph, data, settings, frame_idx); data.out_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, out_heap, data.out_blas_list); d3d12::SetName(data.out_tlas, L"Top Level Acceleration Structure"); @@ -504,7 +528,7 @@ namespace wr data.old_tlas[data.current_frame_index] = {}; } - internal::UpdateTLAS(device, cmd_list, scene_graph, data, frame_idx); + internal::UpdateTLAS(device, cmd_list, scene_graph, data, settings, frame_idx); } data.previous_frame_index = n_render_system.GetFrameIdx(); diff --git a/src/rt_pipeline_registry.hpp b/src/rt_pipeline_registry.hpp index dc34ee4e..82a3a6b7 100644 --- a/src/rt_pipeline_registry.hpp +++ b/src/rt_pipeline_registry.hpp @@ -8,6 +8,7 @@ #include "vertex.hpp" #include "d3d12/d3dx12.hpp" #include "d3d12/d3d12_enums.hpp" +#include "d3d12/d3d12_structs.hpp" #include "util/named_type.hpp" namespace wr @@ -21,7 +22,7 @@ namespace wr { RegistryHandle shader_handle; std::vector exports; - std::vector> m_hit_groups; // first = hit group | second = entry + std::vector m_hit_groups; }; CD3DX12_STATE_OBJECT_DESC desc; diff --git a/tests/demo/demo.cpp b/tests/demo/demo.cpp index a3b88e4a..4212ed8a 100644 --- a/tests/demo/demo.cpp +++ b/tests/demo/demo.cpp @@ -22,12 +22,13 @@ #include "scene_emibl.hpp" #include "scene_spheres.hpp" #include "scene_sun_temple.hpp" +#include "scene_transparency.hpp" #include "model_loader_assimp.hpp" #include "model_loader_tinygltf.hpp" #include "d3d12/d3d12_dynamic_descriptor_heap.hpp" -#define SCENE viknell_scene +#define SCENE transparency_scene std::unique_ptr render_system; std::shared_ptr scene_graph; diff --git a/tests/demo/demo_frame_graphs.hpp b/tests/demo/demo_frame_graphs.hpp index 5ae7536b..8c0e4f61 100644 --- a/tests/demo/demo_frame_graphs.hpp +++ b/tests/demo/demo_frame_graphs.hpp @@ -204,7 +204,7 @@ namespace fg_manager wr::AddShadowDenoiserTask(*fg); //Raytraced Ambient Occlusion task - wr::AddRTAOTask(*fg, static_cast(rs).m_device); + //wr::AddRTAOTask(*fg, static_cast(rs).m_device); wr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt); diff --git a/tests/demo/scene_transparency.hpp b/tests/demo/scene_transparency.hpp new file mode 100644 index 00000000..177178cd --- /dev/null +++ b/tests/demo/scene_transparency.hpp @@ -0,0 +1,150 @@ +#pragma once + +#include "wisp.hpp" +#include "window.hpp" +#include "scene_graph/scene_graph.hpp" +#include "imgui/imgui.hpp" +#include "debug_camera.hpp" +#include "spline_node.hpp" + +namespace transparency_scene +{ + namespace resources + { + ////////////////////////// + // RESOURCES + ////////////////////////// + + static std::shared_ptr model_pool; + static std::shared_ptr texture_pool; + static std::shared_ptr material_pool; + + static wr::Model* plane_model; + static wr::Model* test_model; + + static wr::MaterialHandle leaves_material; + static wr::MaterialHandle floor_material; + static wr::MaterialHandle bamboo_material; + + static wr::TextureHandle equirectangular_environment_map; + + void CreateResources(wr::RenderSystem* render_system) + { + texture_pool = render_system->CreateTexturePool(); + material_pool = render_system->CreateMaterialPool(256); + model_pool = render_system->CreateModelPool(64_mb, 64_mb); + + wr::TextureHandle leaf_albedo = texture_pool->LoadFromFile("resources/materials/alpha_leaf.png", true, true); + + equirectangular_environment_map = texture_pool->LoadFromFile("resources/materials/Circus_Backstage_3k.hdr", false, false); + + // Create Materials + leaves_material = material_pool->Create(texture_pool.get()); + wr::Material* leaves_internal = material_pool->GetMaterial(leaves_material); + + leaves_internal->SetTexture(wr::TextureType::ALBEDO, leaf_albedo); + leaves_internal->SetConstant(true); + leaves_internal->SetConstant(true); + leaves_internal->SetConstant(0.5f); + leaves_internal->SetConstant(0.5f); + + + floor_material = material_pool->Create(texture_pool.get()); + wr::Material* floor_internal = material_pool->GetMaterial(floor_material); + + floor_internal->SetConstant({1.0f, 1.0f, 0.0f}); + floor_internal->SetConstant(0.99f); + floor_internal->SetConstant(0.01f); + floor_internal->SetConstant(20.0f); + + plane_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/plane.fbx"); + test_model = model_pool->LoadWithMaterials(material_pool.get(), texture_pool.get(), "resources/models/xbot.fbx"); + } + + void ReleaseResources() + { + model_pool.reset(); + texture_pool.reset(); + material_pool.reset(); + }; + } + + + static std::shared_ptr camera; + static std::shared_ptr directional_light_node; + static std::vector> leaves; + static std::vector leaves_rots; + static std::vector leaves_rots_offsets; + static float t = 0; + + void CreateScene(wr::SceneGraph* scene_graph, wr::Window* window) + { + camera = scene_graph->CreateChild(nullptr, 90.f, (float)window->GetWidth() / (float)window->GetHeight()); + camera->SetPosition({ 0, 0, 2 }); + camera->SetSpeed(10); + + auto skybox = scene_graph->CreateChild(nullptr, resources::equirectangular_environment_map); + + auto floor = scene_graph->CreateChild(nullptr, resources::plane_model); + floor->SetMaterials({ resources::floor_material }); + floor->SetScale({ 50.0f, 50.0f, 1.0f }); + floor->SetRotation({90.0_deg, 0.0f, 0.0f}); + + // Geometry + for (size_t i = 0; i < 200; ++i) + { + auto leaf = scene_graph->CreateChild(nullptr, resources::plane_model); + leaf->SetMaterials({ resources::leaves_material }); + + float randomX = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + float randomY = static_cast (rand()) / (static_cast (RAND_MAX / 50.0f)); + float randomZ = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + + float xRandRot = static_cast (rand()) / (static_cast (RAND_MAX / 90.0f)); + float yRandRot = static_cast (rand()) / (static_cast (RAND_MAX / 90.0f)); + + leaf->SetPosition({randomX, randomY, randomZ}); + + leaves_rots.push_back({xRandRot, yRandRot, 0.0f}); + leaves.push_back(leaf); + } + + leaves_rots_offsets.resize(200); + + // Lights + auto point_light_0 = scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 1, 1, 1 }); + point_light_0->SetRotation({ -50.0_deg, 0.0f, 0.0f }); + } + + void UpdateScene(wr::SceneGraph* sg) + { + t += ImGui::GetIO().DeltaTime; + + for (size_t i = 0; i < 200; ++i) + { + auto pos = leaves[i]->m_position; + + if (DirectX::XMVectorGetY(pos) < -4.0f) + { + float randomX = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + float randomZ = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + + leaves[i]->SetPosition({ randomX , 50.0f, randomZ }); + } + else + { + leaves[i]->SetPosition(DirectX::XMVectorSubtract(pos, DirectX::XMVectorSet(0.0f, 0.001f * t, 0.0f, 0.0f))); + } + + leaves_rots_offsets[i] = DirectX::XMVectorAdd(leaves_rots_offsets[i], { 0.01f, 0.01f, 0.0f }); + + leaves[i]->SetRotation(DirectX::XMVectorAdd(leaves_rots[i], leaves_rots_offsets[i])); + } + + //auto pos = test_model->m_position; + //pos.m128_f32[0] = sin(t * 0.1) * 0.5; + //test_model->SetPosition(pos); + + camera->Update(ImGui::GetIO().DeltaTime); + } +} /* cube_scene */ diff --git a/tests/demo/scene_viknell.hpp b/tests/demo/scene_viknell.hpp index b5df7ba2..0401bcfe 100644 --- a/tests/demo/scene_viknell.hpp +++ b/tests/demo/scene_viknell.hpp @@ -1,4 +1,3 @@ - #pragma once #include "wisp.hpp" @@ -145,4 +144,4 @@ namespace viknell_scene camera->Update(ImGui::GetIO().DeltaTime); camera_spline_node->UpdateSplineNode(ImGui::GetIO().DeltaTime, camera); } -} /* cube_scene */ +} /* cube_scene */ \ No newline at end of file From 955b3c5d647655442391ac53e478d1ed6308bd80 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Fri, 7 Jun 2019 23:15:25 +0200 Subject: [PATCH 02/12] [CHANGE] Refactored AO shader a bit, adding RAY_FLAG_FORCE_OPAQUE fixes the crashes related to the absence of an any hit shader --- resources/shaders/dxr_ambient_occlusion.hlsl | 22 ++++++++++---------- src/engine_registry.cpp | 1 + src/imgui_graphics_settings.hpp | 3 ++- tests/demo/demo_frame_graphs.hpp | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/resources/shaders/dxr_ambient_occlusion.hlsl b/resources/shaders/dxr_ambient_occlusion.hlsl index e0733c86..b1377cd1 100644 --- a/resources/shaders/dxr_ambient_occlusion.hlsl +++ b/resources/shaders/dxr_ambient_occlusion.hlsl @@ -3,11 +3,14 @@ #include "rand_util.hlsl" #include "dxr_global.hlsl" +#include "dxr_functions.hlsl" RWTexture2D output : register(u0); // x: AO value Texture2D gbuffer_normal : register(t1); Texture2D gbuffer_depth : register(t2); +typedef BuiltInTriangleIntersectionAttributes Attributes; + struct AOHitInfo { float is_hit; @@ -29,15 +32,6 @@ cbuffer CBData : register(b0) unsigned int sample_count; }; -struct Attributes { }; - -float3 unpack_position(float2 uv, float depth) -{ - // Get world space position - const float4 ndc = float4(uv * 2.0 - 1.0, depth, 1.0); - float4 wpos = mul(inv_vp, ndc); - return (wpos.xyz / wpos.w).xyz; -} bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned int depth) { @@ -53,7 +47,7 @@ bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned i // Trace the ray TraceRay( Scene, - RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, + RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, ~0, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex @@ -78,7 +72,7 @@ void AORaygenEntry() float3 normal = gbuffer_normal[screen_co].xyz; float depth = gbuffer_depth[screen_co].x; - float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth); + float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp); float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); float cam_distance = length(wpos-camera_pos); @@ -106,5 +100,11 @@ void MissEntry(inout AOHitInfo hit : SV_RayPayload) { hit.is_hit = 0.0f; } +// +//[shader("anyhit")] +//void AnyHitEntry(inout AOHitInfo hit, in Attributes attr) +//{ +// AcceptHitAndEndSearch(); +//} #endif //__DXR_AMBIENT_OCCLUSION_HLSL__ \ No newline at end of file diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index a1b7f04d..5209fcc8 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -1014,6 +1014,7 @@ namespace wr lib.shader_handle = shaders::rt_ao_lib; lib.exports.push_back(L"AORaygenEntry"); lib.exports.push_back(L"MissEntry"); + //lib.exports.push_back(L"AnyHitEntry"); return lib; }(); diff --git a/src/imgui_graphics_settings.hpp b/src/imgui_graphics_settings.hpp index 245046b5..a07c234c 100644 --- a/src/imgui_graphics_settings.hpp +++ b/src/imgui_graphics_settings.hpp @@ -17,7 +17,6 @@ namespace wr::imgui::window static wr::RTAOSettings rtao_user_settings; static wr::HBAOSettings hbao_user_settings; static wr::AnselSettings ansel_user_settings; - static wr::ASBuildSettings as_build_user_settings; static wr::RTShadowSettings shadow_user_settings; void GraphicsSettings(wr::FrameGraph* frame_graph) @@ -80,6 +79,8 @@ namespace wr::imgui::window if (frame_graph->HasTask() && asbuild_settings_open) { + auto as_build_user_settings = frame_graph->GetSettings(); + ImGui::Begin("Acceleration Structure Settings", &asbuild_settings_open); ImGui::Checkbox("Disable rebuilding", &as_build_user_settings.m_runtime.m_rebuild_as); diff --git a/tests/demo/demo_frame_graphs.hpp b/tests/demo/demo_frame_graphs.hpp index 8c0e4f61..5ae7536b 100644 --- a/tests/demo/demo_frame_graphs.hpp +++ b/tests/demo/demo_frame_graphs.hpp @@ -204,7 +204,7 @@ namespace fg_manager wr::AddShadowDenoiserTask(*fg); //Raytraced Ambient Occlusion task - //wr::AddRTAOTask(*fg, static_cast(rs).m_device); + wr::AddRTAOTask(*fg, static_cast(rs).m_device); wr::AddDeferredCompositionTask(*fg, std::nullopt, std::nullopt); From bc2154e5e96acc7581b391d14b056e774680687c Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Wed, 12 Jun 2019 18:58:28 +0200 Subject: [PATCH 03/12] [CHANGE] Prototyped ray layers --- resources/shaders/dxr_shadow_entries.hlsl | 12 ++- resources/shaders/dxr_shadow_functions.hlsl | 14 ++-- resources/shaders/dxr_structs.hlsl | 2 +- tests/demo/scene_transparency.hpp | 89 +++++++++++++-------- 4 files changed, 71 insertions(+), 46 deletions(-) diff --git a/resources/shaders/dxr_shadow_entries.hlsl b/resources/shaders/dxr_shadow_entries.hlsl index 9963e7d7..39e4148f 100644 --- a/resources/shaders/dxr_shadow_entries.hlsl +++ b/resources/shaders/dxr_shadow_entries.hlsl @@ -64,14 +64,20 @@ void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) float alpha = output_data.alpha; - if (alpha < 0.5f) + if (alpha >= 0.5f) { - IgnoreHit(); + hit.ray_power -= (1.0f / 3.0f); } - else + + if (hit.ray_power < 0.01f) { + hit.ray_power = 0.1f; AcceptHitAndEndSearch(); } + else + { + IgnoreHit(); + } #else hit.is_hit = false; #endif diff --git a/resources/shaders/dxr_shadow_functions.hlsl b/resources/shaders/dxr_shadow_functions.hlsl index de060529..8ae7f3eb 100644 --- a/resources/shaders/dxr_shadow_functions.hlsl +++ b/resources/shaders/dxr_shadow_functions.hlsl @@ -4,7 +4,7 @@ #include "dxr_global.hlsl" #include "dxr_structs.hlsl" -bool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) +float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) { if (depth >= MAX_RECURSION) { @@ -18,7 +18,7 @@ bool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pas ray.TMin = 0; ray.TMax = far; - ShadowHitInfo payload = { false, 0 }; + ShadowHitInfo payload = { false, 1.0f }; uint ray_contr_idx = 1; uint miss_idx = 1; @@ -40,7 +40,7 @@ bool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pas ray, payload); - return payload.is_hit; + return payload.ray_power; } // Get shadow factor @@ -55,18 +55,18 @@ float GetShadowFactor(float3 wpos, float3 light_dir, float light_size, float t_m float3 dir = perturbDirectionVector(rand_seed, light_dir, light_size); float3 ray_direction = normalize(dir); - bool shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); + float shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); - shadow_factor += lerp(1.0, 0.0, shadow); + shadow_factor += shadow; } shadow_factor /= float(sample_count); #else //SOFT_SHADOWS - bool shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); + float shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); - shadow_factor = !shadow; + shadow_factor = shadow ; #endif //SOFT_SHADOWS diff --git a/resources/shaders/dxr_structs.hlsl b/resources/shaders/dxr_structs.hlsl index 19365d50..68adc9c4 100644 --- a/resources/shaders/dxr_structs.hlsl +++ b/resources/shaders/dxr_structs.hlsl @@ -76,7 +76,7 @@ struct ReflectionHitInfo struct ShadowHitInfo { float is_hit; - float thisvariablesomehowmakeshybridrenderingwork_killme; + float ray_power; }; struct PathTracingHitInfo diff --git a/tests/demo/scene_transparency.hpp b/tests/demo/scene_transparency.hpp index 177178cd..78bbb1a6 100644 --- a/tests/demo/scene_transparency.hpp +++ b/tests/demo/scene_transparency.hpp @@ -19,12 +19,12 @@ namespace transparency_scene static std::shared_ptr texture_pool; static std::shared_ptr material_pool; - static wr::Model* plane_model; + //static wr::Model* plane_model; static wr::Model* test_model; static wr::MaterialHandle leaves_material; - static wr::MaterialHandle floor_material; - static wr::MaterialHandle bamboo_material; + static wr::MaterialHandle checkboard_material; + static wr::MaterialHandle gray_material; static wr::TextureHandle equirectangular_environment_map; @@ -34,7 +34,8 @@ namespace transparency_scene material_pool = render_system->CreateMaterialPool(256); model_pool = render_system->CreateModelPool(64_mb, 64_mb); - wr::TextureHandle leaf_albedo = texture_pool->LoadFromFile("resources/materials/alpha_leaf.png", true, true); + wr::TextureHandle leaf_albedo = texture_pool->LoadFromFile("resources/models/plant/tropical_plant.png", true, true); + wr::TextureHandle checkboard = texture_pool->LoadFromFile("resources/materials/presenceMask_File.png", true, true); equirectangular_environment_map = texture_pool->LoadFromFile("resources/materials/Circus_Backstage_3k.hdr", false, false); @@ -45,20 +46,29 @@ namespace transparency_scene leaves_internal->SetTexture(wr::TextureType::ALBEDO, leaf_albedo); leaves_internal->SetConstant(true); leaves_internal->SetConstant(true); - leaves_internal->SetConstant(0.5f); - leaves_internal->SetConstant(0.5f); + leaves_internal->SetConstant(0.99f); + leaves_internal->SetConstant(0.01f); - floor_material = material_pool->Create(texture_pool.get()); - wr::Material* floor_internal = material_pool->GetMaterial(floor_material); + checkboard_material = material_pool->Create(texture_pool.get()); + wr::Material* checkboard_internal = material_pool->GetMaterial(checkboard_material); - floor_internal->SetConstant({1.0f, 1.0f, 0.0f}); - floor_internal->SetConstant(0.99f); - floor_internal->SetConstant(0.01f); - floor_internal->SetConstant(20.0f); + checkboard_internal->SetTexture(wr::TextureType::ALBEDO, checkboard); + checkboard_internal->SetConstant(true); + checkboard_internal->SetConstant(true); + checkboard_internal->SetConstant(0.99f); + checkboard_internal->SetConstant(0.01f); - plane_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/plane.fbx"); - test_model = model_pool->LoadWithMaterials(material_pool.get(), texture_pool.get(), "resources/models/xbot.fbx"); + //Gray mat + gray_material = material_pool->Create(texture_pool.get()); + wr::Material* gray_internal = material_pool->GetMaterial(gray_material); + + gray_internal->SetConstant({ 0.5f, 0.5f, 0.5f }); + gray_internal->SetConstant(0.99f); + gray_internal->SetConstant(0.01f); + + //plane_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/plane.fbx"); + test_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/plant/tropical_2.fbx"); } void ReleaseResources() @@ -75,6 +85,7 @@ namespace transparency_scene static std::vector> leaves; static std::vector leaves_rots; static std::vector leaves_rots_offsets; + static std::vector mats; static float t = 0; void CreateScene(wr::SceneGraph* scene_graph, wr::Window* window) @@ -84,14 +95,22 @@ namespace transparency_scene camera->SetSpeed(10); auto skybox = scene_graph->CreateChild(nullptr, resources::equirectangular_environment_map); + auto scene = scene_graph->CreateChild(nullptr, resources::test_model); + + mats.resize(3); + mats[0] = resources::gray_material; + mats[1] = resources::leaves_material; + mats[2] = resources::leaves_material; + + scene->SetMaterials(mats); - auto floor = scene_graph->CreateChild(nullptr, resources::plane_model); + /*auto floor = scene_graph->CreateChild(nullptr, resources::plane_model); floor->SetMaterials({ resources::floor_material }); floor->SetScale({ 50.0f, 50.0f, 1.0f }); - floor->SetRotation({90.0_deg, 0.0f, 0.0f}); + floor->SetRotation({90.0_deg, 0.0f, 0.0f});*/ // Geometry - for (size_t i = 0; i < 200; ++i) + /*for (size_t i = 0; i < 200; ++i) { auto leaf = scene_graph->CreateChild(nullptr, resources::plane_model); leaf->SetMaterials({ resources::leaves_material }); @@ -107,9 +126,9 @@ namespace transparency_scene leaves_rots.push_back({xRandRot, yRandRot, 0.0f}); leaves.push_back(leaf); - } + }*/ - leaves_rots_offsets.resize(200); + //leaves_rots_offsets.resize(200); // Lights auto point_light_0 = scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 1, 1, 1 }); @@ -120,26 +139,26 @@ namespace transparency_scene { t += ImGui::GetIO().DeltaTime; - for (size_t i = 0; i < 200; ++i) - { - auto pos = leaves[i]->m_position; + //for (size_t i = 0; i < 200; ++i) + //{ + // auto pos = leaves[i]->m_position; - if (DirectX::XMVectorGetY(pos) < -4.0f) - { - float randomX = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; - float randomZ = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + // if (DirectX::XMVectorGetY(pos) < -4.0f) + // { + // float randomX = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; + // float randomZ = (static_cast (rand()) / (static_cast (RAND_MAX / 50.0f))) - 25.0f; - leaves[i]->SetPosition({ randomX , 50.0f, randomZ }); - } - else - { - leaves[i]->SetPosition(DirectX::XMVectorSubtract(pos, DirectX::XMVectorSet(0.0f, 0.001f * t, 0.0f, 0.0f))); - } + // leaves[i]->SetPosition({ randomX , 50.0f, randomZ }); + // } + // else + // { + // leaves[i]->SetPosition(DirectX::XMVectorSubtract(pos, DirectX::XMVectorSet(0.0f, 0.001f * t, 0.0f, 0.0f))); + // } - leaves_rots_offsets[i] = DirectX::XMVectorAdd(leaves_rots_offsets[i], { 0.01f, 0.01f, 0.0f }); + // leaves_rots_offsets[i] = DirectX::XMVectorAdd(leaves_rots_offsets[i], { 0.01f, 0.01f, 0.0f }); - leaves[i]->SetRotation(DirectX::XMVectorAdd(leaves_rots[i], leaves_rots_offsets[i])); - } + // leaves[i]->SetRotation(DirectX::XMVectorAdd(leaves_rots[i], leaves_rots_offsets[i])); + //} //auto pos = test_model->m_position; //pos.m128_f32[0] = sin(t * 0.1) * 0.5; From 19e240e461cd7e13f5390bf9e3b37f3c733192a9 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Thu, 13 Jun 2019 11:10:38 +0200 Subject: [PATCH 04/12] [CHANGE] Pushing to switch branch --- resources/shaders/dxr_shadow_entries.hlsl | 34 +++++++++++++------ resources/shaders/dxr_shadow_functions.hlsl | 18 ++++++---- src/material_pool.hpp | 7 +++- .../d3d12_build_acceleration_structures.hpp | 2 +- tests/demo/demo_frame_graphs.hpp | 2 +- tests/demo/scene_transparency.hpp | 6 ++-- tests/demo/scene_viknell.hpp | 10 ++++-- 7 files changed, 55 insertions(+), 24 deletions(-) diff --git a/resources/shaders/dxr_shadow_entries.hlsl b/resources/shaders/dxr_shadow_entries.hlsl index 39e4148f..1f811180 100644 --- a/resources/shaders/dxr_shadow_entries.hlsl +++ b/resources/shaders/dxr_shadow_entries.hlsl @@ -62,22 +62,36 @@ void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) s0, uv); - float alpha = output_data.alpha; - - if (alpha >= 0.5f) + if (material.data.use_alpha_masking == 1.0f) { - hit.ray_power -= (1.0f / 3.0f); - } + float alpha = output_data.alpha; - if (hit.ray_power < 0.01f) - { - hit.ray_power = 0.1f; - AcceptHitAndEndSearch(); + if (alpha <= 0.5f) + { + hit.is_hit = false; + hit.ray_power = 0.0f; + IgnoreHit(); + } + else + { + hit.ray_power -= (1.0f / 3.0f); + + if (hit.ray_power < 0.01f) + { + hit.ray_power = 0.1f; + AcceptHitAndEndSearch(); + } + else + { + IgnoreHit(); + } + } } else { - IgnoreHit(); + AcceptHitAndEndSearch(); } + #else hit.is_hit = false; #endif diff --git a/resources/shaders/dxr_shadow_functions.hlsl b/resources/shaders/dxr_shadow_functions.hlsl index 8ae7f3eb..a33d9e1a 100644 --- a/resources/shaders/dxr_shadow_functions.hlsl +++ b/resources/shaders/dxr_shadow_functions.hlsl @@ -4,7 +4,7 @@ #include "dxr_global.hlsl" #include "dxr_structs.hlsl" -float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) +float2 TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) { if (depth >= MAX_RECURSION) { @@ -18,7 +18,7 @@ float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pa ray.TMin = 0; ray.TMax = far; - ShadowHitInfo payload = { false, 1.0f }; + ShadowHitInfo payload = { false, 0.0f }; uint ray_contr_idx = 1; uint miss_idx = 1; @@ -40,7 +40,7 @@ float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pa ray, payload); - return payload.ray_power; + return float2(payload.is_hit, payload.ray_power); } // Get shadow factor @@ -55,18 +55,22 @@ float GetShadowFactor(float3 wpos, float3 light_dir, float light_size, float t_m float3 dir = perturbDirectionVector(rand_seed, light_dir, light_size); float3 ray_direction = normalize(dir); - float shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); + float2 result = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); + float shadow = result.x; + float ray_power = result.y; - shadow_factor += shadow; + shadow_factor += lerp(1.0, ray_power, shadow); } shadow_factor /= float(sample_count); #else //SOFT_SHADOWS - float shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); + float2 result = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); + float shadow = result.x; + float ray_power = result.y; - shadow_factor = shadow ; + shadow_factor = lerp(1.0, ray_power, shadow); #endif //SOFT_SHADOWS diff --git a/src/material_pool.hpp b/src/material_pool.hpp index 9507b3a1..293430b7 100644 --- a/src/material_pool.hpp +++ b/src/material_pool.hpp @@ -94,6 +94,11 @@ namespace wr m_material_data.m_constant_data[uint32_t(type) >> 16] = x; } + template + void SetConstant(typename std::enable_if::type x) { + m_material_data.m_constant_data[uint32_t(type) >> 16] = x; + } + template void SetConstant(const typename std::enable_if>::type& val) { float* arr = m_material_data.m_constant_data; @@ -132,7 +137,7 @@ namespace wr float m_roughness; float m_emissive_multiplier; float m_is_double_sided; - float m_use_alpha_constant; + float m_use_alpha_masking; float albedo_scale; float normal_scale; diff --git a/src/render_tasks/d3d12_build_acceleration_structures.hpp b/src/render_tasks/d3d12_build_acceleration_structures.hpp index 7e4dccb8..d533285f 100644 --- a/src/render_tasks/d3d12_build_acceleration_structures.hpp +++ b/src/render_tasks/d3d12_build_acceleration_structures.hpp @@ -73,7 +73,7 @@ namespace wr // Structured buffer for the materials. data.out_sb_material_handle = static_cast(n_render_system.m_raytracing_material_sb_pool->Create(sizeof(temp::RayTracingMaterial_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingMaterial_CBData), false)); - // Structured buffer for the materials. + // Structured buffer for the offsets. data.out_sb_offset_handle = static_cast(n_render_system.m_raytracing_offset_sb_pool->Create(sizeof(temp::RayTracingOffset_CBData) * d3d12::settings::num_max_rt_materials, sizeof(temp::RayTracingOffset_CBData), false)); // Resize the materials diff --git a/tests/demo/demo_frame_graphs.hpp b/tests/demo/demo_frame_graphs.hpp index 5ae7536b..4e00d7ad 100644 --- a/tests/demo/demo_frame_graphs.hpp +++ b/tests/demo/demo_frame_graphs.hpp @@ -201,7 +201,7 @@ namespace fg_manager wr::AddRTReflectionTask(*fg); wr::AddRTShadowTask(*fg); - wr::AddShadowDenoiserTask(*fg); + //wr::AddShadowDenoiserTask(*fg); //Raytraced Ambient Occlusion task wr::AddRTAOTask(*fg, static_cast(rs).m_device); diff --git a/tests/demo/scene_transparency.hpp b/tests/demo/scene_transparency.hpp index 78bbb1a6..8c8823f7 100644 --- a/tests/demo/scene_transparency.hpp +++ b/tests/demo/scene_transparency.hpp @@ -97,6 +97,8 @@ namespace transparency_scene auto skybox = scene_graph->CreateChild(nullptr, resources::equirectangular_environment_map); auto scene = scene_graph->CreateChild(nullptr, resources::test_model); + scene->SetScale({ 0.1f, 0.1f, 0.1f }); + mats.resize(3); mats[0] = resources::gray_material; mats[1] = resources::leaves_material; @@ -131,8 +133,8 @@ namespace transparency_scene //leaves_rots_offsets.resize(200); // Lights - auto point_light_0 = scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 1, 1, 1 }); - point_light_0->SetRotation({ -50.0_deg, 0.0f, 0.0f }); + auto point_light_0 = scene_graph->CreateChild(nullptr, wr::LightType::DIRECTIONAL, DirectX::XMVECTOR{ 2000, 2000, 2000 }); + point_light_0->SetRotation({ 200.0_deg, 0.0f, 0.0f }); } void UpdateScene(wr::SceneGraph* sg) diff --git a/tests/demo/scene_viknell.hpp b/tests/demo/scene_viknell.hpp index 0401bcfe..484ce018 100644 --- a/tests/demo/scene_viknell.hpp +++ b/tests/demo/scene_viknell.hpp @@ -45,8 +45,8 @@ namespace viknell_scene mirror_material = material_pool->Create(texture_pool.get()); wr::Material* mirror_internal = material_pool->GetMaterial(mirror_material); - mirror_internal->SetConstant(0); - mirror_internal->SetConstant(1); + mirror_internal->SetConstant(0.0f); + mirror_internal->SetConstant(1.0f); mirror_internal->SetConstant({ 1, 1, 1 }); bamboo_material = material_pool->Create(texture_pool.get()); @@ -60,6 +60,12 @@ namespace viknell_scene plane_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/plane.fbx"); test_model = model_pool->LoadWithMaterials(material_pool.get(), texture_pool.get(), "resources/models/xbot.fbx"); sphere_model = model_pool->Load(material_pool.get(), texture_pool.get(), "resources/models/sphere.fbx"); + + auto mat_1 = test_model->m_meshes[0].second.m_pool->GetMaterial(test_model->m_meshes[0].second); + mat_1->SetConstant(false); + + auto mat_2 = test_model->m_meshes[1].second.m_pool->GetMaterial(test_model->m_meshes[1].second); + mat_2->SetConstant(false); } void ReleaseResources() From 2675503e9fdd09f6a9db7b613791c7a5f5e6f401 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Fri, 14 Jun 2019 13:04:45 +0200 Subject: [PATCH 05/12] [CHANGE] Added transparency to hybrid path tracer --- resources/shaders/dxr_pathtracer_entries.hlsl | 56 ++++++++ resources/shaders/dxr_raytracing.hlsl | 135 +++++++++++++++++- src/engine_registry.cpp | 44 +++++- src/engine_registry.hpp | 3 +- src/imgui_graphics_settings.hpp | 15 ++ src/render_tasks/d3d12_path_tracer.hpp | 28 +++- 6 files changed, 268 insertions(+), 13 deletions(-) diff --git a/resources/shaders/dxr_pathtracer_entries.hlsl b/resources/shaders/dxr_pathtracer_entries.hlsl index e6ef29cb..9fafa697 100644 --- a/resources/shaders/dxr_pathtracer_entries.hlsl +++ b/resources/shaders/dxr_pathtracer_entries.hlsl @@ -83,4 +83,60 @@ void ReflectionMiss(inout PathTracingHitInfoCone payload) payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb; } +[shader("anyhit")] +void ReflectionAnyHit(inout PathTracingHitInfoCone payload, in Attributes attr) +{ +#ifndef FALLBACK + // Calculate the essentials + const Offset offset = g_offsets[InstanceID()]; + const Material material = g_materials[offset.material_idx]; + const float index_offset = offset.idx_offset; + const float vertex_offset = offset.vertex_offset; + + // Find first index location + const uint index_size = 4; + const uint indices_per_triangle = 3; + const uint triangle_idx_stride = indices_per_triangle * index_size; + + uint base_idx = PrimitiveIndex() * triangle_idx_stride; + base_idx += index_offset * 4; // offset the start + + uint3 indices = Load3x32BitIndices(g_indices, base_idx); + indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start + + // Gather triangle vertices + const Vertex v0 = g_vertices[indices.x]; + const Vertex v1 = g_vertices[indices.y]; + const Vertex v2 = g_vertices[indices.z]; + + //Get data from VBO + float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; + uv.y = 1.0f - uv.y; + + OutputMaterialData output_data = InterpretMaterialDataRT(material.data, + g_textures[material.albedo_id], + g_textures[material.normal_id], + g_textures[material.roughness_id], + g_textures[material.metalicness_id], + g_textures[material.emissive_id], + g_textures[material.ao_id], + 0, + s0, + uv); + + float alpha = output_data.alpha; + + if (alpha < 0.5f) + { + IgnoreHit(); + } + else + { + AcceptHitAndEndSearch(); + } +#else + AcceptHitAndEndSearch(); +#endif +} + #endif //__DXR_PATHTRACER_ENTRIES_HLSL__ diff --git a/resources/shaders/dxr_raytracing.hlsl b/resources/shaders/dxr_raytracing.hlsl index 8e18228a..28effff6 100644 --- a/resources/shaders/dxr_raytracing.hlsl +++ b/resources/shaders/dxr_raytracing.hlsl @@ -29,7 +29,7 @@ Texture2D g_textures[1000] : register(t9); SamplerState s0 : register(s0); SamplerState point_sampler : register(s1); -typedef BuiltInTriangleIntersectionAttributes MyAttributes; +typedef BuiltInTriangleIntersectionAttributes Attributes; #include "dxr_pathtracer_functions.hlsl" @@ -145,8 +145,66 @@ void MissEntry(inout FullRTHitInfo payload) payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb; } +//[shader("anyhit")] +//void AnyHitEntry(inout FullRTHitInfo payload, in Attributes attr) +//{ +//#ifndef FALLBACK +// // Calculate the essentials +// const Offset offset = g_offsets[InstanceID()]; +// const Material material = g_materials[offset.material_idx]; +// const float3 hit_pos = HitWorldPosition(); +// const float index_offset = offset.idx_offset; +// const float vertex_offset = offset.vertex_offset; +// +// // Find first index location +// const uint index_size = 4; +// const uint indices_per_triangle = 3; +// const uint triangle_idx_stride = indices_per_triangle * index_size; +// +// uint base_idx = PrimitiveIndex() * triangle_idx_stride; +// base_idx += index_offset * 4; // offset the start +// +// uint3 indices = Load3x32BitIndices(g_indices, base_idx); +// indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start +// +// // Gather triangle vertices +// const Vertex v0 = g_vertices[indices.x]; +// const Vertex v1 = g_vertices[indices.y]; +// const Vertex v2 = g_vertices[indices.z]; +// +// //Get data from VBO +// float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; +// uv.y = 1.0f - uv.y; +// +// OutputMaterialData output_data = InterpretMaterialDataRT(material.data, +// g_textures[material.albedo_id], +// g_textures[material.normal_id], +// g_textures[material.roughness_id], +// g_textures[material.metalicness_id], +// g_textures[material.emissive_id], +// g_textures[material.ao_id], +// 0, +// s0, +// uv); +// +// float alpha = output_data.alpha; +// +// if (alpha < 0.5f) +// { +// IgnoreHit(); +// } +// else +// { +// AcceptHitAndEndSearch(); +// } +//#else +// payload.color = float3(0.0f, 0.0f, 0.0f); +//#endif +//} + + [shader("closesthit")] -void ClosestHitEntry(inout FullRTHitInfo payload, in MyAttributes attr) +void ClosestHitEntry(inout FullRTHitInfo payload, in Attributes attr) { // Calculate the essentials const Offset offset = g_offsets[InstanceID()]; @@ -222,7 +280,7 @@ void ClosestHitEntry(inout FullRTHitInfo payload, in MyAttributes attr) } [shader("closesthit")] -void ShadowClosestHitEntry(inout ShadowHitInfo hit, MyAttributes bary) +void ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary) { hit.is_hit = true; } @@ -233,4 +291,75 @@ void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) hit.is_hit = false; } +//[shader("anyhit")] +//void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) +//{ +//#ifndef FALLBACK +// // Calculate the essentials +// const Offset offset = g_offsets[InstanceID()]; +// const Material material = g_materials[offset.material_idx]; +// const float index_offset = offset.idx_offset; +// const float vertex_offset = offset.vertex_offset; +// +// // Find first index location +// const uint index_size = 4; +// const uint indices_per_triangle = 3; +// const uint triangle_idx_stride = indices_per_triangle * index_size; +// +// uint base_idx = PrimitiveIndex() * triangle_idx_stride; +// base_idx += index_offset * 4; // offset the start +// +// uint3 indices = Load3x32BitIndices(g_indices, base_idx); +// indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start +// +// // Gather triangle vertices +// const Vertex v0 = g_vertices[indices.x]; +// const Vertex v1 = g_vertices[indices.y]; +// const Vertex v2 = g_vertices[indices.z]; +// +// // Calculate actual "fragment" attributes. +// const float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr); +// const float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr)); +// const float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr); +// const float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr); +// +// float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; +// uv.y = 1.0f - uv.y; +// +// float mip_level = payload.depth; +// +// OutputMaterialData output_data = InterpretMaterialDataRT(material.data, +// g_textures[material.albedo_id], +// g_textures[material.normal_id], +// g_textures[material.roughness_id], +// g_textures[material.metalicness_id], +// g_textures[material.emissive_id], +// g_textures[material.ao_id], +// mip_level, +// s0, +// uv); +// +// if (material.data.use_alpha_masking == 1.0f) +// { +// float alpha = output_data.alpha; +// +// if (alpha <= 0.5f) +// { +// IgnoreHit(); +// } +// else +// { +// AcceptHitAndEndSearch(); +// } +// } +// else +// { +// AcceptHitAndEndSearch(); +// } +// +//#else +// hit.is_hit = false; +//#endif +//} + #endif //__DXR_RAYTRACING_HLSL__ diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index bcb7724f..277fa184 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -571,10 +571,12 @@ namespace wr lib.exports.push_back(L"RaygenEntry"); lib.exports.push_back(L"ClosestHitEntry"); lib.exports.push_back(L"MissEntry"); + //lib.exports.push_back(L"AnyHitEntry"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); - lib.m_hit_groups.push_back({ L"MyHitGroup", L"ClosestHitEntry" }); - lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + //lib.exports.push_back(L"ShadowAnyHitEntry"); + lib.m_hit_groups.push_back({ L"MyHitGroup", L"ClosestHitEntry" /*, L"AnyHitEntry"*/ }); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" /*, L"ShadowAnyHitEntry"*/ }); return lib; }(); @@ -1064,25 +1066,42 @@ namespace wr .m_rtx = true }); - StateObjectDescription::LibraryDesc path_tracer_so_library = []() + StateObjectDescription::LibraryDesc path_tracer_so_library_transparency = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::path_tracer_lib; lib.exports.push_back(L"RaygenEntry"); lib.exports.push_back(L"ReflectionHit"); lib.exports.push_back(L"ReflectionMiss"); + lib.exports.push_back(L"ReflectionAnyHit"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); - lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit" }); - lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + lib.exports.push_back(L"ShadowAnyHitEntry"); + lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit", L"ReflectionAnyHit" }); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry", L"ShadowAnyHitEntry" }); return lib; }(); - REGISTER(state_objects::path_tracer_state_object, RTPipelineRegistry)( + StateObjectDescription::LibraryDesc path_tracer_so_library_no_transparency = []() + { + StateObjectDescription::LibraryDesc lib; + lib.shader_handle = shaders::path_tracer_lib; + lib.exports.push_back(L"RaygenEntry"); + lib.exports.push_back(L"ReflectionHit"); + lib.exports.push_back(L"ReflectionMiss"); + lib.exports.push_back(L"ShadowClosestHitEntry"); + lib.exports.push_back(L"ShadowMissEntry"); + lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit"}); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry"}); + + return lib; + }(); + + REGISTER(state_objects::path_tracer_state_object_transparency, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, - .library_desc = path_tracer_so_library, + .library_desc = path_tracer_so_library_transparency, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 6, @@ -1090,6 +1109,17 @@ namespace wr .local_root_signatures = {}, }); + REGISTER(state_objects::path_tracer_state_object_no_transparency, RTPipelineRegistry)( + { + .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, + .library_desc = path_tracer_so_library_no_transparency, + .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), + .max_attributes_size = sizeof(float) * 4, + .max_recursion_depth = 6, + .global_root_signature = root_signatures::path_tracing_global, + .local_root_signatures = {}, + }); + /* ### Shadow Raytracing ### */ REGISTER(shaders::rt_shadow_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_shadow_main.hlsl", diff --git a/src/engine_registry.hpp b/src/engine_registry.hpp index e8479761..dbff1dcf 100644 --- a/src/engine_registry.hpp +++ b/src/engine_registry.hpp @@ -807,7 +807,8 @@ namespace wr WISPRENDERER_EXPORT static RegistryHandle state_object; WISPRENDERER_EXPORT static RegistryHandle rt_ao_state_opbject; WISPRENDERER_EXPORT static RegistryHandle path_tracing_state_object; - WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object; + WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object_transparency; + WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object_no_transparency; WISPRENDERER_EXPORT static RegistryHandle rt_shadow_state_object; WISPRENDERER_EXPORT static RegistryHandle rt_reflection_state_object; }; diff --git a/src/imgui_graphics_settings.hpp b/src/imgui_graphics_settings.hpp index c704fa9e..05311a0c 100644 --- a/src/imgui_graphics_settings.hpp +++ b/src/imgui_graphics_settings.hpp @@ -15,6 +15,7 @@ namespace wr::imgui::window static bool asbuild_settings_open = true; static bool shadow_settings_open = true; static bool shadow_denoiser_settings_open = true; + static bool path_tracer_settings_open = true; void GraphicsSettings(FrameGraph* frame_graph) { @@ -128,6 +129,20 @@ namespace wr::imgui::window } + + if (frame_graph->HasTask() && path_tracer_settings_open) + { + auto pt_user_settings = frame_graph->GetSettings(); + + ImGui::Begin("Path Tracing Settings", &path_tracer_settings_open); + + ImGui::Checkbox("Allow transparency", &pt_user_settings.m_runtime.m_allow_transparency); + + ImGui::End(); + frame_graph->UpdateSettings(pt_user_settings); + + + } } }// namepace imgui::window diff --git a/src/render_tasks/d3d12_path_tracer.hpp b/src/render_tasks/d3d12_path_tracer.hpp index db2a78ba..302b8479 100644 --- a/src/render_tasks/d3d12_path_tracer.hpp +++ b/src/render_tasks/d3d12_path_tracer.hpp @@ -16,6 +16,16 @@ namespace wr { + struct PathTracerSettings + { + struct Runtime + { + bool m_allow_transparency = false; + }; + Runtime m_runtime; + }; + + //TODO, this struct is unpadded, manual padding might be usefull. struct PathTracerData { @@ -176,7 +186,7 @@ namespace wr // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); - data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object)); + data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_transparency)); // Create Shader Tables CreateShaderTables(device, data, 0); @@ -206,6 +216,19 @@ namespace wr auto& data = fg.GetData(handle); auto& as_build_data = fg.GetPredecessorData(); auto frame_idx = n_render_system.GetFrameIdx(); + auto settings = fg.GetSettings(handle); + + // Pipeline State Object + auto& rt_registry = RTPipelineRegistry::Get(); + + if (settings.m_runtime.m_allow_transparency) + { + data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_transparency)); + } + else + { + data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_no_transparency)); + } // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) @@ -454,7 +477,8 @@ namespace wr desc.m_type = RenderTaskType::COMPUTE; desc.m_allow_multithreading = true; - fg.AddTask(desc, L"Path Traced Global Illumination", FG_DEPS()); + fg.AddTask(desc, L"Path Traced Global Illumination", FG_DEPS()); + fg.UpdateSettings(PathTracerSettings()); } } /* wr */ From 76730501385f7b5581f9d35ac3216fa4ebe09413 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Sat, 15 Jun 2019 14:07:55 +0200 Subject: [PATCH 06/12] [CHANGES] Refactored rt ao task to use rt hybrid base classes and helpers Committing just to switch to master --- imgui.ini | 130 ------------------ .../shaders/deferred_composition_pass.hlsl | 1 + resources/shaders/dxr_ambient_occlusion.hlsl | 10 +- src/engine_registry.cpp | 3 +- src/imgui_graphics_settings.hpp | 3 +- src/render_tasks/d3d12_rt_hybrid_helpers.hpp | 3 +- src/render_tasks/d3d12_rtao_task.hpp | 127 +++++------------ 7 files changed, 46 insertions(+), 231 deletions(-) delete mode 100644 imgui.ini diff --git a/imgui.ini b/imgui.ini deleted file mode 100644 index ba2a201c..00000000 --- a/imgui.ini +++ /dev/null @@ -1,130 +0,0 @@ -[Window][DockspaceViewport_11111111] -Pos=0,21 -Size=1280,699 -Collapsed=0 - -[Window][Debug##Default] -Pos=1022,401 -Size=228,150 -Collapsed=1 - -[Window][Theme] -Pos=1032,376 -Size=248,344 -Collapsed=0 -DockId=0x00000005,0 - -[Window][ImGui Details] -Pos=1032,607 -Size=248,113 -Collapsed=0 - -[Window][Camera Settings] -Pos=0,475 -Size=219,245 -Collapsed=0 -DockId=0x00000008,0 - -[Window][Light Editor] -Pos=0,21 -Size=32,32 -Collapsed=0 - -[Window][Shader Registry] -Pos=340,105 -Size=590,455 -Collapsed=0 - -[Window][Pipeline Registry] -Pos=317,134 -Size=312,443 -Collapsed=0 - -[Window][Root Signature Registry] -Pos=748,338 -Size=169,259 -Collapsed=0 - -[Window][DirectX 12 Settings] -Pos=1032,376 -Size=248,344 -Collapsed=0 -DockId=0x00000005,2 - -[Window][Hardware Info] -Pos=1032,376 -Size=248,344 -Collapsed=0 -DockId=0x00000005,1 - -[Window][Model Editor] -Pos=0,31 -Size=32,32 -Collapsed=0 - -[Window][Light Details] -Pos=0,403 -Size=220,129 -Collapsed=0 - -[Window][Inspect] -Pos=26,21 -Size=32,32 -Collapsed=0 - -[Window][Viewport] -Pos=221,21 -Size=809,699 -Collapsed=0 -DockId=0x00000002,0 - -[Window][Scene Graph Editor] -Pos=0,21 -Size=219,452 -Collapsed=0 -DockId=0x00000007,0 - -[Window][Inspector] -Pos=1032,21 -Size=248,353 -Collapsed=0 -DockId=0x00000006,0 - -[Window][DockSpaceViewport_11111111] -Pos=0,21 -Size=1280,699 -Collapsed=0 - -[Window][Stats] -Pos=280,121 -Size=205,85 -Collapsed=0 - -[Window][Test popup] -ViewportPos=1503,395 -ViewportId=0xB4FE7E97 -Size=151,177 -Collapsed=0 - -[Window][Emissive Multiplier] -Pos=944,589 -Size=303,134 -Collapsed=0 - -[Window][RTAO Settings] -Pos=1032,21 -Size=248,353 -Collapsed=0 -DockId=0x00000006,1 - -[Docking][Data] -DockSpace ID=0x8B93E3BD Pos=0,21 Size=1280,699 Split=X - DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=1030,699 Split=X - DockNode ID=0x00000001 Parent=0x00000003 SizeRef=219,699 Split=Y SelectedTab=0xF4865B00 - DockNode ID=0x00000007 Parent=0x00000001 SizeRef=219,452 SelectedTab=0xF4865B00 - DockNode ID=0x00000008 Parent=0x00000001 SizeRef=219,245 SelectedTab=0x8722C6A1 - DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1059,699 CentralNode=1 SelectedTab=0x995B0CF8 - DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=248,699 Split=Y SelectedTab=0x070A839D - DockNode ID=0x00000006 Parent=0x00000004 SizeRef=248,353 SelectedTab=0xDA48A090 - DockNode ID=0x00000005 Parent=0x00000004 SizeRef=248,344 SelectedTab=0x070A839D - diff --git a/resources/shaders/deferred_composition_pass.hlsl b/resources/shaders/deferred_composition_pass.hlsl index 7629ad54..efaccff5 100644 --- a/resources/shaders/deferred_composition_pass.hlsl +++ b/resources/shaders/deferred_composition_pass.hlsl @@ -131,6 +131,7 @@ void main_cs(int3 dispatch_thread_id : SV_DispatchThreadID) // Shade pixel retval = shade_pixel(pos, V, albedo, metallic, roughness, emissive, normal, irradiance, ao, reflection, sampled_brdf, shadow_factor, has_shadows); + retval = ao.xxx; } else { diff --git a/resources/shaders/dxr_ambient_occlusion.hlsl b/resources/shaders/dxr_ambient_occlusion.hlsl index b1377cd1..8f0a55ac 100644 --- a/resources/shaders/dxr_ambient_occlusion.hlsl +++ b/resources/shaders/dxr_ambient_occlusion.hlsl @@ -32,7 +32,6 @@ cbuffer CBData : register(b0) unsigned int sample_count; }; - bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned int depth) { // Define a ray, consisting of origin, direction, and the min-max distance values @@ -48,6 +47,7 @@ bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned i TraceRay( Scene, RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, + //RAY_FLAG_NONE, ~0, // InstanceInclusionMask 0, // RayContributionToHitGroupIndex 0, // MultiplierForGeometryContributionToHitGroupIndex @@ -58,7 +58,6 @@ bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned i return payload.is_hit; } - [shader("raygeneration")] void AORaygenEntry() { @@ -80,9 +79,9 @@ void AORaygenEntry() { //SPP decreases the closer a pixel is to the max distance //Total is always calculated using the full sample count to have further pixels less occluded - int spp = min(sample_count, round(sample_count * ((max_distance - cam_distance)/max_distance))); + int spp = min(sample_count, round(sample_count * ((max_distance - cam_distance) / max_distance))); int ao_value = sample_count; - for(uint i = 0; i< spp; i++) + for(uint i = 0; i < spp; i++) { ao_value -= TraceAORay(0, wpos + normal * bias , getCosHemisphereSample(rand_seed, normal), radius, 0); } @@ -96,10 +95,11 @@ void AORaygenEntry() } [shader("miss")] -void MissEntry(inout AOHitInfo hit : SV_RayPayload) +void AOMissEntry(inout AOHitInfo hit : SV_RayPayload) { hit.is_hit = 0.0f; } + // //[shader("anyhit")] //void AnyHitEntry(inout AOHitInfo hit, in Attributes attr) diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index 277fa184..5a2015d8 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -1014,8 +1014,9 @@ namespace wr StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_ao_lib; lib.exports.push_back(L"AORaygenEntry"); - lib.exports.push_back(L"MissEntry"); + lib.exports.push_back(L"AOMissEntry"); //lib.exports.push_back(L"AnyHitEntry"); + //lib.m_hit_groups.push_back({ L"AOHitGroup", L"AOHitEntry", L"AnyHitEntry" }); return lib; }(); diff --git a/src/imgui_graphics_settings.hpp b/src/imgui_graphics_settings.hpp index 05311a0c..8520d6af 100644 --- a/src/imgui_graphics_settings.hpp +++ b/src/imgui_graphics_settings.hpp @@ -26,6 +26,7 @@ namespace wr::imgui::window ImGui::DragFloat("Bias", &rtao_user_settings.m_runtime.bias, 0.01f, 0.0f, 100.f); ImGui::DragFloat("Radius", &rtao_user_settings.m_runtime.radius, 0.1f, 0.0f, 1000.f); + ImGui::DragFloat("Max Distance", &rtao_user_settings.m_runtime.max_distance, 0.1f, 0.0f, 5000.f); ImGui::DragFloat("Power", &rtao_user_settings.m_runtime.power, 0.1f, 0.0f, 10.f); ImGui::DragInt("SPP", &rtao_user_settings.m_runtime.sample_count, 1, 0, 1073741824); @@ -102,7 +103,7 @@ namespace wr::imgui::window { auto shadow_user_settings = frame_graph->GetSettings(); - ImGui::Begin("Shadow Settings", &rtao_settings_open); + ImGui::Begin("Shadow Settings", &shadow_settings_open); ImGui::DragFloat("Epsilon", &shadow_user_settings.m_runtime.m_epsilon, 0.01f, 0.0f, 15.f); ImGui::DragInt("Sample Count", &shadow_user_settings.m_runtime.m_sample_count, 1, 1, 64); diff --git a/src/render_tasks/d3d12_rt_hybrid_helpers.hpp b/src/render_tasks/d3d12_rt_hybrid_helpers.hpp index 8c5fd939..b37020bb 100644 --- a/src/render_tasks/d3d12_rt_hybrid_helpers.hpp +++ b/src/render_tasks/d3d12_rt_hybrid_helpers.hpp @@ -29,7 +29,6 @@ namespace wr std::array out_miss_shader_table = { nullptr, nullptr, nullptr }; std::array out_hitgroup_shader_table = { nullptr, nullptr, nullptr }; - // Pipeline objects d3d12::StateObject* out_state_object = nullptr; d3d12::RootSignature* out_root_signature = nullptr; @@ -82,6 +81,7 @@ namespace wr } // Set up Miss Shader Table + if (miss_entries.size() > 0) { // Create Record(s) and Table(s) std::uint32_t shader_record_count = miss_entries.size(); @@ -98,6 +98,7 @@ namespace wr } // Set up Hit Group Shader Table + if (hit_groups.size() > 0) { // Create Record(s) std::uint32_t shader_record_count = hit_groups.size(); diff --git a/src/render_tasks/d3d12_rtao_task.hpp b/src/render_tasks/d3d12_rtao_task.hpp index bfab2927..bca8d424 100644 --- a/src/render_tasks/d3d12_rtao_task.hpp +++ b/src/render_tasks/d3d12_rtao_task.hpp @@ -12,6 +12,7 @@ #include "../render_tasks/d3d12_deferred_main.hpp" #include "../render_tasks/d3d12_build_acceleration_structures.hpp" +#include "../render_tasks/d3d12_rt_hybrid_helpers.hpp" namespace wr { @@ -31,75 +32,11 @@ namespace wr struct RTAOData { - - d3d12::AccelerationStructure out_tlas = {}; - - // Shader tables - std::array in_raygen_shader_table = { nullptr, nullptr, nullptr }; - std::array in_miss_shader_table = { nullptr, nullptr, nullptr }; - std::array in_hitgroup_shader_table = { nullptr, nullptr, nullptr }; - - // Pipeline objects - d3d12::StateObject* in_state_object; - d3d12::RootSignature* in_root_signature; - - D3D12ConstantBufferHandle* out_cb_handle; - d3d12::RenderTarget* in_deferred_main_rt; - - DescriptorAllocation out_uav_from_rtv; - DescriptorAllocation in_gbuffers; - DescriptorAllocation in_depthbuffer; - - bool tlas_requires_init = false; + RTHybrid_BaseData base_data; }; namespace internal { - inline void CreateShaderTables(d3d12::Device* device, RTAOData& data, int frame_idx) - { - // Delete existing shader table - if (data.in_miss_shader_table[frame_idx]) - { - d3d12::Destroy(data.in_miss_shader_table[frame_idx]); - } - if (data.in_hitgroup_shader_table[frame_idx]) - { - d3d12::Destroy(data.in_hitgroup_shader_table[frame_idx]); - } - if (data.in_raygen_shader_table[frame_idx]) - { - d3d12::Destroy(data.in_raygen_shader_table[frame_idx]); - } - - // Set up Raygen Shader Table - { - // Create Record(s) - UINT shader_record_count = 1; - auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); - auto shader_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, "AORaygenEntry"); - - auto shader_record = d3d12::CreateShaderRecord(shader_identifier, shader_identifier_size); - - // Create Table - data.in_raygen_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); - d3d12::AddShaderRecord(data.in_raygen_shader_table[frame_idx], shader_record); - } - - // Set up Miss Shader Table - { - // Create Record(s) - UINT shader_record_count = 1; - auto shader_identifier_size = d3d12::GetShaderIdentifierSize(device); - - auto miss_identifier = d3d12::GetShaderIdentifier(device, data.in_state_object, "MissEntry"); - auto miss_record = d3d12::CreateShaderRecord(miss_identifier, shader_identifier_size); - - // Create Table(s) - data.in_miss_shader_table[frame_idx] = d3d12::CreateShaderTable(device, shader_record_count, shader_identifier_size); - d3d12::AddShaderRecord(data.in_miss_shader_table[frame_idx], miss_record); - } - } - inline void SetupAOTask(RenderSystem& render_system, FrameGraph& fg, RenderTaskHandle& handle, bool resize) { // Initialize variables @@ -109,27 +46,29 @@ namespace wr auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"AO Target"); + RTHybrid_BaseData& h_data = data.base_data; + if (!resize) { auto& as_build_data = fg.GetPredecessorData(); - data.out_uav_from_rtv = std::move(as_build_data.out_allocator->Allocate(1)); - data.in_gbuffers = std::move(as_build_data.out_allocator->Allocate(1)); - data.in_depthbuffer = std::move(as_build_data.out_allocator->Allocate(1)); + h_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + h_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + h_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate(1)); } // Versioning for (int frame_idx = 0; frame_idx < 1; ++frame_idx) { // Bind output texture - d3d12::DescHeapCPUHandle rtv_handle = data.out_uav_from_rtv.GetDescriptorHandle(); + d3d12::DescHeapCPUHandle rtv_handle = h_data.out_output_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); // Bind g-buffers - d3d12::DescHeapCPUHandle normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0); - d3d12::DescHeapCPUHandle depth_buffer_handle = data.in_depthbuffer.GetDescriptorHandle(0); + d3d12::DescHeapCPUHandle normal_gbuffer_handle = h_data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); + d3d12::DescHeapCPUHandle depth_buffer_handle = h_data.out_gbuffer_depth_alloc.GetDescriptorHandle(0); - auto deferred_main_rt = data.in_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); + auto deferred_main_rt = h_data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_gbuffer_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats.data()[1]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_buffer_handle); @@ -138,20 +77,20 @@ namespace wr if (!resize) { // Camera constant buffer - data.out_cb_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); + h_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); - data.in_state_object = static_cast(rt_registry.Find(state_objects::rt_ao_state_opbject)); + h_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_ao_state_opbject)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); - data.in_root_signature = static_cast(rs_registry.Find(root_signatures::rt_ao_global)); + h_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_ao_global)); // Create Shader Tables - CreateShaderTables(device, data, 0); - CreateShaderTables(device, data, 1); - CreateShaderTables(device, data, 2); + CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, {}, 0); + CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, {}, 1); + CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, {}, 2); } } @@ -170,19 +109,21 @@ namespace wr fg.WaitForPredecessorTask(); float scalar = 1.0f; + RTHybrid_BaseData& h_data = data.base_data; + if (n_render_system.m_render_window.has_value()) { - d3d12::BindRaytracingPipeline(cmd_list, data.in_state_object, false); + d3d12::BindRaytracingPipeline(cmd_list, h_data.out_state_object, false); // Bind output, indices and materials, offsets, etc - auto out_uav_handle = data.out_uav_from_rtv.GetDescriptorHandle(); + auto out_uav_handle = h_data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::OUTPUT)), out_uav_handle); - auto in_scene_normal_gbuffer_handle = data.in_gbuffers.GetDescriptorHandle(0); + auto in_scene_normal_gbuffer_handle = h_data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 0, in_scene_normal_gbuffer_handle); - auto in_scene_depth_handle = data.in_depthbuffer.GetDescriptorHandle(); + auto in_scene_depth_handle = h_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 1, in_scene_depth_handle); // Update offset data @@ -206,14 +147,14 @@ namespace wr cb_data.m_frame_idx = frame_idx; cb_data.m_sample_count = static_cast(settings.m_runtime.sample_count); - n_render_system.m_camera_pool->Update(data.out_cb_handle, sizeof(temp::RTAO_CBData), 0, frame_idx, (std::uint8_t*)& cb_data); // FIXME: Uhh wrong pool? + n_render_system.m_camera_pool->Update(h_data.out_cb_camera_handle, sizeof(temp::RTAO_CBData), 0, frame_idx, (std::uint8_t*)& cb_data); // FIXME: Uhh wrong pool? // Transition depth to NON_PIXEL_RESOURCE - d3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); + d3d12::TransitionDepth(cmd_list, h_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, false); d3d12::BindDescriptorHeaps(cmd_list, false); - d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_handle->m_native, 2, frame_idx); + d3d12::BindComputeConstantBuffer(cmd_list, h_data.out_cb_camera_handle->m_native, 2, frame_idx); if (!as_build_data.out_blas_list.empty()) { @@ -221,23 +162,23 @@ namespace wr } #ifdef _DEBUG - CreateShaderTables(device, data, frame_idx); + CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, {}, frame_idx); #endif // _DEBUG scalar = fg.GetRenderTargetResolutionScale(handle); // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, - data.in_hitgroup_shader_table[frame_idx], - data.in_miss_shader_table[frame_idx], - data.in_raygen_shader_table[frame_idx], + h_data.out_hitgroup_shader_table[frame_idx], + h_data.out_miss_shader_table[frame_idx], + h_data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(scalar * d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(scalar * d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE - d3d12::TransitionDepth(cmd_list, data.in_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); + d3d12::TransitionDepth(cmd_list, h_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } @@ -248,9 +189,9 @@ namespace wr if (!resize) { // Small hack to force the allocations to go out of scope, which will tell the allocator to free them - DescriptorAllocation temp1 = std::move(data.out_uav_from_rtv); - DescriptorAllocation temp2 = std::move(data.in_gbuffers); - DescriptorAllocation temp3 = std::move(data.in_depthbuffer); + DescriptorAllocation temp1 = std::move(data.base_data.out_output_alloc); + DescriptorAllocation temp2 = std::move(data.base_data.out_gbuffer_normal_alloc); + DescriptorAllocation temp3 = std::move(data.base_data.out_gbuffer_depth_alloc); } } } From 5e5af30918c939fd6af574356c0e5a9c7f73bdd4 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Tue, 18 Jun 2019 11:03:29 +0200 Subject: [PATCH 07/12] [CHANGE] Shadows were bugged in full raytracing --- resources/shaders/dxr_raytracing.hlsl | 66 ++------------------------- src/engine_registry.cpp | 6 +-- 2 files changed, 6 insertions(+), 66 deletions(-) diff --git a/resources/shaders/dxr_raytracing.hlsl b/resources/shaders/dxr_raytracing.hlsl index 28effff6..ac002c09 100644 --- a/resources/shaders/dxr_raytracing.hlsl +++ b/resources/shaders/dxr_raytracing.hlsl @@ -145,64 +145,6 @@ void MissEntry(inout FullRTHitInfo payload) payload.color = skybox.SampleLevel(s0, WorldRayDirection(), 0).rgb; } -//[shader("anyhit")] -//void AnyHitEntry(inout FullRTHitInfo payload, in Attributes attr) -//{ -//#ifndef FALLBACK -// // Calculate the essentials -// const Offset offset = g_offsets[InstanceID()]; -// const Material material = g_materials[offset.material_idx]; -// const float3 hit_pos = HitWorldPosition(); -// const float index_offset = offset.idx_offset; -// const float vertex_offset = offset.vertex_offset; -// -// // Find first index location -// const uint index_size = 4; -// const uint indices_per_triangle = 3; -// const uint triangle_idx_stride = indices_per_triangle * index_size; -// -// uint base_idx = PrimitiveIndex() * triangle_idx_stride; -// base_idx += index_offset * 4; // offset the start -// -// uint3 indices = Load3x32BitIndices(g_indices, base_idx); -// indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start -// -// // Gather triangle vertices -// const Vertex v0 = g_vertices[indices.x]; -// const Vertex v1 = g_vertices[indices.y]; -// const Vertex v2 = g_vertices[indices.z]; -// -// //Get data from VBO -// float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; -// uv.y = 1.0f - uv.y; -// -// OutputMaterialData output_data = InterpretMaterialDataRT(material.data, -// g_textures[material.albedo_id], -// g_textures[material.normal_id], -// g_textures[material.roughness_id], -// g_textures[material.metalicness_id], -// g_textures[material.emissive_id], -// g_textures[material.ao_id], -// 0, -// s0, -// uv); -// -// float alpha = output_data.alpha; -// -// if (alpha < 0.5f) -// { -// IgnoreHit(); -// } -// else -// { -// AcceptHitAndEndSearch(); -// } -//#else -// payload.color = float3(0.0f, 0.0f, 0.0f); -//#endif -//} - - [shader("closesthit")] void ClosestHitEntry(inout FullRTHitInfo payload, in Attributes attr) { @@ -274,21 +216,21 @@ void ClosestHitEntry(inout FullRTHitInfo payload, in Attributes attr) } nextRand(payload.seed); - payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1); - payload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); + //payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1); + payload.color = ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); payload.color += emissive; } [shader("closesthit")] void ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary) { - hit.is_hit = true; + hit.ray_power = 0.1f; } [shader("miss")] void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) { - hit.is_hit = false; + hit.ray_power = 1.0f; } //[shader("anyhit")] diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index 2871b5a2..d6159f09 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -571,12 +571,10 @@ namespace wr lib.exports.push_back(L"RaygenEntry"); lib.exports.push_back(L"ClosestHitEntry"); lib.exports.push_back(L"MissEntry"); - //lib.exports.push_back(L"AnyHitEntry"); lib.exports.push_back(L"ShadowClosestHitEntry"); lib.exports.push_back(L"ShadowMissEntry"); - //lib.exports.push_back(L"ShadowAnyHitEntry"); - lib.m_hit_groups.push_back({ L"MyHitGroup", L"ClosestHitEntry" /*, L"AnyHitEntry"*/ }); - lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" /*, L"ShadowAnyHitEntry"*/ }); + lib.m_hit_groups.push_back({ L"MyHitGroup", L"ClosestHitEntry"}); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry"}); return lib; }(); From 9fccc9b0ffaf784720f43847049e30f1df0c8eb1 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Tue, 18 Jun 2019 12:04:13 +0200 Subject: [PATCH 08/12] [CHANGE] Acted on some of the feedback from Pull Request --- resources/shaders/dxr_raytracing.hlsl | 71 --------------------------- tests/demo/demo.cpp | 2 +- 2 files changed, 1 insertion(+), 72 deletions(-) diff --git a/resources/shaders/dxr_raytracing.hlsl b/resources/shaders/dxr_raytracing.hlsl index ac002c09..d1f17f23 100644 --- a/resources/shaders/dxr_raytracing.hlsl +++ b/resources/shaders/dxr_raytracing.hlsl @@ -233,75 +233,4 @@ void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) hit.ray_power = 1.0f; } -//[shader("anyhit")] -//void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) -//{ -//#ifndef FALLBACK -// // Calculate the essentials -// const Offset offset = g_offsets[InstanceID()]; -// const Material material = g_materials[offset.material_idx]; -// const float index_offset = offset.idx_offset; -// const float vertex_offset = offset.vertex_offset; -// -// // Find first index location -// const uint index_size = 4; -// const uint indices_per_triangle = 3; -// const uint triangle_idx_stride = indices_per_triangle * index_size; -// -// uint base_idx = PrimitiveIndex() * triangle_idx_stride; -// base_idx += index_offset * 4; // offset the start -// -// uint3 indices = Load3x32BitIndices(g_indices, base_idx); -// indices += float3(vertex_offset, vertex_offset, vertex_offset); // offset the start -// -// // Gather triangle vertices -// const Vertex v0 = g_vertices[indices.x]; -// const Vertex v1 = g_vertices[indices.y]; -// const Vertex v2 = g_vertices[indices.z]; -// -// // Calculate actual "fragment" attributes. -// const float3 frag_pos = HitAttribute(v0.pos, v1.pos, v2.pos, attr); -// const float3 normal = normalize(HitAttribute(v0.normal, v1.normal, v2.normal, attr)); -// const float3 tangent = HitAttribute(v0.tangent, v1.tangent, v2.tangent, attr); -// const float3 bitangent = HitAttribute(v0.bitangent, v1.bitangent, v2.bitangent, attr); -// -// float2 uv = HitAttribute(float3(v0.uv, 0), float3(v1.uv, 0), float3(v2.uv, 0), attr).xy; -// uv.y = 1.0f - uv.y; -// -// float mip_level = payload.depth; -// -// OutputMaterialData output_data = InterpretMaterialDataRT(material.data, -// g_textures[material.albedo_id], -// g_textures[material.normal_id], -// g_textures[material.roughness_id], -// g_textures[material.metalicness_id], -// g_textures[material.emissive_id], -// g_textures[material.ao_id], -// mip_level, -// s0, -// uv); -// -// if (material.data.use_alpha_masking == 1.0f) -// { -// float alpha = output_data.alpha; -// -// if (alpha <= 0.5f) -// { -// IgnoreHit(); -// } -// else -// { -// AcceptHitAndEndSearch(); -// } -// } -// else -// { -// AcceptHitAndEndSearch(); -// } -// -//#else -// hit.is_hit = false; -//#endif -//} - #endif //__DXR_RAYTRACING_HLSL__ diff --git a/tests/demo/demo.cpp b/tests/demo/demo.cpp index 8cc87ccf..753809ef 100644 --- a/tests/demo/demo.cpp +++ b/tests/demo/demo.cpp @@ -29,7 +29,7 @@ #include "model_loader_tinygltf.hpp" #include "d3d12/d3d12_dynamic_descriptor_heap.hpp" -using DefaultScene = TransparencyScene; +using DefaultScene = ViknellScene; //#define ENABLE_PHYSICS std::unique_ptr render_system; From c6f64975392ac45615707f8d2519f2707824f9b2 Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Wed, 19 Jun 2019 23:26:01 +0200 Subject: [PATCH 09/12] [CHANGE] Reverted left out debug code --- resources/shaders/dxr_raytracing.hlsl | 4 ++-- src/engine_registry.cpp | 2 +- tests/demo/demo_frame_graphs.hpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/shaders/dxr_raytracing.hlsl b/resources/shaders/dxr_raytracing.hlsl index d1f17f23..94358990 100644 --- a/resources/shaders/dxr_raytracing.hlsl +++ b/resources/shaders/dxr_raytracing.hlsl @@ -216,8 +216,8 @@ void ClosestHitEntry(inout FullRTHitInfo payload, in Attributes attr) } nextRand(payload.seed); - //payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1); - payload.color = ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); + payload.color = ggxIndirect(hit_pos, fN, N, V, albedo, metal, roughness, ao, payload.seed, payload.depth + 1); + payload.color += ggxDirect(hit_pos, fN, N, V, albedo, metal, roughness, payload.seed, payload.depth + 1); payload.color += emissive; } diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index d6159f09..a69fbab2 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -1009,7 +1009,7 @@ namespace wr ROOT_PARAM(GetSRV(params::rt_ao, params::RTAOE::VERTICES)), }, .m_samplers = { - { TextureFilter::FILTER_ANISOTROPIC, TextureAddressMode::TAM_WRAP } + { TextureFilter::FILTER_POINT, TextureAddressMode::TAM_WRAP } }, .m_rtx = true }); diff --git a/tests/demo/demo_frame_graphs.hpp b/tests/demo/demo_frame_graphs.hpp index 1568a06b..a59989d6 100644 --- a/tests/demo/demo_frame_graphs.hpp +++ b/tests/demo/demo_frame_graphs.hpp @@ -202,7 +202,7 @@ namespace fg_manager wr::AddRTReflectionTask(*fg); wr::AddRTShadowTask(*fg); - //wr::AddShadowDenoiserTask(*fg); + wr::AddShadowDenoiserTask(*fg); //Raytraced Ambient Occlusion task wr::AddRTAOTask(*fg, static_cast(rs).m_device); From a61279a5f9fa1fd71cfe5e291a4e87ce7347da6c Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Thu, 20 Jun 2019 11:12:16 +0200 Subject: [PATCH 10/12] [CHANGE] - Refactored Reflection, Shadows, AO tasks to use inheritance on the RT Hybrid data - Use of transparency is taken directly from the AS build data, except for path tracing, as that task has an override. - Gave some structure to the imgui layout - Changed Destroy function for the ShaderTable, now correctly set to nullptr, as that is also checked in dispatch rays - Acted on the feedback received from the PR. --- imgui.ini | 86 +++++++------ resources/shaders/dxr_ambient_occlusion.hlsl | 63 +++++++++- resources/shaders/dxr_raytracing.hlsl | 13 +- resources/shaders/dxr_shadow_entries.hlsl | 10 +- resources/shaders/dxr_shadow_functions.hlsl | 14 +-- resources/shaders/dxr_structs.hlsl | 2 +- src/d3d12/d3d12_functions.hpp | 2 +- src/d3d12/d3d12_shader_table.cpp | 3 +- src/engine_registry.cpp | 109 ++++++++++++++--- src/engine_registry.hpp | 6 +- src/imgui_graphics_settings.hpp | 12 +- src/material_pool.hpp | 5 - .../d3d12_build_acceleration_structures.hpp | 18 +-- src/render_tasks/d3d12_path_tracer.hpp | 39 +++--- src/render_tasks/d3d12_rt_reflection_task.hpp | 75 ++++++------ src/render_tasks/d3d12_rt_shadow_task.hpp | 77 ++++++------ src/render_tasks/d3d12_rtao_task.hpp | 113 ++++++++++-------- tests/demo/demo.cpp | 2 +- 18 files changed, 404 insertions(+), 245 deletions(-) diff --git a/imgui.ini b/imgui.ini index 963ddc95..a1314b51 100644 --- a/imgui.ini +++ b/imgui.ini @@ -9,8 +9,8 @@ Size=228,150 Collapsed=1 [Window][Theme] -Pos=1032,376 -Size=248,201 +Pos=1548,307 +Size=372,313 Collapsed=0 DockId=0x00000009,0 @@ -20,8 +20,8 @@ Size=248,113 Collapsed=0 [Window][Camera Settings] -Pos=0,342 -Size=270,378 +Pos=0,478 +Size=270,539 Collapsed=0 DockId=0x00000008,0 @@ -46,14 +46,14 @@ Size=169,259 Collapsed=0 [Window][DirectX 12 Settings] -Pos=1032,376 -Size=248,201 +Pos=1548,307 +Size=372,313 Collapsed=0 DockId=0x00000009,2 [Window][Hardware Info] -Pos=1032,376 -Size=248,201 +Pos=1548,307 +Size=372,313 Collapsed=0 DockId=0x00000009,1 @@ -74,25 +74,25 @@ Collapsed=0 [Window][Viewport] Pos=272,21 -Size=758,699 +Size=1274,996 Collapsed=0 DockId=0x00000002,0 [Window][Scene Graph Editor] Pos=0,21 -Size=270,319 +Size=270,455 Collapsed=0 DockId=0x00000007,1 [Window][Inspector] -Pos=1032,21 -Size=248,353 +Pos=1548,21 +Size=372,284 Collapsed=0 DockId=0x00000006,0 [Window][DockSpaceViewport_11111111] Pos=0,21 -Size=1280,699 +Size=1920,996 Collapsed=0 [Window][Stats] @@ -112,51 +112,59 @@ Size=303,134 Collapsed=0 [Window][RTAO Settings] -Pos=1032,579 -Size=248,141 +Pos=1548,622 +Size=372,173 Collapsed=0 -DockId=0x0000000A,0 +DockId=0x0000000B,0 [Window][HBAO+ Settings] -Pos=1032,579 -Size=248,141 +Pos=1548,622 +Size=372,173 Collapsed=0 -DockId=0x0000000A,0 +DockId=0x0000000B,0 [Window][NVIDIA Ansel Settings] -Pos=1032,376 -Size=248,201 +Pos=1548,307 +Size=372,313 Collapsed=0 DockId=0x00000009,3 [Window][Acceleration Structure Settings] -Pos=1032,579 -Size=248,141 +Pos=1548,797 +Size=372,220 Collapsed=0 -DockId=0x0000000A,1 +DockId=0x0000000C,0 [Window][Shadow Settings] -Pos=1032,579 -Size=248,141 +Pos=1548,622 +Size=372,173 Collapsed=0 -DockId=0x0000000A,2 +DockId=0x0000000B,1 [Window][Demo Scene] Pos=0,21 -Size=270,319 +Size=270,455 Collapsed=0 DockId=0x00000007,0 +[Window][Path Tracing Settings] +Pos=1548,797 +Size=372,220 +Collapsed=0 +DockId=0x0000000C,1 + [Docking][Data] -DockSpace ID=0x8B93E3BD Pos=0,21 Size=1280,699 Split=X - DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=1030,699 Split=X - DockNode ID=0x00000001 Parent=0x00000003 SizeRef=270,699 Split=Y SelectedTab=0xF4865B00 - DockNode ID=0x00000007 Parent=0x00000001 SizeRef=219,319 SelectedTab=0xF4865B00 - DockNode ID=0x00000008 Parent=0x00000001 SizeRef=219,378 SelectedTab=0x8722C6A1 - DockNode ID=0x00000002 Parent=0x00000003 SizeRef=758,699 CentralNode=1 SelectedTab=0x995B0CF8 - DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=248,699 Split=Y SelectedTab=0x070A839D - DockNode ID=0x00000006 Parent=0x00000004 SizeRef=248,353 SelectedTab=0xF02CD328 - DockNode ID=0x00000005 Parent=0x00000004 SizeRef=248,344 Split=Y SelectedTab=0xDA48A090 - DockNode ID=0x00000009 Parent=0x00000005 SizeRef=248,201 SelectedTab=0x4BB5AED3 - DockNode ID=0x0000000A Parent=0x00000005 SizeRef=248,141 SelectedTab=0xDCA1C528 +DockSpace ID=0x8B93E3BD Pos=0,21 Size=1920,996 Split=X + DockNode ID=0x00000003 Parent=0x8B93E3BD SizeRef=1030,699 Split=X + DockNode ID=0x00000001 Parent=0x00000003 SizeRef=270,699 Split=Y SelectedTab=0xF4865B00 + DockNode ID=0x00000007 Parent=0x00000001 SizeRef=219,319 SelectedTab=0xF4865B00 + DockNode ID=0x00000008 Parent=0x00000001 SizeRef=219,378 SelectedTab=0x8722C6A1 + DockNode ID=0x00000002 Parent=0x00000003 SizeRef=758,699 CentralNode=1 SelectedTab=0x995B0CF8 + DockNode ID=0x00000004 Parent=0x8B93E3BD SizeRef=248,699 Split=Y SelectedTab=0x070A839D + DockNode ID=0x00000006 Parent=0x00000004 SizeRef=248,199 SelectedTab=0xF02CD328 + DockNode ID=0x00000005 Parent=0x00000004 SizeRef=248,498 Split=Y SelectedTab=0xDA48A090 + DockNode ID=0x00000009 Parent=0x00000005 SizeRef=248,219 SelectedTab=0x4BB5AED3 + DockNode ID=0x0000000A Parent=0x00000005 SizeRef=248,277 Split=Y SelectedTab=0x23DB2B59 + DockNode ID=0x0000000B Parent=0x0000000A SizeRef=248,121 SelectedTab=0x23DB2B59 + DockNode ID=0x0000000C Parent=0x0000000A SizeRef=248,154 SelectedTab=0x9426F559 diff --git a/resources/shaders/dxr_ambient_occlusion.hlsl b/resources/shaders/dxr_ambient_occlusion.hlsl index 53a9866f..87c4494a 100644 --- a/resources/shaders/dxr_ambient_occlusion.hlsl +++ b/resources/shaders/dxr_ambient_occlusion.hlsl @@ -25,7 +25,7 @@ typedef BuiltInTriangleIntersectionAttributes Attributes; struct AOHitInfo { float is_hit; - float thisvariablesomehowmakeshybridrenderingwork_killme; + float padding; }; cbuffer CBData : register(b0) @@ -68,8 +68,68 @@ bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned i return payload.is_hit; } +bool TraceAORay_Optimized(uint idx, float3 origin, float3 direction, float far, unsigned int depth) +{ + // Define a ray, consisting of origin, direction, and the min-max distance values + RayDesc ray; + ray.Origin = origin; + ray.Direction = direction; + ray.TMin = 0.f; + ray.TMax = far; + + AOHitInfo payload = { 1.0f, 0.0f }; + + // Trace the ray + TraceRay( + Scene, + RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, + ~0, // InstanceInclusionMask + 0, // RayContributionToHitGroupIndex + 0, // MultiplierForGeometryContributionToHitGroupIndex + 0, // miss shader index is set to idx but can probably be anything. + ray, + payload); + + return payload.is_hit; +} + [shader("raygeneration")] void AORaygenEntry() +{ + // Texture UV coordinates [0, 1] + float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); + + uint rand_seed = initRand(DispatchRaysIndex().x + DispatchRaysIndex().y * DispatchRaysDimensions().x, frame_idx); + + // Screen coordinates [0, resolution] (inverted y) + int2 screen_co = DispatchRaysIndex().xy; + + float3 normal = gbuffer_normal[screen_co].xyz; + float depth = gbuffer_depth[screen_co].x; + float3 wpos = unpack_position(float2(uv.x, 1.f - uv.y), depth, inv_vp); + + float3 camera_pos = float3(inv_view[0][3], inv_view[1][3], inv_view[2][3]); + float cam_distance = length(wpos - camera_pos); + if (cam_distance < max_distance) + { + //SPP decreases the closer a pixel is to the max distance + //Total is always calculated using the full sample count to have further pixels less occluded + int spp = min(sample_count, round(sample_count * ((max_distance - cam_distance) / max_distance))); + int ao_value = sample_count; + for (uint i = 0; i < spp; i++) + { + ao_value -= TraceAORay_Optimized(0, wpos + normal * bias, getCosHemisphereSample(rand_seed, normal), radius, 0); + } + output[DispatchRaysIndex().xy].x = pow(ao_value / float(sample_count), power); + } + else + { + output[DispatchRaysIndex().xy].x = 1.f; + } +} + +[shader("raygeneration")] +void AORaygenEntry_Transparency() { // Texture UV coordinates [0, 1] float2 uv = float2(DispatchRaysIndex().xy) / float2(DispatchRaysDimensions().xy - 1); @@ -101,7 +161,6 @@ void AORaygenEntry() { output[DispatchRaysIndex().xy].x = 1.f; } - } [shader("closesthit")] diff --git a/resources/shaders/dxr_raytracing.hlsl b/resources/shaders/dxr_raytracing.hlsl index 94358990..f027807e 100644 --- a/resources/shaders/dxr_raytracing.hlsl +++ b/resources/shaders/dxr_raytracing.hlsl @@ -32,6 +32,7 @@ SamplerState point_sampler : register(s1); typedef BuiltInTriangleIntersectionAttributes Attributes; #include "dxr_pathtracer_functions.hlsl" +#include "dxr_shadow_entries.hlsl" cbuffer CameraProperties : register(b0) { @@ -221,16 +222,4 @@ void ClosestHitEntry(inout FullRTHitInfo payload, in Attributes attr) payload.color += emissive; } -[shader("closesthit")] -void ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary) -{ - hit.ray_power = 0.1f; -} - -[shader("miss")] -void ShadowMissEntry(inout ShadowHitInfo hit : SV_RayPayload) -{ - hit.ray_power = 1.0f; -} - #endif //__DXR_RAYTRACING_HLSL__ diff --git a/resources/shaders/dxr_shadow_entries.hlsl b/resources/shaders/dxr_shadow_entries.hlsl index 5e2f64ab..083dba89 100644 --- a/resources/shaders/dxr_shadow_entries.hlsl +++ b/resources/shaders/dxr_shadow_entries.hlsl @@ -12,7 +12,6 @@ [shader("closesthit")] void ShadowClosestHitEntry(inout ShadowHitInfo hit, Attributes bary) { - hit.ray_power = 0.1f; hit.is_hit = true; } @@ -71,16 +70,9 @@ void ShadowAnyHitEntry(inout ShadowHitInfo hit, Attributes attr) { IgnoreHit(); } - - hit.ray_power -= (1.0f / 3.0f); - - if (hit.ray_power <= 0.02f) - { - AcceptHitAndEndSearch(); - } else { - IgnoreHit(); + AcceptHitAndEndSearch(); } } else diff --git a/resources/shaders/dxr_shadow_functions.hlsl b/resources/shaders/dxr_shadow_functions.hlsl index 2d5abb51..2b9b041f 100644 --- a/resources/shaders/dxr_shadow_functions.hlsl +++ b/resources/shaders/dxr_shadow_functions.hlsl @@ -4,7 +4,7 @@ #include "dxr_global.hlsl" #include "dxr_structs.hlsl" -float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) +bool TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pass, unsigned int depth) { if (depth >= MAX_RECURSION) { @@ -18,7 +18,7 @@ float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pa ray.TMin = 0; ray.TMax = far; - ShadowHitInfo payload = { false, 1.0f }; + ShadowHitInfo payload = { false, 0.0f }; uint ray_contr_idx = 1; uint miss_idx = 1; @@ -40,7 +40,7 @@ float TraceShadowRay(float3 origin, float3 direction, float far, uint calling_pa ray, payload); - return payload.ray_power; + return payload.is_hit; } // Get shadow factor @@ -55,18 +55,18 @@ float GetShadowFactor(float3 wpos, float3 light_dir, float light_size, float t_m float3 dir = perturbDirectionVector(rand_seed, light_dir, light_size); float3 ray_direction = normalize(dir); - float shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); + bool shadow = TraceShadowRay(wpos, ray_direction, t_max, calling_pass, depth); - shadow_factor += shadow; + shadow_factor += lerp(1.0, 0.0, shadow); } shadow_factor /= float(sample_count); #else //SOFT_SHADOWS - float shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); + bool shadow = TraceShadowRay(wpos, light_dir, t_max, calling_pass, depth); - shadow_factor = shadow; + shadow_factor = !shadow; #endif //SOFT_SHADOWS diff --git a/resources/shaders/dxr_structs.hlsl b/resources/shaders/dxr_structs.hlsl index c2a75f45..9d66dd72 100644 --- a/resources/shaders/dxr_structs.hlsl +++ b/resources/shaders/dxr_structs.hlsl @@ -75,7 +75,7 @@ struct ReflectionHitInfo struct ShadowHitInfo { float is_hit; - float ray_power; + float padding; }; struct PathTracingHitInfo diff --git a/src/d3d12/d3d12_functions.hpp b/src/d3d12/d3d12_functions.hpp index 602f45a9..d1d6cf06 100644 --- a/src/d3d12/d3d12_functions.hpp +++ b/src/d3d12/d3d12_functions.hpp @@ -310,6 +310,6 @@ namespace wr::d3d12 std::uint64_t shader_record_size); void AddShaderRecord(ShaderTable* table, ShaderRecord record); void SetName(AccelerationStructure& acceleration_structure, std::wstring name); - void Destroy(ShaderTable* table); + void Destroy(ShaderTable*& table); } /* wr::d3d12 */ diff --git a/src/d3d12/d3d12_shader_table.cpp b/src/d3d12/d3d12_shader_table.cpp index 2ea6ff79..ebf916dc 100644 --- a/src/d3d12/d3d12_shader_table.cpp +++ b/src/d3d12/d3d12_shader_table.cpp @@ -70,10 +70,11 @@ namespace wr::d3d12 table->m_mapped_shader_records += table->m_shader_record_size; } - void Destroy(ShaderTable* table) + void Destroy(ShaderTable*& table) { SAFE_RELEASE(table->m_resource); delete table; + table = nullptr; } } /* wr::d3d12 */ diff --git a/src/engine_registry.cpp b/src/engine_registry.cpp index a69fbab2..ed34bba0 100644 --- a/src/engine_registry.cpp +++ b/src/engine_registry.cpp @@ -1014,11 +1014,11 @@ namespace wr .m_rtx = true }); - StateObjectDescription::LibraryDesc rt_ao_so_library = []() + StateObjectDescription::LibraryDesc rt_ao_so_transparency_library = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_ao_lib; - lib.exports.push_back(L"AORaygenEntry"); + lib.exports.push_back(L"AORaygenEntry_Transparency"); lib.exports.push_back(L"AOClosestHitEntry"); lib.exports.push_back(L"AOMissEntry"); lib.exports.push_back(L"AOAnyHitEntry"); @@ -1026,10 +1026,30 @@ namespace wr return lib; }(); + StateObjectDescription::LibraryDesc rt_ao_so_no_transparency_library = []() + { + StateObjectDescription::LibraryDesc lib; + lib.shader_handle = shaders::rt_ao_lib; + lib.exports.push_back(L"AORaygenEntry"); + lib.exports.push_back(L"AOMissEntry"); + return lib; + }(); + REGISTER(state_objects::rt_ao_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, - .library_desc = rt_ao_so_library, + .library_desc = rt_ao_so_no_transparency_library, + .max_payload_size = (sizeof(float) * 2), + .max_attributes_size = sizeof(float) * 4, + .max_recursion_depth = 1, + .global_root_signature = root_signatures::rt_ao_global, + .local_root_signatures = {}, + }); + + REGISTER(state_objects::rt_ao_state_object_transparency, RTPipelineRegistry)( + { + .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, + .library_desc = rt_ao_so_transparency_library, .max_payload_size = (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 1, @@ -1105,6 +1125,17 @@ namespace wr return lib; }(); + REGISTER(state_objects::path_tracer_state_object, RTPipelineRegistry)( + { + .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, + .library_desc = path_tracer_so_library_no_transparency, + .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), + .max_attributes_size = sizeof(float) * 4, + .max_recursion_depth = 6, + .global_root_signature = root_signatures::path_tracing_global, + .local_root_signatures = {}, + }); + REGISTER(state_objects::path_tracer_state_object_transparency, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, @@ -1116,17 +1147,6 @@ namespace wr .local_root_signatures = {}, }); - REGISTER(state_objects::path_tracer_state_object_no_transparency, RTPipelineRegistry)( - { - .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, - .library_desc = path_tracer_so_library_no_transparency, - .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), - .max_attributes_size = sizeof(float) * 4, - .max_recursion_depth = 6, - .global_root_signature = root_signatures::path_tracing_global, - .local_root_signatures = {}, - }); - /* ### Shadow Raytracing ### */ REGISTER(shaders::rt_shadow_lib, ShaderRegistry)({ .path = "resources/shaders/dxr_shadow_main.hlsl", @@ -1135,7 +1155,19 @@ namespace wr .defines = {} }); - StateObjectDescription::LibraryDesc rt_shadow_so_library = []() + StateObjectDescription::LibraryDesc rt_shadow_so_library_no_transparency = []() + { + StateObjectDescription::LibraryDesc lib; + lib.shader_handle = shaders::rt_shadow_lib; + lib.exports.push_back(L"ShadowRaygenEntry"); + lib.exports.push_back(L"ShadowClosestHitEntry"); + lib.exports.push_back(L"ShadowMissEntry"); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + + return lib; + }(); + + StateObjectDescription::LibraryDesc rt_shadow_so_library_transparency = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_shadow_lib; @@ -1147,10 +1179,23 @@ namespace wr return lib; }(); + + REGISTER(state_objects::rt_shadow_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, - .library_desc = rt_shadow_so_library, + .library_desc = rt_shadow_so_library_no_transparency, + .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), + .max_attributes_size = sizeof(float) * 4, + .max_recursion_depth = 1, + .global_root_signature = root_signatures::rt_hybrid_global, + .local_root_signatures = {} + }); + + REGISTER(state_objects::rt_shadow_state_object_transparency, RTPipelineRegistry)( + { + .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, + .library_desc = rt_shadow_so_library_transparency, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 1, @@ -1166,7 +1211,22 @@ namespace wr .defines = {} }); - StateObjectDescription::LibraryDesc rt_reflection_so_library = []() + StateObjectDescription::LibraryDesc rt_reflection_so_library_no_transparency = []() + { + StateObjectDescription::LibraryDesc lib; + lib.shader_handle = shaders::rt_reflection_lib; + lib.exports.push_back(L"ReflectionRaygenEntry"); + lib.exports.push_back(L"ReflectionHit"); + lib.exports.push_back(L"ReflectionMiss"); + lib.exports.push_back(L"ShadowClosestHitEntry"); + lib.exports.push_back(L"ShadowMissEntry"); + lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit" }); + lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry" }); + + return lib; + }(); + + StateObjectDescription::LibraryDesc rt_reflection_so_library_transparency = []() { StateObjectDescription::LibraryDesc lib; lib.shader_handle = shaders::rt_reflection_lib; @@ -1180,12 +1240,25 @@ namespace wr lib.m_hit_groups.push_back({ L"ReflectionHitGroup", L"ReflectionHit", L"ReflectionAnyHit" }); lib.m_hit_groups.push_back({ L"ShadowHitGroup", L"ShadowClosestHitEntry", L"ShadowAnyHitEntry" }); + return lib; }(); + REGISTER(state_objects::rt_reflection_state_object, RTPipelineRegistry)( { .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, - .library_desc = rt_reflection_so_library, + .library_desc = rt_reflection_so_library_no_transparency, + .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), + .max_attributes_size = sizeof(float) * 4, + .max_recursion_depth = 3, + .global_root_signature = root_signatures::rt_hybrid_global, + .local_root_signatures = {} + }); + + REGISTER(state_objects::rt_reflection_state_object_transparency, RTPipelineRegistry)( + { + .desc = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE, + .library_desc = rt_reflection_so_library_transparency, .max_payload_size = (sizeof(float) * 6) + (sizeof(unsigned int) * 2) + (sizeof(float) * 2), .max_attributes_size = sizeof(float) * 4, .max_recursion_depth = 3, diff --git a/src/engine_registry.hpp b/src/engine_registry.hpp index 3c073fe5..f5343c2e 100644 --- a/src/engine_registry.hpp +++ b/src/engine_registry.hpp @@ -817,11 +817,13 @@ namespace wr { WISPRENDERER_EXPORT static RegistryHandle state_object; WISPRENDERER_EXPORT static RegistryHandle rt_ao_state_object; - WISPRENDERER_EXPORT static RegistryHandle path_tracing_state_object; + WISPRENDERER_EXPORT static RegistryHandle rt_ao_state_object_transparency; + WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object; WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object_transparency; - WISPRENDERER_EXPORT static RegistryHandle path_tracer_state_object_no_transparency; WISPRENDERER_EXPORT static RegistryHandle rt_shadow_state_object; + WISPRENDERER_EXPORT static RegistryHandle rt_shadow_state_object_transparency; WISPRENDERER_EXPORT static RegistryHandle rt_reflection_state_object; + WISPRENDERER_EXPORT static RegistryHandle rt_reflection_state_object_transparency; }; } /* wr */ diff --git a/src/imgui_graphics_settings.hpp b/src/imgui_graphics_settings.hpp index 8520d6af..a0033d90 100644 --- a/src/imgui_graphics_settings.hpp +++ b/src/imgui_graphics_settings.hpp @@ -86,17 +86,23 @@ namespace wr::imgui::window ImGui::Begin("Acceleration Structure Settings", &asbuild_settings_open); - ImGui::Checkbox("Disable rebuilding", &as_build_user_settings.m_runtime.m_rebuild_as); + static bool rebuild_as = false; + static bool allow_transparency = false; - ImGui::Checkbox("Allow transparency", &as_build_user_settings.m_runtime.m_allow_transparency); + ImGui::Checkbox("Disable rebuilding", &rebuild_as); + + ImGui::Checkbox("Allow transparency", &allow_transparency); if(ImGui::Button("Rebuild BLAS")) { as_build_user_settings.m_runtime.m_rebuild_bot_level = true; + as_build_user_settings.m_runtime.m_rebuild_as = rebuild_as; + as_build_user_settings.m_runtime.m_allow_transparency = allow_transparency; + + frame_graph->UpdateSettings(as_build_user_settings); } ImGui::End(); - frame_graph->UpdateSettings(as_build_user_settings); } if (frame_graph->HasTask() && shadow_settings_open) diff --git a/src/material_pool.hpp b/src/material_pool.hpp index 293430b7..57584ef4 100644 --- a/src/material_pool.hpp +++ b/src/material_pool.hpp @@ -94,11 +94,6 @@ namespace wr m_material_data.m_constant_data[uint32_t(type) >> 16] = x; } - template - void SetConstant(typename std::enable_if::type x) { - m_material_data.m_constant_data[uint32_t(type) >> 16] = x; - } - template void SetConstant(const typename std::enable_if>::type& val) { float* arr = m_material_data.m_constant_data; diff --git a/src/render_tasks/d3d12_build_acceleration_structures.hpp b/src/render_tasks/d3d12_build_acceleration_structures.hpp index 3ef18e76..076395b4 100644 --- a/src/render_tasks/d3d12_build_acceleration_structures.hpp +++ b/src/render_tasks/d3d12_build_acceleration_structures.hpp @@ -53,6 +53,7 @@ namespace wr bool out_init = true; bool out_materials_require_update = true; + bool out_using_transparency = false; }; namespace internal @@ -154,7 +155,7 @@ namespace wr return obj; } - inline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair mesh_material, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) + inline void BuildBLASSingle(d3d12::Device* device, d3d12::CommandList* cmd_list, Model* model, std::pair mesh_material, ASBuildData& data, std::uint32_t frame_idx) { auto n_model_pool = static_cast(model->m_model_pool); auto vb = n_model_pool->GetVertexStagingBuffer(); @@ -170,7 +171,7 @@ namespace wr d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH - auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, settings.m_runtime.m_allow_transparency); + auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, data.out_using_transparency); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); @@ -183,7 +184,7 @@ namespace wr AppendOffset(data, n_mesh, material_id); } - inline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) + inline void BuildBLASList(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_material_handles.clear(); @@ -230,7 +231,7 @@ namespace wr d3d12::desc::GeometryDesc obj = CreateGeometryDescFromMesh(n_mesh, vb, ib); // Build Bottom level BVH - auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, settings.m_runtime.m_allow_transparency); + auto blas = d3d12::CreateBottomLevelAccelerationStructures(device, cmd_list, out_heap, { obj }, data.out_using_transparency); d3d12::UAVBarrierAS(cmd_list, blas, frame_idx); d3d12::SetName(blas, L"Bottom Level Acceleration Structure"); @@ -348,7 +349,7 @@ namespace wr return needs_reconstruction; }*/ - inline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, ASBuildSettings& settings, std::uint32_t frame_idx) + inline void UpdateTLAS(d3d12::Device* device, d3d12::CommandList* cmd_list, SceneGraph& scene_graph, ASBuildData& data, std::uint32_t frame_idx) { data.out_materials.clear(); data.out_offsets.clear(); @@ -402,7 +403,7 @@ namespace wr d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::VERTEX_AND_CONSTANT_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::INDEX_BUFFER, ResourceState::NON_PIXEL_SHADER_RESOURCE); - BuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, settings, frame_idx); + BuildBLASSingle(device, cmd_list, model, { mesh.first, material_handle }, data, frame_idx); d3d12::Transition(cmd_list, n_model_pool->GetVertexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::VERTEX_AND_CONSTANT_BUFFER); d3d12::Transition(cmd_list, n_model_pool->GetIndexStagingBuffer(), ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::INDEX_BUFFER); @@ -457,6 +458,7 @@ namespace wr auto frame_idx = n_render_system.GetFrameIdx(); data.out_materials_require_update = false; + data.out_using_transparency = settings.m_runtime.m_allow_transparency; data.current_frame_index = n_render_system.GetFrameIdx(); @@ -496,7 +498,7 @@ namespace wr } // List all materials used by meshes - internal::BuildBLASList(device, cmd_list, scene_graph, data, settings, frame_idx); + internal::BuildBLASList(device, cmd_list, scene_graph, data, frame_idx); data.out_tlas = d3d12::CreateTopLevelAccelerationStructure(device, cmd_list, out_heap, data.out_blas_list); d3d12::SetName(data.out_tlas, L"Top Level Acceleration Structure"); @@ -530,7 +532,7 @@ namespace wr data.old_tlas[data.current_frame_index] = {}; } - internal::UpdateTLAS(device, cmd_list, scene_graph, data, settings, frame_idx); + internal::UpdateTLAS(device, cmd_list, scene_graph, data, frame_idx); } data.previous_frame_index = n_render_system.GetFrameIdx(); diff --git a/src/render_tasks/d3d12_path_tracer.hpp b/src/render_tasks/d3d12_path_tracer.hpp index 302b8479..40bccfd2 100644 --- a/src/render_tasks/d3d12_path_tracer.hpp +++ b/src/render_tasks/d3d12_path_tracer.hpp @@ -155,6 +155,22 @@ namespace wr data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); data.tlas_requires_init = true; + + + // Camera constant buffer + data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); + + // Pipeline State Object + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::path_tracer_state_object_transparency + : state_objects::path_tracer_state_object)); + + // Create Shader Tables + for (int frame_idx = 0; frame_idx < d3d12::settings::num_back_buffers; ++frame_idx) + { + CreateShaderTables(device, data, frame_idx); + } } // Versioning @@ -179,21 +195,6 @@ namespace wr d3d12::CreateSRVFromDSV(deferred_main_rt, depth_handle); } - if (!resize) - { - // Camera constant buffer - data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); - - // Pipeline State Object - auto& rt_registry = RTPipelineRegistry::Get(); - data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_transparency)); - - // Create Shader Tables - CreateShaderTables(device, data, 0); - CreateShaderTables(device, data, 1); - CreateShaderTables(device, data, 2); - } - } inline void ExecutePathTracerTask(RenderSystem& render_system, FrameGraph& fg, SceneGraph& scene_graph, RenderTaskHandle& handle) @@ -223,11 +224,15 @@ namespace wr if (settings.m_runtime.m_allow_transparency) { - data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_transparency)); + // Pipeline State Object + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::path_tracer_state_object_transparency + : state_objects::path_tracer_state_object)); } else { - data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object_no_transparency)); + data.out_state_object = static_cast(rt_registry.Find(state_objects::path_tracer_state_object)); } // Rebuild acceleratrion structure a 2e time for fallback diff --git a/src/render_tasks/d3d12_rt_reflection_task.hpp b/src/render_tasks/d3d12_rt_reflection_task.hpp index caa7f401..f5262459 100644 --- a/src/render_tasks/d3d12_rt_reflection_task.hpp +++ b/src/render_tasks/d3d12_rt_reflection_task.hpp @@ -16,9 +16,8 @@ namespace wr { - struct RTReflectionData + struct RTReflectionData : RTHybrid_BaseData { - RTHybrid_BaseData base_data; }; namespace internal @@ -39,40 +38,40 @@ namespace wr // Get AS build data auto& as_build_data = fg.GetPredecessorData(); - data.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.tlas_requires_init = true; - } + data.tlas_requires_init = true; - CreateUAVsAndSRVs(fg, data.base_data, n_render_target); - if (!resize) - { // Camera constant buffer - data.base_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); + data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); - data.base_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_reflection_state_object)); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_reflection_state_object_transparency + : state_objects::rt_reflection_state_object)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); - data.base_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); + data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); } + CreateUAVsAndSRVs(fg, data, n_render_target); + // Create Shader Tables for (int i = 0; i < d3d12::settings::num_back_buffers; ++i) { - CreateShaderTables(device, data.base_data, "ReflectionRaygenEntry", + CreateShaderTables(device, data, "ReflectionRaygenEntry", { "ReflectionMiss", "ShadowMissEntry" }, { "ReflectionHitGroup", "ShadowHitGroup" }, i); } // Setup frame index - data.base_data.frame_idx = 0; + data.frame_idx = 0; } inline void ExecuteRTReflectionTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) @@ -93,16 +92,22 @@ namespace wr // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { - d3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx); + d3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx); d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); } if (n_render_system.m_render_window.has_value()) { - d3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_reflection_state_object_transparency + : state_objects::rt_reflection_state_object)); + + + d3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); // Bind output, indices and materials, offsets, etc - auto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(); + auto out_uav_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); @@ -114,13 +119,13 @@ namespace wr auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle); - auto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); + auto out_albedo_gbuffer_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle); - auto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle(); + auto out_normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle); - auto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); + auto out_scene_depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle); /* @@ -197,8 +202,8 @@ namespace wr cam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection); cam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.m_intensity = n_render_system.temp_intensity; - cam_data.m_frame_idx = static_cast(++data.base_data.frame_idx); - n_render_system.m_camera_pool->Update(data.base_data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool? + cam_data.m_frame_idx = static_cast(++data.frame_idx); + n_render_system.m_camera_pool->Update(data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool? // Make sure the convolution pass wrote to the skybox. fg.WaitForPredecessorTask(); @@ -223,11 +228,11 @@ namespace wr d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text); // Transition depth to NON_PIXEL_RESOURCE - d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); - d3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx); + d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { @@ -244,7 +249,7 @@ namespace wr d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); //#ifdef _DEBUG - CreateShaderTables(device, data.base_data, "ReflectionRaygenEntry", + CreateShaderTables(device, data, "ReflectionRaygenEntry", { "ReflectionMiss", "ShadowMissEntry" }, { "ReflectionHitGroup", "ShadowHitGroup" }, frame_idx); //#endif @@ -253,16 +258,16 @@ namespace wr // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, - data.base_data.out_hitgroup_shader_table[frame_idx], - data.base_data.out_miss_shader_table[frame_idx], - data.base_data.out_raygen_shader_table[frame_idx], + data.out_hitgroup_shader_table[frame_idx], + data.out_miss_shader_table[frame_idx], + data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(scalar * d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(scalar * d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE - d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } @@ -273,10 +278,10 @@ namespace wr if (!resize) { // Small hack to force the allocations to go out of scope, which will tell the allocator to free them - std::move(data.base_data.out_output_alloc); - std::move(data.base_data.out_gbuffer_albedo_alloc); - std::move(data.base_data.out_gbuffer_normal_alloc); - std::move(data.base_data.out_gbuffer_depth_alloc); + std::move(data.out_output_alloc); + std::move(data.out_gbuffer_albedo_alloc); + std::move(data.out_gbuffer_normal_alloc); + std::move(data.out_gbuffer_depth_alloc); } } diff --git a/src/render_tasks/d3d12_rt_shadow_task.hpp b/src/render_tasks/d3d12_rt_shadow_task.hpp index 3e27b4d4..972de711 100644 --- a/src/render_tasks/d3d12_rt_shadow_task.hpp +++ b/src/render_tasks/d3d12_rt_shadow_task.hpp @@ -28,9 +28,8 @@ namespace wr Runtime m_runtime; }; - struct RTShadowData + struct RTShadowData : RTHybrid_BaseData { - RTHybrid_BaseData base_data; }; namespace internal @@ -51,41 +50,40 @@ namespace wr // Get AS build data auto& as_build_data = fg.GetPredecessorData(); - data.base_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_albedo_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate()); + data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate()); - data.base_data.tlas_requires_init = true; - } - - // Versioning - CreateUAVsAndSRVs(fg, data.base_data, n_render_target); + data.tlas_requires_init = true; - if (!resize) - { // Camera constant buffer - data.base_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); + data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTHybridCamera_CBData))); // Pipeline State Object auto& rt_registry = RTPipelineRegistry::Get(); - data.base_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_shadow_state_object)); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_shadow_state_object_transparency + : state_objects::rt_shadow_state_object)); // Root Signature auto& rs_registry = RootSignatureRegistry::Get(); - data.base_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); + data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_hybrid_global)); } + // Versioning + CreateUAVsAndSRVs(fg, data, n_render_target); + // Create Shader Tables for (int i = 0; i < d3d12::settings::num_back_buffers; ++i) { - CreateShaderTables(device, data.base_data, "ShadowRaygenEntry", + CreateShaderTables(device, data, "ShadowRaygenEntry", { "ShadowMissEntry" }, { "ShadowHitGroup" }, i); } // Setup frame index - data.base_data.frame_idx = 0; + data.frame_idx = 0; } inline void ExecuteRTShadowTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) @@ -108,16 +106,21 @@ namespace wr // Rebuild acceleratrion structure a 2e time for fallback if (d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK) { - d3d12::CreateOrUpdateTLAS(device, cmd_list, data.base_data.tlas_requires_init, data.base_data.out_tlas, as_build_data.out_blas_list, frame_idx); + d3d12::CreateOrUpdateTLAS(device, cmd_list, data.tlas_requires_init, data.out_tlas, as_build_data.out_blas_list, frame_idx); d3d12::UAVBarrierAS(cmd_list, as_build_data.out_tlas, frame_idx); } if (n_render_system.m_render_window.has_value()) { - d3d12::BindRaytracingPipeline(cmd_list, data.base_data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_shadow_state_object_transparency + : state_objects::rt_shadow_state_object)); + + d3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); // Bind output, indices and materials, offsets, etc - auto out_uav_handle = data.base_data.out_output_alloc.GetDescriptorHandle(); + auto out_uav_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OUTPUT)), out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); @@ -129,13 +132,13 @@ namespace wr auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::OFFSETS)), out_scene_offset_handle); - auto out_albedo_gbuffer_handle = data.base_data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); + auto out_albedo_gbuffer_handle = data.out_gbuffer_albedo_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 0, out_albedo_gbuffer_handle); - auto out_normal_gbuffer_handle = data.base_data.out_gbuffer_normal_alloc.GetDescriptorHandle(); + auto out_normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 1, out_normal_gbuffer_handle); - auto out_scene_depth_handle = data.base_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); + auto out_scene_depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::GBUFFERS)) + 2, out_scene_depth_handle); /* @@ -212,10 +215,10 @@ namespace wr cam_data.m_inverse_projection = DirectX::XMMatrixInverse(nullptr, camera->m_projection); cam_data.m_inv_vp = DirectX::XMMatrixInverse(nullptr, camera->m_view * camera->m_projection); cam_data.m_intensity = n_render_system.temp_intensity; - cam_data.m_frame_idx = static_cast(++data.base_data.frame_idx); + cam_data.m_frame_idx = static_cast(++data.frame_idx); cam_data.m_epsilon = settings.m_runtime.m_epsilon; cam_data.m_sample_count = settings.m_runtime.m_sample_count; - n_render_system.m_camera_pool->Update(data.base_data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool? + n_render_system.m_camera_pool->Update(data.out_cb_camera_handle, sizeof(temp::RTHybridCamera_CBData), 0, frame_idx, (std::uint8_t*) & cam_data); // FIXME: Uhh wrong pool? // Make sure the convolution pass wrote to the skybox. fg.WaitForPredecessorTask(); @@ -240,11 +243,11 @@ namespace wr d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_hybrid, params::RTHybridE::BRDF_LUT)), brdf_lut_text); // Transition depth to NON_PIXEL_RESOURCE - d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); d3d12::BindDescriptorHeaps(cmd_list, d3d12::GetRaytracingType(device) == RaytracingType::FALLBACK); - d3d12::BindComputeConstantBuffer(cmd_list, data.base_data.out_cb_camera_handle->m_native, 2, frame_idx); + d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx); if (d3d12::GetRaytracingType(device) == RaytracingType::NATIVE) { @@ -261,7 +264,7 @@ namespace wr d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); //#ifdef _DEBUG - CreateShaderTables(device, data.base_data, "ShadowRaygenEntry", + CreateShaderTables(device, data, "ShadowRaygenEntry", { "ShadowMissEntry" }, { "ShadowHitGroup" }, frame_idx); //#endif @@ -270,16 +273,16 @@ namespace wr // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, - data.base_data.out_hitgroup_shader_table[frame_idx], - data.base_data.out_miss_shader_table[frame_idx], - data.base_data.out_raygen_shader_table[frame_idx], + data.out_hitgroup_shader_table[frame_idx], + data.out_miss_shader_table[frame_idx], + data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(scalar * d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(scalar * d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE - d3d12::TransitionDepth(cmd_list, data.base_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } @@ -290,10 +293,10 @@ namespace wr if (!resize) { // Small hack to force the allocations to go out of scope, which will tell the allocator to free them - std::move(data.base_data.out_output_alloc); - std::move(data.base_data.out_gbuffer_albedo_alloc); - std::move(data.base_data.out_gbuffer_normal_alloc); - std::move(data.base_data.out_gbuffer_depth_alloc); + std::move(data.out_output_alloc); + std::move(data.out_gbuffer_albedo_alloc); + std::move(data.out_gbuffer_normal_alloc); + std::move(data.out_gbuffer_depth_alloc); } } diff --git a/src/render_tasks/d3d12_rtao_task.hpp b/src/render_tasks/d3d12_rtao_task.hpp index 76064bee..b4f0ad88 100644 --- a/src/render_tasks/d3d12_rtao_task.hpp +++ b/src/render_tasks/d3d12_rtao_task.hpp @@ -30,9 +30,8 @@ namespace wr Runtime m_runtime; }; - struct RTAOData + struct RTAOData : RTHybrid_BaseData { - RTHybrid_BaseData base_data; }; namespace internal @@ -46,52 +45,62 @@ namespace wr auto n_render_target = fg.GetRenderTarget(handle); d3d12::SetName(n_render_target, L"AO Target"); - RTHybrid_BaseData& h_data = data.base_data; - if (!resize) { auto& as_build_data = fg.GetPredecessorData(); - h_data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(1)); - h_data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate(1)); - h_data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + data.out_output_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate(1)); + + + // Camera constant buffer + data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); + + // Pipeline State Object + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_ao_state_object_transparency + : state_objects::rt_ao_state_object)); + + // Root Signature + auto& rs_registry = RootSignatureRegistry::Get(); + data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_ao_global)); + + if (as_build_data.out_using_transparency) + { + // Create Shader Tables + for (int frame_idx = 0; frame_idx < d3d12::settings::num_back_buffers; ++frame_idx) + { + CreateShaderTables(device, data, "AORaygenEntry_Transparency", { "AOMissEntry" }, { "AOHitGroup" }, frame_idx); + } + } + else + { + // Create Shader Tables + for (int frame_idx = 0; frame_idx < d3d12::settings::num_back_buffers; ++frame_idx) + { + CreateShaderTables(device, data, "AORaygenEntry", { "AOMissEntry" }, { }, frame_idx); + } + } } // Versioning for (int frame_idx = 0; frame_idx < 1; ++frame_idx) { // Bind output texture - d3d12::DescHeapCPUHandle rtv_handle = h_data.out_output_alloc.GetDescriptorHandle(); + d3d12::DescHeapCPUHandle rtv_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::CreateUAVFromSpecificRTV(n_render_target, rtv_handle, 0, n_render_target->m_create_info.m_rtv_formats[0]); // Bind g-buffers - d3d12::DescHeapCPUHandle normal_gbuffer_handle = h_data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); - d3d12::DescHeapCPUHandle depth_buffer_handle = h_data.out_gbuffer_depth_alloc.GetDescriptorHandle(0); + d3d12::DescHeapCPUHandle normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); + d3d12::DescHeapCPUHandle depth_buffer_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(0); - auto deferred_main_rt = h_data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); + auto deferred_main_rt = data.out_deferred_main_rt = static_cast(fg.GetPredecessorRenderTarget()); d3d12::CreateSRVFromSpecificRTV(deferred_main_rt, normal_gbuffer_handle, 1, deferred_main_rt->m_create_info.m_rtv_formats.data()[1]); d3d12::CreateSRVFromDSV(deferred_main_rt, depth_buffer_handle); } - - if (!resize) - { - // Camera constant buffer - h_data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); - - // Pipeline State Object - auto& rt_registry = RTPipelineRegistry::Get(); - h_data.out_state_object = static_cast(rt_registry.Find(state_objects::rt_ao_state_object)); - - // Root Signature - auto& rs_registry = RootSignatureRegistry::Get(); - h_data.out_root_signature = static_cast(rs_registry.Find(root_signatures::rt_ao_global)); - - // Create Shader Tables - CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, { "AOHitGroup" }, 0); - CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, { "AOHitGroup" }, 1); - CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, { "AOHitGroup" }, 2); - } } inline void ExecuteAOTask(RenderSystem & render_system, FrameGraph & fg, SceneGraph & scene_graph, RenderTaskHandle & handle) @@ -109,14 +118,17 @@ namespace wr fg.WaitForPredecessorTask(); float scalar = 1.0f; - RTHybrid_BaseData& h_data = data.base_data; - if (n_render_system.m_render_window.has_value()) { - d3d12::BindRaytracingPipeline(cmd_list, h_data.out_state_object, false); + auto& rt_registry = RTPipelineRegistry::Get(); + data.out_state_object = static_cast(rt_registry.Find(as_build_data.out_using_transparency + ? state_objects::rt_ao_state_object_transparency + : state_objects::rt_ao_state_object)); + + d3d12::BindRaytracingPipeline(cmd_list, data.out_state_object, false); // Bind output, indices and materials, offsets, etc - auto out_uav_handle = h_data.out_output_alloc.GetDescriptorHandle(); + auto out_uav_handle = data.out_output_alloc.GetDescriptorHandle(); d3d12::SetRTShaderUAV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::OUTPUT)), out_uav_handle); auto out_scene_ib_handle = as_build_data.out_scene_ib_alloc.GetDescriptorHandle(); @@ -128,10 +140,10 @@ namespace wr auto out_scene_offset_handle = as_build_data.out_scene_offset_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::OFFSETS)), out_scene_offset_handle); - auto out_normal_gbuffer_handle = h_data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); + auto out_normal_gbuffer_handle = data.out_gbuffer_normal_alloc.GetDescriptorHandle(0); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 0, out_normal_gbuffer_handle); - auto out_scene_depth_handle = h_data.out_gbuffer_depth_alloc.GetDescriptorHandle(); + auto out_scene_depth_handle = data.out_gbuffer_depth_alloc.GetDescriptorHandle(); d3d12::SetRTShaderSRV(cmd_list, 0, COMPILATION_EVAL(rs_layout::GetHeapLoc(params::rt_ao, params::RTAOE::GBUFFERS)) + 1, out_scene_depth_handle); @@ -200,14 +212,14 @@ namespace wr cb_data.m_frame_idx = frame_idx; cb_data.m_sample_count = static_cast(settings.m_runtime.sample_count); - n_render_system.m_camera_pool->Update(h_data.out_cb_camera_handle, sizeof(temp::RTAO_CBData), 0, frame_idx, (std::uint8_t*)& cb_data); // FIXME: Uhh wrong pool? + n_render_system.m_camera_pool->Update(data.out_cb_camera_handle, sizeof(temp::RTAO_CBData), 0, frame_idx, (std::uint8_t*)& cb_data); // FIXME: Uhh wrong pool? // Transition depth to NON_PIXEL_RESOURCE - d3d12::TransitionDepth(cmd_list, h_data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::DEPTH_WRITE, ResourceState::NON_PIXEL_SHADER_RESOURCE); d3d12::BindDescriptorHeap(cmd_list, cmd_list->m_rt_descriptor_heap.get()->GetHeap(), DescriptorHeapType::DESC_HEAP_TYPE_CBV_SRV_UAV, frame_idx, false); d3d12::BindDescriptorHeaps(cmd_list, false); - d3d12::BindComputeConstantBuffer(cmd_list, h_data.out_cb_camera_handle->m_native, 2, frame_idx); + d3d12::BindComputeConstantBuffer(cmd_list, data.out_cb_camera_handle->m_native, 2, frame_idx); if (!as_build_data.out_blas_list.empty()) { @@ -217,23 +229,30 @@ namespace wr d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); #ifdef _DEBUG - CreateShaderTables(device, h_data, "AORaygenEntry", { "AOMissEntry" }, { "AOHitGroup" }, frame_idx); + if (as_build_data.out_using_transparency) + { + CreateShaderTables(device, data, "AORaygenEntry_Transparency", { "AOMissEntry" }, { "AOHitGroup" }, frame_idx); + } + else + { + CreateShaderTables(device, data, "AORaygenEntry", { "AOMissEntry" }, { }, frame_idx); + } #endif // _DEBUG scalar = fg.GetRenderTargetResolutionScale(handle); // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list, - h_data.out_hitgroup_shader_table[frame_idx], - h_data.out_miss_shader_table[frame_idx], - h_data.out_raygen_shader_table[frame_idx], + data.out_hitgroup_shader_table[frame_idx], + data.out_miss_shader_table[frame_idx], + data.out_raygen_shader_table[frame_idx], static_cast(std::ceil(scalar * d3d12::GetRenderTargetWidth(render_target))), static_cast(std::ceil(scalar * d3d12::GetRenderTargetHeight(render_target))), 1, frame_idx); // Transition depth back to DEPTH_WRITE - d3d12::TransitionDepth(cmd_list, h_data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); + d3d12::TransitionDepth(cmd_list, data.out_deferred_main_rt, ResourceState::NON_PIXEL_SHADER_RESOURCE, ResourceState::DEPTH_WRITE); } } @@ -244,9 +263,9 @@ namespace wr if (!resize) { // Small hack to force the allocations to go out of scope, which will tell the allocator to free them - DescriptorAllocation temp1 = std::move(data.base_data.out_output_alloc); - DescriptorAllocation temp2 = std::move(data.base_data.out_gbuffer_normal_alloc); - DescriptorAllocation temp3 = std::move(data.base_data.out_gbuffer_depth_alloc); + DescriptorAllocation temp1 = std::move(data.out_output_alloc); + DescriptorAllocation temp2 = std::move(data.out_gbuffer_normal_alloc); + DescriptorAllocation temp3 = std::move(data.out_gbuffer_depth_alloc); } } } diff --git a/tests/demo/demo.cpp b/tests/demo/demo.cpp index 753809ef..8cc87ccf 100644 --- a/tests/demo/demo.cpp +++ b/tests/demo/demo.cpp @@ -29,7 +29,7 @@ #include "model_loader_tinygltf.hpp" #include "d3d12/d3d12_dynamic_descriptor_heap.hpp" -using DefaultScene = ViknellScene; +using DefaultScene = TransparencyScene; //#define ENABLE_PHYSICS std::unique_ptr render_system; From fad5a33c95b68c217bcac4e034ca2b3ba4bb35ea Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Thu, 20 Jun 2019 16:15:36 +0200 Subject: [PATCH 11/12] [CHANGE] Re-named function as requested in the PR --- resources/shaders/dxr_ambient_occlusion.hlsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/shaders/dxr_ambient_occlusion.hlsl b/resources/shaders/dxr_ambient_occlusion.hlsl index 6092cbb5..687fa7f7 100644 --- a/resources/shaders/dxr_ambient_occlusion.hlsl +++ b/resources/shaders/dxr_ambient_occlusion.hlsl @@ -83,7 +83,7 @@ bool TraceAORay(uint idx, float3 origin, float3 direction, float far, unsigned i return payload.is_hit; } -bool TraceAORay_Optimized(uint idx, float3 origin, float3 direction, float far, unsigned int depth) +bool TraceAORay_SkipClosestHit(uint idx, float3 origin, float3 direction, float far, unsigned int depth) { // Define a ray, consisting of origin, direction, and the min-max distance values RayDesc ray; @@ -133,7 +133,7 @@ void AORaygenEntry() int ao_value = sample_count; for (uint i = 0; i < spp; i++) { - ao_value -= TraceAORay_Optimized(0, wpos + normal * bias, getCosHemisphereSample(rand_seed, normal), radius, 0); + ao_value -= TraceAORay_SkipClosestHit(0, wpos + normal * bias, getCosHemisphereSample(rand_seed, normal), radius, 0); } output[DispatchRaysIndex().xy].x = pow(ao_value / float(sample_count), power); } From 1090d5e32cf757e51aba84d8049ae16809b369da Mon Sep 17 00:00:00 2001 From: LaisoEmilio <162008@buas.nl> Date: Sat, 22 Jun 2019 13:30:07 +0200 Subject: [PATCH 12/12] [CHANGE] Merged with master. Also, ifdef DEBUG in ambient occlusion task was preventing the shader tables to be created therefore crashing the renderer in release mode. Fixed that, as well as renaming padding --- resources/shaders/dxr_ambient_occlusion.hlsl | 3 +-- src/render_tasks/d3d12_rtao_task.hpp | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/resources/shaders/dxr_ambient_occlusion.hlsl b/resources/shaders/dxr_ambient_occlusion.hlsl index 687fa7f7..ae2ee612 100644 --- a/resources/shaders/dxr_ambient_occlusion.hlsl +++ b/resources/shaders/dxr_ambient_occlusion.hlsl @@ -40,7 +40,7 @@ typedef BuiltInTriangleIntersectionAttributes Attributes; struct AOHitInfo { float is_hit; - float padding; + float padding_for_fallback; }; cbuffer CBData : register(b0) @@ -193,7 +193,6 @@ void AOMissEntry(inout AOHitInfo hit : SV_RayPayload) [shader("anyhit")] void AOAnyHitEntry(inout AOHitInfo hit, in Attributes attr) { -//#define FALLBACK #ifndef FALLBACK // Calculate the essentials const Offset offset = g_offsets[InstanceID()]; diff --git a/src/render_tasks/d3d12_rtao_task.hpp b/src/render_tasks/d3d12_rtao_task.hpp index d9e84d59..4a19503d 100644 --- a/src/render_tasks/d3d12_rtao_task.hpp +++ b/src/render_tasks/d3d12_rtao_task.hpp @@ -68,7 +68,6 @@ namespace wr data.out_gbuffer_normal_alloc = std::move(as_build_data.out_allocator->Allocate(1)); data.out_gbuffer_depth_alloc = std::move(as_build_data.out_allocator->Allocate(1)); - // Camera constant buffer data.out_cb_camera_handle = static_cast(n_render_system.m_raytracing_cb_pool->Create(sizeof(temp::RTAO_CBData))); @@ -243,7 +242,6 @@ namespace wr d3d12::BindComputeShaderResourceView(cmd_list, as_build_data.out_scene_vb->m_buffer, 3); -#ifdef _DEBUG if (as_build_data.out_using_transparency) { CreateShaderTables(device, data, "AORaygenEntry_Transparency", { "AOMissEntry" }, { "AOHitGroup" }, frame_idx); @@ -252,7 +250,6 @@ namespace wr { CreateShaderTables(device, data, "AORaygenEntry", { "AOMissEntry" }, { }, frame_idx); } -#endif // _DEBUG // Dispatch hybrid ray tracing rays d3d12::DispatchRays(cmd_list,