void PrefixSumClusters::prefixSum( Device& device, CommandList& cmd, BufferSRV& clusters, BufferSRV& clusterCounts, Buffer& args, BufferUAV& results) { if (!m_bucketBufferUAV.valid() || m_bucketBufferUAV.desc().elements != clusters.desc().elements) { m_bucketBufferUAV = device.createBufferUAV(BufferDescription() .elementSize(sizeof(uint32_t)) .elements(clusters.desc().elements) .format(Format::R32_UINT) .usage(ResourceUsage::GpuReadWrite) .name("PrefixSumClusters bucket buffer (uint)")); m_bucketBufferSRV = device.createBufferSRV(m_bucketBufferUAV.buffer()); m_bucketSumBufferUAV = device.createBufferUAV(BufferDescription() .elementSize(sizeof(uint32_t)) .elements(clusters.desc().elements) .format(Format::R32_UINT) .usage(ResourceUsage::GpuReadWrite) .name("PrefixSumClusters bucket buffer (uint)")); m_bucketSumBufferSRV = device.createBufferSRV(m_bucketSumBufferUAV.buffer()); } m_buckets.cs.clusterCountBuffer = clusterCounts; m_buckets.cs.frustumCullingOutput = clusters; m_buckets.cs.output = m_bucketBufferUAV; cmd.bindPipe(m_buckets); cmd.dispatchIndirect(args, 0); m_bucketResults.cs.clusterCountBuffer = clusterCounts; m_bucketResults.cs.input = m_bucketBufferSRV; m_bucketResults.cs.output = m_bucketSumBufferUAV; cmd.bindPipe(m_bucketResults); cmd.dispatchIndirect(args, 0); m_addbuckets.cs.clusterCountBuffer = clusterCounts; m_addbuckets.cs.input1 = m_bucketBufferSRV; m_addbuckets.cs.input2 = m_bucketSumBufferSRV; m_addbuckets.cs.output = results; cmd.bindPipe(m_addbuckets); cmd.dispatchIndirect(args, 0); }
void LightData::updateLightInfo(Device& device, CommandList& commandList, const std::vector<FlatSceneLightNode>& lights) { bool sizeChanged = static_cast<uint32_t>(lights.size()) != m_lightCount; std::vector<Vector4f> positions; std::vector<Vector4f> directions; std::vector<Vector4f> colors; std::vector<float> intensities; std::vector<float> ranges; std::vector<unsigned int> types; std::vector<Vector4f> parameters; bool positionsChanged = false; bool directionsChanged = false; bool colorsChanged = false; bool intensitiesChanged = false; bool parametersChanged = false; bool rangesChanged = false; bool typesChanged = false; for (auto&& light : lights) { positionsChanged |= light.positionChanged; directionsChanged |= light.rotationChanged; colorsChanged |= light.light->colorChanged(true); intensitiesChanged |= light.light->intensityChanged(true); rangesChanged |= light.light->rangeChanged(true); typesChanged |= light.light->lightTypeChanged(true); parametersChanged |= light.light->lightParametersChanged(true); } m_changeHappened = //positionsChanged | //directionsChanged | //colorsChanged | //intensitiesChanged | //rangesChanged | typesChanged | parametersChanged; m_transforms.clear(); m_positions.clear(); m_directions.clear(); m_ranges.clear(); m_types.clear(); m_shadowCaster.clear(); for (auto&& light : lights) { if (sizeChanged || positionsChanged) positions.emplace_back(Vector4f(light.position, 1.0f)); if (sizeChanged || directionsChanged) directions.emplace_back(Vector4f(light.direction, 1.0f)); if (sizeChanged || colorsChanged) colors.emplace_back(Vector4f(light.light->color(), 1.0f)); if (sizeChanged || intensitiesChanged) intensities.emplace_back(light.light->intensity()); if (sizeChanged || rangesChanged) ranges.emplace_back(light.light->range()); if (sizeChanged || typesChanged) types.emplace_back(static_cast<unsigned int>(light.light->lightType())); if (sizeChanged || parametersChanged) parameters.emplace_back(light.light->parameters()); m_transforms.emplace_back(light.transform); m_positions.emplace_back(light.position); m_directions.emplace_back(light.direction); m_ranges.emplace_back(light.range); m_types.emplace_back(static_cast<unsigned int>(light.type)); m_shadowCaster.emplace_back(light.shadowCaster); } if (sizeChanged) { m_lightCount = static_cast<uint32_t>(lights.size()); if (m_lightCount > 0) { m_lightWorlPositions = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightPositions") .format(Format::R32G32B32A32_FLOAT) .setInitialData(BufferDescription::InitialData(positions)))); m_lightDirections = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightDirections") .format(Format::R32G32B32A32_FLOAT) .setInitialData(BufferDescription::InitialData(directions)))); m_lightColors = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightColors") .format(Format::R32G32B32A32_FLOAT) .setInitialData(BufferDescription::InitialData(colors)))); m_lightIntensities = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightIntensities") .format(Format::R32_FLOAT) .setInitialData(BufferDescription::InitialData(intensities)))); m_lightRanges = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightRanges") .format(Format::R32_FLOAT) .setInitialData(BufferDescription::InitialData(ranges)))); m_lightTypes = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightTypes") .format(Format::R32_UINT) .setInitialData(BufferDescription::InitialData(types)))); m_lightParameters = std::make_shared<BufferSRV>(device.createBufferSRV(BufferDescription() .name("lightParameters") .format(Format::R32G32B32A32_FLOAT) .setInitialData(BufferDescription::InitialData(parameters)))); m_saved.push(*m_lightWorlPositions); m_saved.push(*m_lightDirections); m_saved.push(*m_lightColors); m_saved.push(*m_lightIntensities); m_saved.push(*m_lightRanges); m_saved.push(*m_lightTypes); m_saved.push(*m_lightParameters); } else { m_lightWorlPositions = nullptr; m_lightDirections = nullptr; m_lightColors = nullptr; m_lightIntensities = nullptr; m_lightRanges = nullptr; m_lightTypes = nullptr; m_lightParameters = nullptr; } } else { if (m_lightCount == 0) { m_lightWorlPositions = nullptr; m_lightDirections = nullptr; m_lightColors = nullptr; m_lightIntensities = nullptr; m_lightRanges = nullptr; m_lightTypes = nullptr; m_lightParameters = nullptr; } else { // update existing textures if (positionsChanged) device.uploadBuffer(commandList, *m_lightWorlPositions, ByteRange{ positions }); if (directionsChanged) device.uploadBuffer(commandList, *m_lightDirections, ByteRange{ directions }); if (colorsChanged) device.uploadBuffer(commandList, *m_lightColors, ByteRange{ colors }); if (intensitiesChanged) device.uploadBuffer(commandList, *m_lightIntensities, ByteRange{ intensities }); if (rangesChanged) device.uploadBuffer(commandList, *m_lightRanges, ByteRange{ ranges }); if (typesChanged) device.uploadBuffer(commandList, *m_lightTypes, ByteRange{ types }); if (parametersChanged) device.uploadBuffer(commandList, *m_lightParameters, ByteRange{ parameters }); } } while (m_saved.size() > 14) { m_saved.pop(); } }
Core::Core(Device& device, ShaderStorage& shaderStorage, const std::string& shaderRootPath) : m_linearClamp{ SamplerDescription().filter(Filter::Bilinear) .textureAddressMode(TextureAddressMode::Clamp) } , m_pointClamp{ SamplerDescription().filter(Filter::Point) .textureAddressMode(TextureAddressMode::Clamp) } //, rootSignature{ device.createRootSignature() } /*, mainPipeline{ device.createPipeline( shaderStorage.loadShader(device, shaderRootPath + "vert.spv"), shaderStorage.loadShader(device, shaderRootPath + "frag.spv") ) }*/ , mainPipeline{ device.createPipeline<shaders::HLSLTest>(shaderStorage) } , uniformBuffer{ device.createBuffer(BufferDescription() .structured(true) .elements(1000) .usage(ResourceUsage::CpuToGpu) .name("someBuffer") .elementSize(sizeof(UniformBufferObject))) } //, descriptorHeap{ device.createDescriptorHeap({ DescriptorType::UniformBuffer, DescriptorType::CombinedImageSampler }) } { /*InputElementDescription vertDesc; vertDesc.alignedByteOffset(sizeof(Vertex)) .format(Format::Format_R32G32B32_FLOAT) .inputSlot(0) .inputSlotClass(InputClassification::PerVertexData) .offset(offsetof(Vertex, pos)); InputElementDescription colorDesc; colorDesc.alignedByteOffset(sizeof(Vertex)) .format(Format::Format_R32G32B32_FLOAT) .inputSlot(0) .inputSlotClass(InputClassification::PerVertexData) .offset(offsetof(Vertex, color)); InputElementDescription uvDesc; uvDesc.alignedByteOffset(sizeof(Vertex)) .format(Format::Format_R32G32_FLOAT) .inputSlot(0) .inputSlotClass(InputClassification::PerVertexData) .offset(offsetof(Vertex, texCoord)); std::vector<InputElementDescription> layoutDescs; layoutDescs.emplace_back(vertDesc); layoutDescs.emplace_back(colorDesc); layoutDescs.emplace_back(uvDesc); mainPipeline->setInputLayout(static_cast<unsigned int>(layoutDescs.size()), layoutDescs.data());*/ mainPipeline.setUniformBuffer(uniformBuffer); mainPipeline.setPrimitiveTopologyType(PrimitiveTopologyType::TriangleList); /*mainPipeline->setVertexShader(shaderStorage.loadShader(device, shaderRootPath + "vert.spv")); mainPipeline->setPixelShader(shaderStorage.loadShader(device, shaderRootPath + "frag.spv"));*/ /*m_rootSignature.reset(4, 2); m_rootSignature.initStaticSampler(0, m_linearClamp, ShaderVisibility::Pixel); m_rootSignature.initStaticSampler(1, m_pointClamp, ShaderVisibility::Pixel); m_rootSignature[0].initAsDescriptorRange(DescriptorRangeType::SRV, 0, 2, ShaderVisibility::Pixel); m_rootSignature[1].initAsConstants(0, 6, ShaderVisibility::Pixel); m_rootSignature[2].initAsSRV(2, ShaderVisibility::Vertex); m_rootSignature[3].initAsDescriptorRange(DescriptorRangeType::UAV, 0, 1, ShaderVisibility::Pixel); m_rootSignature.finalize(); m_blendUIPipelineState.setRootSignature(m_rootSignature); m_blendUIPipelineState.setRasterizerState(RasterizerDescription().cullMode(CullMode::None)); m_blendUIPipelineState.setBlendState( BlendDescription() .renderTarget(0, RenderTargetBlendDescription() .renderTargetWriteMask(static_cast<unsigned char>(ColorWriteEnable::All)) .blendEnable(true) .srcBlend(Blend::One))); m_blendUIPipelineState.setDepthStencilState(DepthStencilDescription() .depthEnable(false) .depthWriteMask(DepthWriteMask::Zero) .depthFunc(ComparisonFunction::Always) .frontFace({ StencilOp::Keep, StencilOp::Keep, StencilOp::Keep, ComparisonFunction::Always }) .backFace({ StencilOp::Keep, StencilOp::Keep, StencilOp::Keep, ComparisonFunction::Always }) ); m_blendUIPipelineState.setSampleMask(0xFFFFFFFF); InputElementDescription vertDesc; vertDesc.alignedByteOffset(sizeof(Vertex)) .format(Format::Format_R32G32_FLOAT) .inputSlot(0) .inputSlotClass(InputClassification::PerVertexData) .offset(offsetof(Vertex, pos)); InputElementDescription colorDesc; colorDesc.alignedByteOffset(sizeof(Vertex)) .format(Format::Format_R32G32B32_FLOAT) .inputSlot(0) .inputSlotClass(InputClassification::PerVertexData) .offset(offsetof(Vertex, color)); std::vector<InputElementDescription> layoutDescs; layoutDescs.emplace_back(vertDesc); layoutDescs.emplace_back(colorDesc); m_blendUIPipelineState.setInputLayout(static_cast<unsigned int>(layoutDescs.size()), layoutDescs.data()); m_blendUIPipelineState.setPrimitiveTopologyType(PrimitiveTopologyType::Triangle); #ifndef VULKAN m_blendUIPipelineState.setVertexShader(shaderStorage.loadShader(device, shaderRootPath + "ScreenQuadVS.cso")); m_blendUIPipelineState.setPixelShader(shaderStorage.loadShader(device, shaderRootPath + "BufferCopyPS.cso")); #else m_blendUIPipelineState.setVertexShader(shaderStorage.loadShader(device, shaderRootPath + "vert.spv")); m_blendUIPipelineState.setPixelShader(shaderStorage.loadShader(device, shaderRootPath + "frag.spv")); #endif m_blendUIPipelineState.setRenderTargetFormat(Format::Format_R10G10B10A2_UNORM, Format::Format_UNKNOWN); m_blendUIPipelineState.finalize();*/ }