17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 12:09:19 +02:00
Files
maszyna/betterRenderer/renderer/source/contactshadows.cpp
maj00r 26501b0fd7 hoist standard includes above everything else
Ordering them before the imports in the same file was not enough: a
project header on line 1 can import a module several levels down whose
global module fragment already pulled in the same standard header, and
that include guard does not cross the module boundary. Putting them
first means the guard is set before anything can import.

Skips vendored single-header libraries and anything inside an #if.
2026-09-10 01:02:17 +02:00

78 lines
3.3 KiB
C++

#include <cstdint>
#include "contactshadows.h"
#include <nvrhi/utils.h>
#include "gbuffer.h"
#include "nvrenderer/nvrenderer.h"
#include "nvrendererbackend.h"
MaContactShadows::MaContactShadows(NvRenderer* renderer, NvGbuffer* gbuffer)
: m_backend(renderer->GetBackend()),
m_gbuffer(gbuffer),
m_width(0),
m_height(0) {}
void MaContactShadows::Init() {
m_width = m_gbuffer->m_gbuffer_depth->getDesc().width;
m_height = m_gbuffer->m_gbuffer_depth->getDesc().height;
m_compute_shader = m_backend->CreateShader("contact_shadows",
nvrhi::ShaderType::Compute);
m_constant_buffer = m_backend->GetDevice()->createBuffer(
nvrhi::utils::CreateStaticConstantBufferDesc(sizeof(ComputeConstants),
"Contact shadow constants")
.setInitialState(nvrhi::ResourceStates::ConstantBuffer)
.setKeepInitialState(true));
m_output_texture = m_backend->GetDevice()->createTexture(
nvrhi::TextureDesc()
.setFormat(nvrhi::Format::R8_UNORM)
.setWidth(m_width)
.setHeight(m_height)
.setIsUAV(true)
.setInitialState(nvrhi::ResourceStates::ShaderResource)
.setKeepInitialState(true));
nvrhi::utils::CreateBindingSetAndLayout(
m_backend->GetDevice(), nvrhi::ShaderType::Compute, 0,
nvrhi::BindingSetDesc()
.addItem(
nvrhi::BindingSetItem::Texture_SRV(0, m_gbuffer->m_gbuffer_depth))
.addItem(nvrhi::BindingSetItem::Texture_UAV(0, m_output_texture))
.addItem(nvrhi::BindingSetItem::ConstantBuffer(0, m_constant_buffer))
.addItem(nvrhi::BindingSetItem::Sampler(
0, m_backend->GetDevice()->createSampler(
nvrhi::SamplerDesc()
.setAllFilters(true)
.setAllAddressModes(
nvrhi::SamplerAddressMode::ClampToEdge)
.setReductionType(
nvrhi::SamplerReductionType::Maximum)))),
m_binding_layout, m_binding_set);
m_pipeline = m_backend->GetDevice()->createComputePipeline(
nvrhi::ComputePipelineDesc()
.setComputeShader(m_compute_shader)
.addBindingLayout(m_binding_layout));
}
void MaContactShadows::Render(nvrhi::ICommandList* command_list) {
command_list->setComputeState(nvrhi::ComputeState()
.setPipeline(m_pipeline)
.addBindingSet(m_binding_set));
command_list->dispatch((m_width + 7) / 8, (m_height + 7) / 8, 1);
}
void MaContactShadows::UpdateConstants(nvrhi::ICommandList* command_list,
const glm::dmat4& projection,
const glm::dmat4& view,
const glm::dvec3& light,
uint64_t frame_index) {
ComputeConstants constants{};
constants.g_InverseProjection = glm::inverse(projection);
constants.g_Projection = projection;
constants.g_LightDirView = view * glm::dvec4(-light, 0.);
constants.g_NumSamples = 16;
constants.g_SampleRange = .1;
constants.g_Thickness = .01;
constants.g_FrameIndex = frame_index % 64;
command_list->writeBuffer(m_constant_buffer, &constants, sizeof(constants));
}