Graphics: Make use of shader binding sets

This commit is contained in:
Jérôme Leclercq 2021-06-16 14:47:54 +02:00
parent 5559fe1af7
commit 3cd9172f7a
49 changed files with 592 additions and 792 deletions

View File

@ -14,8 +14,8 @@ struct ViewerData
external
{
[binding(0)] colorTexture: sampler2D<f32>,
[binding(1)] viewerData: uniform<ViewerData>
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] colorTexture: sampler2D<f32>,
}
struct FragIn

View File

@ -14,9 +14,9 @@ struct ViewerData
external
{
[binding(0)] colorTexture: sampler2D<f32>,
[binding(1)] bloomTexture: sampler2D<f32>,
[binding(2)] viewerData: uniform<ViewerData>
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] colorTexture: sampler2D<f32>,
[set(1), binding(1)] bloomTexture: sampler2D<f32>,
}
struct FragIn

View File

@ -32,12 +32,11 @@ struct ViewerData
external
{
[binding(5)] viewerData: uniform<ViewerData>,
[binding(4)] instanceData: uniform<InstanceData>,
[binding(3)] settings: uniform<BasicSettings>,
[binding(0)] MaterialAlphaMap: sampler2D<f32>,
[binding(1)] MaterialDiffuseMap: sampler2D<f32>,
[binding(2)] TextureOverlay: sampler2D<f32>
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] instanceData: uniform<InstanceData>,
[set(2), binding(0)] settings: uniform<BasicSettings>,
[set(2), binding(2)] MaterialAlphaMap: sampler2D<f32>,
[set(2), binding(1)] MaterialDiffuseMap: sampler2D<f32>
}
struct InputData

View File

@ -28,9 +28,9 @@ struct ViewerData
external
{
[binding(5)] viewerData: uniform<ViewerData>,
[binding(4)] instanceData: uniform<InstanceData>,
[binding(3)] settings: uniform<BasicSettings>
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] instanceData: uniform<InstanceData>,
[set(2), binding(0)] settings: uniform<BasicSettings>,
}
struct InputData

Binary file not shown.

Binary file not shown.

46
bin/resources/gamma.nzsl Normal file
View File

@ -0,0 +1,46 @@
external
{
[binding(0)] colorTexture: sampler2D<f32>
}
struct FragIn
{
[location(0)] uv: vec2<f32>
}
struct FragOut
{
[location(0)] color: vec4<f32>
}
struct VertIn
{
[location(0)] pos: vec3<f32>,
[location(1)] uv: vec2<f32>
}
struct VertOut
{
[location(0)] vertUV: vec2<f32>,
[builtin(position)] position: vec4<f32>
}
[entry(frag)]
fn main(input: FragIn) -> FragOut
{
let gamma = 2.2;
let output: FragOut;
output.color = colorTexture.Sample(input.uv);
//output.color = pow(colorTexture.Sample(input.uv), vec4<f32>(1.0 / gamma, 1.0 / gamma, 1.0 / gamma, 1.0));
return output;
}
[entry(vert)]
fn main(input: VertIn) -> VertOut
{
let output: VertOut;
output.position = vec4<f32>(input.pos, 1.0);
output.vertUV = input.uv;
return output;
}

View File

@ -14,8 +14,8 @@ struct ViewerData
external
{
[binding(0)] colorTexture: sampler2D<f32>,
[binding(1)] viewerData: uniform<ViewerData>
[set(0), binding(0)] viewerData: uniform<ViewerData>,
[set(1), binding(0)] colorTexture: sampler2D<f32>,
}
struct FragIn

View File

@ -38,13 +38,24 @@ struct ViewerData
eyePosition: vec3<f32>
}
[set(0)]
external
{
[binding(0)] viewerData: uniform<ViewerData>
}
[set(1)]
external
{
[binding(0)] colorTexture: sampler2D<f32>,
[binding(1)] normalTexture: sampler2D<f32>,
[binding(2)] positionTexture: sampler2D<f32>,
[binding(3)] lightParameters: uniform<SpotLight>,
[binding(4)] viewerData: uniform<ViewerData>
}
[set(2)]
external
{
[binding(0)] lightParameters: uniform<SpotLight>,
}
struct FragIn

View File

@ -14,7 +14,7 @@ struct ViewerData
external
{
[binding(1)] skybox: samplerCube<f32>
[set(1), binding(0)] skybox: samplerCube<f32>
}
struct VertOut
@ -42,7 +42,7 @@ fn main(input: VertOut) -> FragOut
external
{
[binding(0)] viewerData: uniform<ViewerData>
[set(0), binding(0)] viewerData: uniform<ViewerData>
}
struct VertIn

View File

@ -132,18 +132,16 @@ int main()
std::shared_ptr<Nz::GraphicalMesh> cubeMeshGfx = std::make_shared<Nz::GraphicalMesh>(*cubeMesh);
Nz::RenderPipelineLayoutInfo pipelineLayoutInfo;
auto& uboBinding = pipelineLayoutInfo.bindings.emplace_back();
uboBinding.index = 0;
uboBinding.shaderStageFlags = Nz::ShaderStageType::Vertex;
uboBinding.type = Nz::ShaderBindingType::UniformBuffer;
Nz::RenderPipelineLayoutInfo skyboxPipelineLayoutInfo;
Nz::Graphics::FillViewerPipelineLayout(skyboxPipelineLayoutInfo);
auto& textureBinding = pipelineLayoutInfo.bindings.emplace_back();
textureBinding.index = 1;
auto& textureBinding = skyboxPipelineLayoutInfo.bindings.emplace_back();
textureBinding.setIndex = 1;
textureBinding.bindingIndex = 0;
textureBinding.shaderStageFlags = Nz::ShaderStageType::Fragment;
textureBinding.type = Nz::ShaderBindingType::Texture;
std::shared_ptr<Nz::RenderPipelineLayout> skyboxPipelineLayout = device->InstantiateRenderPipelineLayout(std::move(pipelineLayoutInfo));
std::shared_ptr<Nz::RenderPipelineLayout> skyboxPipelineLayout = device->InstantiateRenderPipelineLayout(std::move(skyboxPipelineLayoutInfo));
Nz::RenderPipelineInfo skyboxPipelineInfo;
skyboxPipelineInfo.depthBuffer = true;
@ -227,49 +225,36 @@ int main()
viewerInstance.UpdateTargetSize(Nz::Vector2f(windowSize));
viewerInstance.UpdateProjViewMatrices(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f), Nz::Matrix4f::Translate(Nz::Vector3f::Up() * 1));
Nz::ModelInstance modelInstance1(spaceshipMat->GetSettings());
spaceshipMat->UpdateShaderBinding(modelInstance1.GetShaderBinding());
Nz::WorldInstance modelInstance1;
modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Left() + Nz::Vector3f::Up()));
Nz::ModelInstance modelInstance2(spaceshipMat->GetSettings());
spaceshipMat->UpdateShaderBinding(modelInstance2.GetShaderBinding());
Nz::WorldInstance modelInstance2;
modelInstance2.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Right() + Nz::Vector3f::Up()));
Nz::ModelInstance planeInstance(planeMat->GetSettings());
planeMat->UpdateShaderBinding(planeInstance.GetShaderBinding());
std::shared_ptr<Nz::AbstractBuffer> viewerDataUBO = Nz::Graphics::Instance()->GetViewerDataUBO();
Nz::WorldInstance planeInstance;
Nz::RenderWindowImpl* windowImpl = window.GetImpl();
std::shared_ptr<Nz::CommandPool> commandPool = windowImpl->CreateCommandPool(Nz::QueueType::Graphics);
Nz::RenderPipelineLayoutInfo fullscreenPipelineLayoutInfo;
fullscreenPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
0
});
Nz::RenderPipelineLayoutInfo lightingPipelineLayoutInfo;
Nz::Graphics::FillViewerPipelineLayout(lightingPipelineLayoutInfo);
for (unsigned int i = 0; i < 3; ++i)
{
lightingPipelineLayoutInfo.bindings.push_back({
1,
i,
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
i
});
}
lightingPipelineLayoutInfo.bindings.push_back({
2,
0,
Nz::ShaderBindingType::UniformBuffer,
Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex,
3
});
lightingPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::UniformBuffer,
Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex,
4
});
/*Nz::FieldOffsets pointLightOffsets(Nz::StructLayout::Std140);
@ -349,35 +334,12 @@ int main()
// Bloom data
Nz::RenderPipelineLayoutInfo bloomPipelineLayoutInfo;
bloomPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
0
});
Nz::Graphics::FillViewerPipelineLayout(bloomPipelineLayoutInfo);
bloomPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::UniformBuffer,
Nz::ShaderStageType::Fragment,
1
});
Nz::RenderPipelineLayoutInfo bloomBlendPipelineLayoutInfo;
bloomBlendPipelineLayoutInfo.bindings.push_back({
1, 0,
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
0
});
bloomBlendPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
1
});
bloomBlendPipelineLayoutInfo.bindings.push_back({
Nz::ShaderBindingType::UniformBuffer,
Nz::ShaderStageType::Fragment,
2
});
Nz::RenderPipelineInfo bloomPipelineInfo;
@ -390,13 +352,30 @@ int main()
bloomPipelineInfo.shaderModules.push_back(device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, resourceDir / "bloom_bright.nzsl", {}));
std::shared_ptr<Nz::ShaderBinding> bloomBrightShaderBinding = bloomPipelineInfo.pipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> gaussianBlurShaderBinding = bloomPipelineInfo.pipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> bloomBrightShaderBinding = bloomPipelineInfo.pipelineLayout->AllocateShaderBinding(1);
std::shared_ptr<Nz::ShaderBinding> gaussianBlurShaderBinding = bloomPipelineInfo.pipelineLayout->AllocateShaderBinding(1);
std::shared_ptr<Nz::RenderPipeline> bloomBrightPipeline = device->InstantiateRenderPipeline(bloomPipelineInfo);
bloomPipelineInfo.shaderModules.clear();
bloomPipelineInfo.shaderModules.push_back(device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, resourceDir / "gaussian_blur.nzsl", {}));
Nz::RenderPipelineLayoutInfo bloomBlendPipelineLayoutInfo;
Nz::Graphics::FillViewerPipelineLayout(bloomBlendPipelineLayoutInfo);
bloomBlendPipelineLayoutInfo.bindings.push_back({
1, 0,
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
});
bloomBlendPipelineLayoutInfo.bindings.push_back({
1, 1,
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
});
std::shared_ptr<Nz::RenderPipeline> gaussianBlurPipeline = device->InstantiateRenderPipeline(bloomPipelineInfo);
@ -412,9 +391,17 @@ int main()
std::shared_ptr<Nz::RenderPipeline> bloomBlendPipeline = device->InstantiateRenderPipeline(bloomBlendPipelineInfo);
std::shared_ptr<Nz::ShaderBinding> bloomBlendShaderBinding = bloomBlendPipelineInfo.pipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> bloomBlendShaderBinding = bloomBlendPipelineInfo.pipelineLayout->AllocateShaderBinding(1);
// Fullscreen data
Nz::RenderPipelineLayoutInfo fullscreenPipelineLayoutInfo;
fullscreenPipelineLayoutInfo.bindings.push_back({
0, 0,
Nz::ShaderBindingType::Texture,
Nz::ShaderStageType::Fragment,
});
Nz::RenderPipelineInfo fullscreenPipelineInfo;
fullscreenPipelineInfo.primitiveMode = Nz::PrimitiveMode::TriangleList;
@ -424,8 +411,7 @@ int main()
fullscreenVertexDeclaration
});
fullscreenPipelineInfo.shaderModules.push_back(device->InstantiateShaderModule(Nz::ShaderStageType::Fragment, Nz::ShaderLanguage::NazaraBinary, resourceDir / "fullscreen.frag.shader", {}));
fullscreenPipelineInfo.shaderModules.push_back(device->InstantiateShaderModule(Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraBinary, resourceDir / "fullscreen.vert.shader", {}));
fullscreenPipelineInfo.shaderModules.push_back(device->InstantiateShaderModule(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLanguage::NazaraShader, resourceDir / "gamma.nzsl", {}));
const std::shared_ptr<const Nz::VertexDeclaration>& lightingVertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_UV);
@ -525,23 +511,18 @@ int main()
if (!fullscreenVertexBuffer->Fill(vertexData.data(), 0, fullscreenVertexBuffer->GetSize()))
return __LINE__;
std::shared_ptr<Nz::ShaderBinding> finalBlitBinding = fullscreenPipelineInfo.pipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> bloomSkipBlit = fullscreenPipelineInfo.pipelineLayout->AllocateShaderBinding(0);
std::shared_ptr<Nz::ShaderBinding> finalBlitBinding = fullscreenPipelineInfo.pipelineLayout->AllocateShaderBinding(0);
bool lightUpdate = true;
bool matUpdate = false;
std::shared_ptr<Nz::TextureSampler> textureSampler = device->InstantiateTextureSampler({});
std::shared_ptr<Nz::ShaderBinding> skyboxShaderBinding = skyboxPipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> skyboxShaderBinding = skyboxPipelineLayout->AllocateShaderBinding(1);
skyboxShaderBinding->Update({
{
0,
Nz::ShaderBinding::UniformBufferBinding {
viewerDataUBO.get(),
0, viewerDataUBO->GetSize()
}
},
{
1,
Nz::ShaderBinding::TextureBinding {
skyboxTexture.get(),
textureSampler.get()
@ -549,6 +530,8 @@ int main()
}
});
std::shared_ptr<Nz::ShaderBinding> gbufferShaderBinding = lightingPipelineInfo.pipelineLayout->AllocateShaderBinding(1);
bool bloomEnabled = true;
bool forwardEnabled = true;
bool lightAnimation = true;
@ -641,11 +624,18 @@ int main()
gbufferPass.SetDepthStencilOutput(depthBuffer);
gbufferPass.SetExecutionCallback([&]
{
return (matUpdate) ? Nz::FramePassExecution::UpdateAndExecute : Nz::FramePassExecution::Execute;
});
gbufferPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder)
{
builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
spaceshipModel.Draw(builder, modelInstance1);
spaceshipModel.Draw(builder, modelInstance2);
@ -668,9 +658,12 @@ int main()
builder.BindIndexBuffer(coneMeshGfx->GetIndexBuffer(0).get());
builder.BindVertexBuffer(0, coneMeshGfx->GetVertexBuffer(0).get());
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
builder.BindShaderBinding(1, *gbufferShaderBinding);
for (std::size_t i = 0; i < spotLights.size(); ++i)
{
builder.BindShaderBinding(*lightingShaderBindings[i]);
builder.BindShaderBinding(2, *lightingShaderBindings[i]);
builder.BindPipeline(*stencilPipeline);
builder.DrawIndexed(coneMeshGfx->GetIndexCount(0));
@ -683,6 +676,7 @@ int main()
lightingPass.AddInput(colorTexture);
lightingPass.AddInput(normalTexture);
lightingPass.AddInput(positionTexture);
lightingPass.SetClearColor(lightingPass.AddOutput(lightOutput), Nz::Color::Black);
lightingPass.SetDepthStencilInput(depthBuffer);
lightingPass.SetDepthStencilOutput(depthBuffer);
@ -693,7 +687,8 @@ int main()
builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.BindShaderBinding(*skyboxShaderBinding);
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
builder.BindShaderBinding(1, *skyboxShaderBinding);
builder.BindIndexBuffer(cubeMeshGfx->GetIndexBuffer(0).get());
builder.BindVertexBuffer(0, cubeMeshGfx->GetVertexBuffer(0).get());
@ -711,14 +706,15 @@ int main()
forwardPass.SetDepthStencilInput(depthBuffer);
forwardPass.SetDepthStencilOutput(depthBuffer);
Nz::FramePass& bloomBrightPass = graph.AddPass("Bloom pass - extract bright pixels");
bloomBrightPass.SetCommandCallback([&](Nz::CommandBufferBuilder& builder)
{
builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth) / 10, int(offscreenHeight) / 10 });
builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth) / 10, int(offscreenHeight) / 10 });
builder.BindShaderBinding(*bloomBrightShaderBinding);
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
builder.BindShaderBinding(1, *bloomBrightShaderBinding);
builder.BindPipeline(*bloomBrightPipeline);
builder.BindVertexBuffer(0, fullscreenVertexBuffer.get());
@ -738,7 +734,8 @@ int main()
builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth) / 10, int(offscreenHeight) / 10 });
builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth) / 10, int(offscreenHeight) / 10 });
builder.BindShaderBinding(*gaussianBlurShaderBinding);
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
builder.BindShaderBinding(1, *gaussianBlurShaderBinding);
builder.BindPipeline(*gaussianBlurPipeline);
builder.BindVertexBuffer(0, fullscreenVertexBuffer.get());
@ -758,7 +755,8 @@ int main()
builder.SetScissor(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.SetViewport(Nz::Recti{ 0, 0, int(offscreenWidth), int(offscreenHeight) });
builder.BindShaderBinding(*bloomBlendShaderBinding);
builder.BindShaderBinding(0, viewerInstance.GetShaderBinding());
builder.BindShaderBinding(1, *bloomBlendShaderBinding);
builder.BindPipeline(*bloomBlendPipeline);
builder.BindVertexBuffer(0, fullscreenVertexBuffer.get());
@ -780,45 +778,41 @@ int main()
bakedGraph.Resize(offscreenWidth, offscreenHeight);
gbufferShaderBinding->Update({
{
0,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(colorTexture).get(),
textureSampler.get()
}
},
{
1,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(normalTexture).get(),
textureSampler.get()
}
},
{
2,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(positionTexture).get(),
textureSampler.get()
}
}
});
for (std::size_t i = 0; i < MaxPointLight; ++i)
{
std::shared_ptr<Nz::ShaderBinding> lightingShaderBinding = lightingPipelineInfo.pipelineLayout->AllocateShaderBinding();
std::shared_ptr<Nz::ShaderBinding> lightingShaderBinding = lightingPipelineInfo.pipelineLayout->AllocateShaderBinding(2);
lightingShaderBinding->Update({
{
0,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(colorTexture).get(),
textureSampler.get()
}
},
{
1,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(normalTexture).get(),
textureSampler.get()
}
},
{
2,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(positionTexture).get(),
textureSampler.get()
}
},
{
3,
Nz::ShaderBinding::UniformBufferBinding {
lightUbo.get(),
i * alignedSpotLightSize, spotLightOffsets.GetAlignedSize()
}
},
{
4,
Nz::ShaderBinding::UniformBufferBinding {
viewerDataUBO.get(),
0, viewerDataUBO->GetSize()
}
}
});
@ -832,13 +826,6 @@ int main()
bakedGraph.GetAttachmentTexture(lightOutput).get(),
textureSampler.get()
}
},
{
1,
Nz::ShaderBinding::UniformBufferBinding {
viewerDataUBO.get(),
0, viewerDataUBO->GetSize()
}
}
});
@ -849,13 +836,6 @@ int main()
bakedGraph.GetAttachmentTexture(bloomTextureA).get(),
textureSampler.get()
}
},
{
1,
Nz::ShaderBinding::UniformBufferBinding {
viewerDataUBO.get(),
0, viewerDataUBO->GetSize()
}
}
});
@ -873,12 +853,15 @@ int main()
bakedGraph.GetAttachmentTexture(bloomTextureB).get(),
textureSampler.get()
}
},
}
});
bloomSkipBlit->Update({
{
2,
Nz::ShaderBinding::UniformBufferBinding {
viewerDataUBO.get(),
0, viewerDataUBO->GetSize()
0,
Nz::ShaderBinding::TextureBinding {
bakedGraph.GetAttachmentTexture(lightOutput).get(),
textureSampler.get()
}
}
});
@ -912,7 +895,7 @@ int main()
builder.SetScissor(Nz::Recti{ 0, 0, int(windowSize.x), int(windowSize.y) });
builder.SetViewport(Nz::Recti{ 0, 0, int(windowSize.x), int(windowSize.y) });
builder.BindShaderBinding(*finalBlitBinding);
builder.BindShaderBinding(0, *finalBlitBinding);
builder.BindPipeline(*fullscreenPipeline);
builder.BindVertexBuffer(0, fullscreenVertexBuffer.get());
builder.Draw(3);
@ -1011,8 +994,11 @@ int main()
forwardEnabled = !forwardEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::A)
lightAnimation = !lightAnimation;
else if (event.key.virtualKey == Nz::Keyboard::VKey::B)
bloomEnabled = !bloomEnabled;
else if (event.key.virtualKey == Nz::Keyboard::VKey::E)
modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Transform(viewerPos, camQuat));
break;
}
@ -1064,7 +1050,10 @@ int main()
continue;
if (frame.IsFramebufferInvalidated())
{
frame.PushForRelease(std::move(drawCommandBuffer));
RebuildCommandBuffer();
}
Nz::UploadPool& uploadPool = frame.GetUploadPool();
@ -1078,7 +1067,7 @@ int main()
modelInstance2.UpdateBuffers(uploadPool, builder);
planeInstance.UpdateBuffers(uploadPool, builder);
viewerInstance.UpdateViewBuffer(uploadPool, builder);
viewerInstance.UpdateBuffers(uploadPool, builder);
if (!spotLights.empty() && (lightUpdate || lightAnimation))
{
@ -1110,7 +1099,8 @@ int main()
builder.CopyBuffer(lightDataAllocation, lightUbo.get());
}
spaceshipMat->UpdateBuffers(uploadPool, builder);
matUpdate = spaceshipMat->Update(frame, builder) || matUpdate;
matUpdate = planeMat->Update(frame, builder) || matUpdate;
builder.PostTransferBarrier();
}
@ -1124,6 +1114,7 @@ int main()
window.Display();
matUpdate = false;
lightUpdate = false;
// On incrémente le compteur de FPS improvisé

View File

@ -27,8 +27,9 @@ int main()
Nz::RenderWindow window;
Nz::MeshParams meshParams;
meshParams.center = true;
meshParams.storage = Nz::DataStorage::Software;
meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, 90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f));
meshParams.matrix = Nz::Matrix4f::Rotate(Nz::EulerAnglesf(0.f, -90.f, 0.f)) * Nz::Matrix4f::Scale(Nz::Vector3f(0.002f));
meshParams.vertexDeclaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Normal_UV);
std::shared_ptr<Nz::RenderDevice> device = Nz::Graphics::Instance()->GetRenderDevice();
@ -66,7 +67,7 @@ int main()
material->EnableFaceCulling(true);
Nz::BasicMaterial basicMat(*material);
basicMat.EnableAlphaTest(true);
basicMat.EnableAlphaTest(false);
basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
@ -74,40 +75,17 @@ int main()
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
model.SetMaterial(i, material);
Nz::PredefinedInstanceData instanceUboOffsets = Nz::PredefinedInstanceData::GetOffsets();
Nz::PredefinedViewerData viewerUboOffsets = Nz::PredefinedViewerData::GetOffsets();
const Nz::BasicMaterial::UniformOffsets& materialSettingOffsets = Nz::BasicMaterial::GetOffsets();
std::vector<std::uint8_t> viewerDataBuffer(viewerUboOffsets.totalSize);
Nz::Vector2ui windowSize = window.GetSize();
Nz::AccessByOffset<Nz::Matrix4f&>(viewerDataBuffer.data(), viewerUboOffsets.viewMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Backward() * 1);
Nz::AccessByOffset<Nz::Matrix4f&>(viewerDataBuffer.data(), viewerUboOffsets.projMatrixOffset) = Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f);
Nz::ViewerInstance viewerInstance;
viewerInstance.UpdateTargetSize(Nz::Vector2f(window.GetSize()));
viewerInstance.UpdateProjViewMatrices(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f), Nz::Matrix4f::Translate(Nz::Vector3f::Backward() * 1));
std::vector<std::uint8_t> instanceDataBuffer(instanceUboOffsets.totalSize);
Nz::WorldInstance modelInstance;
modelInstance.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Left()));
Nz::ModelInstance modelInstance(material->GetSettings());
{
material->UpdateShaderBinding(modelInstance.GetShaderBinding());
Nz::AccessByOffset<Nz::Matrix4f&>(instanceDataBuffer.data(), instanceUboOffsets.worldMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right());
std::shared_ptr<Nz::AbstractBuffer>& instanceDataUBO = modelInstance.GetInstanceBuffer();
instanceDataUBO->Fill(instanceDataBuffer.data(), 0, instanceDataBuffer.size());
}
Nz::ModelInstance modelInstance2(material->GetSettings());
{
material->UpdateShaderBinding(modelInstance2.GetShaderBinding());
Nz::AccessByOffset<Nz::Matrix4f&>(instanceDataBuffer.data(), instanceUboOffsets.worldMatrixOffset) = Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right() * 3.f);
std::shared_ptr<Nz::AbstractBuffer>& instanceDataUBO = modelInstance2.GetInstanceBuffer();
instanceDataUBO->Fill(instanceDataBuffer.data(), 0, instanceDataBuffer.size());
}
std::shared_ptr<Nz::AbstractBuffer> viewerDataUBO = Nz::Graphics::Instance()->GetViewerDataUBO();
Nz::WorldInstance modelInstance2;
modelInstance2.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Right()));
Nz::RenderWindowImpl* windowImpl = window.GetImpl();
std::shared_ptr<Nz::CommandPool> commandPool = windowImpl->CreateCommandPool(Nz::QueueType::Graphics);
@ -116,7 +94,6 @@ int main()
auto RebuildCommandBuffer = [&]
{
Nz::Vector2ui windowSize = window.GetSize();
drawCommandBuffer = commandPool->BuildCommandBuffer([&](Nz::CommandBufferBuilder& builder)
{
Nz::Recti renderRect(0, 0, window.GetSize().x, window.GetSize().y);
@ -132,16 +109,18 @@ int main()
{
builder.SetScissor(Nz::Recti{ 0, 0, int(windowSize.x), int(windowSize.y) });
builder.SetViewport(Nz::Recti{ 0, 0, int(windowSize.x), int(windowSize.y) });
builder.BindShaderBinding(Nz::Graphics::ViewerBindingSet, viewerInstance.GetShaderBinding());
for (Nz::ModelInstance& modelInstance : { std::ref(modelInstance), std::ref(modelInstance2) })
for (Nz::WorldInstance& instance : { std::ref(modelInstance), std::ref(modelInstance2) })
{
builder.BindShaderBinding(modelInstance.GetShaderBinding());
builder.BindShaderBinding(Nz::Graphics::WorldBindingSet, instance.GetShaderBinding());
for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i)
{
builder.BindIndexBuffer(model.GetIndexBuffer(i).get());
builder.BindVertexBuffer(0, model.GetVertexBuffer(i).get());
builder.BindPipeline(*model.GetRenderPipeline(i));
builder.BindShaderBinding(Nz::Graphics::MaterialBindingSet, model.GetMaterial(i)->GetShaderBinding());
builder.DrawIndexed(model.GetIndexCount(i));
}
@ -152,7 +131,6 @@ int main()
builder.EndDebugRegion();
});
};
RebuildCommandBuffer();
Nz::Vector3f viewerPos = Nz::Vector3f::Zero();
@ -165,10 +143,10 @@ int main()
Nz::Clock updateClock;
Nz::Clock secondClock;
unsigned int fps = 0;
bool viewerUboUpdate = true;
Nz::Mouse::SetRelativeMouseMode(true);
bool updateMat = false;
while (window.IsOpen())
{
Nz::WindowEvent event;
@ -180,6 +158,15 @@ int main()
window.Close();
break;
case Nz::WindowEventType::KeyPressed:
if (event.key.virtualKey == Nz::Keyboard::VKey::A)
{
basicMat.EnableAlphaTest(!basicMat.IsAlphaTestEnabled());
updateMat = true;
}
break;
case Nz::WindowEventType::MouseMoved: // La souris a bougé
{
// Gestion de la caméra free-fly (Rotation)
@ -193,16 +180,14 @@ int main()
camAngles.pitch = Nz::Clamp(camAngles.pitch - event.mouseMove.deltaY*sensitivity, -89.f, 89.f);
camQuat = camAngles;
viewerUboUpdate = true;
break;
}
case Nz::WindowEventType::Resized:
{
Nz::Vector2ui windowSize = window.GetSize();
Nz::AccessByOffset<Nz::Matrix4f&>(viewerDataBuffer.data(), viewerUboOffsets.projMatrixOffset) = Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f);
viewerUboUpdate = true;
viewerInstance.UpdateProjectionMatrix(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f));
viewerInstance.UpdateTargetSize(Nz::Vector2f(windowSize));
break;
}
@ -238,41 +223,37 @@ int main()
// Contrôle (Gauche ou droite) pour descendre dans l'espace global, etc...
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::LControl) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::RControl))
viewerPos += Nz::Vector3f::Down() * cameraSpeed;
viewerUboUpdate = true;
}
Nz::RenderFrame frame = windowImpl->Acquire();
if (!frame)
continue;
if (frame.IsFramebufferInvalidated())
RebuildCommandBuffer();
Nz::UploadPool& uploadPool = frame.GetUploadPool();
Nz::AccessByOffset<Nz::Matrix4f&>(viewerDataBuffer.data(), viewerUboOffsets.viewMatrixOffset) = Nz::Matrix4f::ViewMatrix(viewerPos, camAngles);
viewerInstance.UpdateViewMatrix(Nz::Matrix4f::ViewMatrix(viewerPos, camAngles));
if (viewerUboUpdate)
frame.Execute([&](Nz::CommandBufferBuilder& builder)
{
Nz::UploadPool& uploadPool = frame.GetUploadPool();
auto& allocation = uploadPool.Allocate(viewerDataBuffer.size());
std::memcpy(allocation.mappedPtr, viewerDataBuffer.data(), viewerDataBuffer.size());
frame.Execute([&](Nz::CommandBufferBuilder& builder)
builder.BeginDebugRegion("UBO Update", Nz::Color::Yellow);
{
builder.BeginDebugRegion("UBO Update", Nz::Color::Yellow);
{
builder.PreTransferBarrier();
builder.CopyBuffer(allocation, viewerDataUBO.get());
builder.PreTransferBarrier();
material->UpdateBuffers(uploadPool, builder);
viewerInstance.UpdateBuffers(uploadPool, builder);
modelInstance.UpdateBuffers(uploadPool, builder);
modelInstance2.UpdateBuffers(uploadPool, builder);
builder.PostTransferBarrier();
}
builder.EndDebugRegion();
}, Nz::QueueType::Transfer);
updateMat = material->Update(frame, builder) || updateMat;
viewerUboUpdate = false;
builder.PostTransferBarrier();
}
builder.EndDebugRegion();
}, Nz::QueueType::Transfer);
if (updateMat || frame.IsFramebufferInvalidated())
{
frame.PushForRelease(std::move(drawCommandBuffer));
RebuildCommandBuffer();
}
frame.SubmitCommandBuffer(drawCommandBuffer.get(), Nz::QueueType::Graphics);

View File

@ -369,7 +369,10 @@ int main()
continue;
if (frame.IsFramebufferInvalidated())
{
frame.PushForRelease(std::move(drawCommandBuffer));
RebuildCommandBuffer();
}
ubo.viewMatrix = Nz::Matrix4f::ViewMatrix(viewerPos, camAngles);

View File

@ -43,11 +43,12 @@
#include <Nazara/Graphics/MaterialPipeline.hpp>
#include <Nazara/Graphics/MaterialSettings.hpp>
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/ModelInstance.hpp>
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <Nazara/Graphics/RenderQueue.hpp>
#include <Nazara/Graphics/TextureSamplerCache.hpp>
#include <Nazara/Graphics/UberShader.hpp>
#include <Nazara/Graphics/ViewerInstance.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#endif // NAZARA_GLOBAL_GRAPHICS_HPP

View File

@ -16,18 +16,6 @@ namespace Nz
Sphere,
Volume
};
enum class PredefinedShaderBinding
{
TexOverlay,
UboInstanceData,
UboLighData,
UboViewerData,
Max = UboViewerData
};
constexpr std::size_t PredefinedShaderBindingCount = static_cast<std::size_t>(PredefinedShaderBinding::Max) + 1;
}
#endif // NAZARA_ENUMS_GRAPHICS_HPP

View File

@ -12,12 +12,12 @@
#include <Nazara/Graphics/TextureSamplerCache.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
#include <optional>
namespace Nz
{
class AbstractBuffer;
class RenderDevice;
class NAZARA_GRAPHICS_API Graphics : public ModuleBase<Graphics>
{
@ -31,19 +31,26 @@ namespace Nz
Graphics(Config config);
~Graphics();
inline const std::shared_ptr<RenderPipelineLayout>& GetReferencePipelineLayout() const;
inline const std::shared_ptr<RenderDevice>& GetRenderDevice() const;
inline TextureSamplerCache& GetSamplerCache();
inline const std::shared_ptr<AbstractBuffer>& GetViewerDataUBO();
struct Config
{
bool useDedicatedRenderDevice = true;
};
static constexpr UInt32 MaterialBindingSet = 2;
static constexpr UInt32 ViewerBindingSet = 0;
static constexpr UInt32 WorldBindingSet = 1;
static void FillViewerPipelineLayout(RenderPipelineLayoutInfo& layoutInfo, UInt32 set = ViewerBindingSet);
static void FillWorldPipelineLayout(RenderPipelineLayoutInfo& layoutInfo, UInt32 set = WorldBindingSet);
private:
std::optional<TextureSamplerCache> m_samplerCache;
std::shared_ptr<AbstractBuffer> m_viewerDataUBO;
std::shared_ptr<RenderDevice> m_renderDevice;
std::shared_ptr<RenderPipelineLayout> m_referencePipelineLayout;
static Graphics* s_instance;
};

View File

@ -7,6 +7,11 @@
namespace Nz
{
inline const std::shared_ptr<RenderPipelineLayout>& Graphics::GetReferencePipelineLayout() const
{
return m_referencePipelineLayout;
}
inline const std::shared_ptr<RenderDevice>& Graphics::GetRenderDevice() const
{
return m_renderDevice;
@ -16,11 +21,6 @@ namespace Nz
{
return *m_samplerCache;
}
inline const std::shared_ptr<AbstractBuffer>& Graphics::GetViewerDataUBO()
{
return m_viewerDataUBO;
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -13,7 +13,7 @@
namespace Nz
{
class CommandBufferBuilder;
class ModelInstance;
class WorldInstance;
class NAZARA_GRAPHICS_API InstancedRenderable
{
@ -23,7 +23,7 @@ namespace Nz
InstancedRenderable(InstancedRenderable&&) noexcept = default;
~InstancedRenderable();
virtual void Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const = 0;
virtual void Draw(CommandBufferBuilder& commandBuffer, WorldInstance& instance) const = 0;
InstancedRenderable& operator=(const InstancedRenderable&) = delete;
InstancedRenderable& operator=(InstancedRenderable&&) noexcept = default;

View File

@ -28,7 +28,7 @@
namespace Nz
{
class CommandBufferBuilder;
class UploadPool;
class RenderFrame;
class NAZARA_GRAPHICS_API Material : public Resource
{
@ -70,6 +70,7 @@ namespace Nz
inline float GetPointSize() const;
inline const std::shared_ptr<const MaterialSettings>& GetSettings() const;
inline const std::shared_ptr<UberShader>& GetShader(ShaderStageType shaderStage) const;
inline ShaderBinding& GetShaderBinding();
inline const std::shared_ptr<Texture>& GetTexture(std::size_t textureIndex) const;
inline const TextureSamplerInfo& GetTextureSampler(std::size_t textureIndex) const;
inline const std::shared_ptr<AbstractBuffer>& GetUniformBuffer(std::size_t bufferIndex) const;
@ -103,8 +104,7 @@ namespace Nz
inline void SetTextureSampler(std::size_t textureIndex, TextureSamplerInfo samplerInfo);
inline void SetUniformBuffer(std::size_t bufferIndex, std::shared_ptr<AbstractBuffer> uniformBuffer);
void UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder);
void UpdateShaderBinding(ShaderBinding& shaderBinding) const;
bool Update(RenderFrame& renderFrame, CommandBufferBuilder& builder);
// Signals:
NazaraSignal(OnMaterialRelease, const Material* /*material*/);
@ -114,6 +114,7 @@ namespace Nz
inline void InvalidateShaderBinding();
inline void InvalidateTextureSampler(std::size_t textureIndex);
inline void UpdatePipeline() const;
void UpdateShaderBinding();
struct MaterialTexture
{
@ -135,7 +136,9 @@ namespace Nz
mutable std::shared_ptr<MaterialPipeline> m_pipeline;
UInt64 m_enabledConditions;
mutable MaterialPipelineInfo m_pipelineInfo;
ShaderBindingPtr m_shaderBinding;
mutable bool m_pipelineUpdated;
bool m_shaderBindingUpdated;
bool m_shadowCastingEnabled;
};
}

View File

@ -94,7 +94,7 @@ namespace Nz
{
if (TestBit<UInt64>(m_enabledConditions, conditionIndex) != enable)
{
m_enabledConditions = SetBit<UInt64>(m_enabledConditions, conditionIndex);
m_enabledConditions = ToggleBit<UInt64>(m_enabledConditions, conditionIndex);
InvalidatePipeline();
}
}
@ -439,6 +439,12 @@ namespace Nz
return m_pipelineInfo.shaders[UnderlyingCast(shaderStage)].uberShader;
}
inline ShaderBinding& Material::GetShaderBinding()
{
assert(m_shaderBinding);
return *m_shaderBinding;
}
inline const std::shared_ptr<Texture>& Material::GetTexture(std::size_t textureIndex) const
{
NazaraAssert(textureIndex < m_textures.size(), "Invalid texture index");
@ -725,7 +731,7 @@ namespace Nz
inline void Material::InvalidateShaderBinding()
{
//TODO
m_shaderBindingUpdated = false;
}
inline void Material::InvalidateTextureSampler(std::size_t textureIndex)

View File

@ -24,7 +24,6 @@ namespace Nz
class MaterialSettings
{
public:
using PredefinedBinding = std::array<std::size_t, PredefinedShaderBindingCount>;
using Shaders = std::array<std::shared_ptr<UberShader>, ShaderStageTypeCount>;
struct Builder;
@ -42,7 +41,6 @@ namespace Nz
inline const Builder& GetBuilderData() const;
inline const std::vector<Condition>& GetConditions() const;
inline std::size_t GetConditionIndex(const std::string_view& name) const;
inline std::size_t GetPredefinedBindingIndex(PredefinedShaderBinding binding) const;
inline const std::shared_ptr<RenderPipelineLayout>& GetRenderPipelineLayout() const;
inline const std::shared_ptr<UberShader>& GetShader(ShaderStageType stage) const;
inline const Shaders& GetShaders() const;
@ -62,7 +60,6 @@ namespace Nz
struct Builder
{
PredefinedBinding predefinedBinding;
Shaders shaders;
std::vector<Condition> conditions;
std::vector<Texture> textures;
@ -84,25 +81,28 @@ namespace Nz
struct SharedUniformBlock
{
UInt32 bindingIndex;
std::string name;
std::string bindingPoint;
std::vector<UniformVariable> uniforms;
ShaderStageTypeFlags shaderStages = ShaderStageType_All;
};
struct Texture
{
std::string bindingPoint;
UInt32 bindingIndex;
std::string name;
ImageType type;
ShaderStageTypeFlags shaderStages = ShaderStageType_All;
};
struct UniformBlock
{
std::size_t blockSize;
UInt32 bindingIndex;
std::string name;
std::string bindingPoint;
std::size_t blockSize;
std::vector<UniformVariable> uniforms;
std::vector<UInt8> defaultValues;
ShaderStageTypeFlags shaderStages = ShaderStageType_All;
};
private:

View File

@ -18,36 +18,36 @@ namespace Nz
m_data(std::move(data))
{
RenderPipelineLayoutInfo info;
unsigned int bindingIndex = 0;
Graphics::FillViewerPipelineLayout(info);
Graphics::FillWorldPipelineLayout(info);
for (const Texture& textureInfo : m_data.textures)
{
info.bindings.push_back({
//textureInfo.bindingPoint,
Graphics::MaterialBindingSet,
textureInfo.bindingIndex,
ShaderBindingType::Texture,
ShaderStageType_All,
bindingIndex++
textureInfo.shaderStages
});
}
for (const UniformBlock& ubo : m_data.uniformBlocks)
{
info.bindings.push_back({
//ubo.bindingPoint,
Graphics::MaterialBindingSet,
ubo.bindingIndex,
ShaderBindingType::UniformBuffer,
ShaderStageType_All,
bindingIndex++
ubo.shaderStages
});
}
for (const SharedUniformBlock& ubo : m_data.sharedUniformBlocks)
{
info.bindings.push_back({
//ubo.bindingPoint,
Graphics::MaterialBindingSet,
ubo.bindingIndex,
ShaderBindingType::UniformBuffer,
ShaderStageType_All,
bindingIndex++
ubo.shaderStages
});
}
@ -75,11 +75,6 @@ namespace Nz
return InvalidIndex;
}
inline std::size_t MaterialSettings::GetPredefinedBindingIndex(PredefinedShaderBinding binding) const
{
return m_data.predefinedBinding[UnderlyingCast(binding)];
}
inline const std::shared_ptr<RenderPipelineLayout>& MaterialSettings::GetRenderPipelineLayout() const
{
return m_pipelineLayout;

View File

@ -28,10 +28,11 @@ namespace Nz
Model(Model&&) noexcept = default;
~Model() = default;
void Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const override;
void Draw(CommandBufferBuilder& commandBuffer, WorldInstance& instance) const override;
const std::shared_ptr<AbstractBuffer>& GetIndexBuffer(std::size_t subMeshIndex) const;
std::size_t GetIndexCount(std::size_t subMeshIndex) const;
const std::shared_ptr<Material>& GetMaterial(std::size_t subMeshIndex) const;
const std::shared_ptr<RenderPipeline>& GetRenderPipeline(std::size_t subMeshIndex) const;
const std::shared_ptr<AbstractBuffer>& GetVertexBuffer(std::size_t subMeshIndex) const;
inline std::size_t GetSubMeshCount() const;

View File

@ -42,7 +42,6 @@ namespace Nz
std::size_t worldMatrixOffset;
static PredefinedInstanceData GetOffsets();
static MaterialSettings::SharedUniformBlock GetUniformBlock();
};
struct NAZARA_GRAPHICS_API PredefinedViewerData
@ -59,7 +58,6 @@ namespace Nz
std::size_t viewProjMatrixOffset;
static PredefinedViewerData GetOffsets();
static MaterialSettings::SharedUniformBlock GetUniformBlock();
};
}

View File

@ -28,7 +28,11 @@ namespace Nz
ViewerInstance(ViewerInstance&&) noexcept = default;
~ViewerInstance() = default;
void UpdateViewBuffer(UploadPool& uploadPool, CommandBufferBuilder& builder);
inline std::shared_ptr<AbstractBuffer>& GetInstanceBuffer();
inline const std::shared_ptr<AbstractBuffer>& GetInstanceBuffer() const;
inline ShaderBinding& GetShaderBinding();
void UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder);
inline void UpdateProjectionMatrix(const Matrix4f& projectionMatrix);
inline void UpdateProjectionMatrix(const Matrix4f& projectionMatrix, const Matrix4f& invProjectionMatrix);
inline void UpdateProjViewMatrices(const Matrix4f& projectionMatrix, const Matrix4f& viewMatrix);
@ -42,13 +46,16 @@ namespace Nz
ViewerInstance& operator=(ViewerInstance&&) noexcept = default;
private:
std::shared_ptr<AbstractBuffer> m_viewerDataBuffer;
Matrix4f m_invProjectionMatrix;
Matrix4f m_invViewProjMatrix;
Matrix4f m_invViewMatrix;
Matrix4f m_projectionMatrix;
Matrix4f m_viewProjMatrix;
Matrix4f m_viewMatrix;
ShaderBindingPtr m_shaderBinding;
Vector2f m_targetSize;
bool m_dataInvalided;
};
}

View File

@ -8,17 +8,36 @@
namespace Nz
{
inline std::shared_ptr<AbstractBuffer>& ViewerInstance::GetInstanceBuffer()
{
return m_viewerDataBuffer;
}
inline const std::shared_ptr<AbstractBuffer>& ViewerInstance::GetInstanceBuffer() const
{
return m_viewerDataBuffer;
}
inline ShaderBinding& ViewerInstance::GetShaderBinding()
{
return *m_shaderBinding;
}
inline void ViewerInstance::UpdateProjectionMatrix(const Matrix4f& projectionMatrix)
{
m_projectionMatrix = projectionMatrix;
if (!m_projectionMatrix.GetInverse(&m_invProjectionMatrix))
NazaraError("failed to inverse projection matrix");
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateProjectionMatrix(const Matrix4f& projectionMatrix, const Matrix4f& invProjectionMatrix)
{
m_projectionMatrix = projectionMatrix;
m_invProjectionMatrix = invProjectionMatrix;
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateProjViewMatrices(const Matrix4f& projectionMatrix, const Matrix4f& viewMatrix)
@ -30,6 +49,12 @@ namespace Nz
m_viewMatrix = viewMatrix;
if (!m_viewMatrix.GetInverseAffine(&m_invViewMatrix))
NazaraError("failed to inverse view matrix");
m_viewProjMatrix = m_projectionMatrix * m_viewMatrix;
if (!m_viewProjMatrix.GetInverse(&m_invViewProjMatrix))
NazaraError("failed to inverse view proj matrix");
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateProjViewMatrices(const Matrix4f& projectionMatrix, const Matrix4f& invProjectionMatrix, const Matrix4f& viewMatrix, const Matrix4f& invViewMatrix)
@ -41,6 +66,8 @@ namespace Nz
m_viewProjMatrix = m_viewMatrix * m_projectionMatrix;
m_invViewProjMatrix = m_invProjectionMatrix * m_invViewMatrix;
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateProjViewMatrices(const Matrix4f& projectionMatrix, const Matrix4f& invProjectionMatrix, const Matrix4f& viewMatrix, const Matrix4f& invViewMatrix, const Matrix4f& viewProjMatrix, const Matrix4f& invViewProjMatrix)
@ -52,11 +79,15 @@ namespace Nz
m_viewProjMatrix = viewProjMatrix;
m_invViewProjMatrix = invViewProjMatrix;
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateTargetSize(const Vector2f& targetSize)
{
m_targetSize = targetSize;
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateViewMatrix(const Matrix4f& viewMatrix)
@ -67,6 +98,8 @@ namespace Nz
m_viewProjMatrix = m_viewMatrix * m_projectionMatrix;
m_invViewProjMatrix = m_invProjectionMatrix * m_invViewMatrix;
m_dataInvalided = true;
}
inline void ViewerInstance::UpdateViewMatrix(const Matrix4f& viewMatrix, const Matrix4f& invViewMatrix)
@ -76,6 +109,8 @@ namespace Nz
m_viewProjMatrix = m_viewMatrix * m_projectionMatrix;
m_invViewProjMatrix = m_invProjectionMatrix * m_invViewMatrix;
m_dataInvalided = true;
}
}

View File

@ -20,13 +20,13 @@ namespace Nz
class MaterialSettings;
class UploadPool;
class NAZARA_GRAPHICS_API ModelInstance
class NAZARA_GRAPHICS_API WorldInstance
{
public:
ModelInstance(const std::shared_ptr<const MaterialSettings>& settings);
ModelInstance(const ModelInstance&) = delete;
ModelInstance(ModelInstance&&) noexcept = default;
~ModelInstance() = default;
WorldInstance();
WorldInstance(const WorldInstance&) = delete;
WorldInstance(WorldInstance&&) noexcept = default;
~WorldInstance() = default;
inline std::shared_ptr<AbstractBuffer>& GetInstanceBuffer();
inline const std::shared_ptr<AbstractBuffer>& GetInstanceBuffer() const;
@ -36,8 +36,8 @@ namespace Nz
inline void UpdateWorldMatrix(const Matrix4f& worldMatrix);
inline void UpdateWorldMatrix(const Matrix4f& worldMatrix, const Matrix4f& invWorldMatrix);
ModelInstance& operator=(const ModelInstance&) = delete;
ModelInstance& operator=(ModelInstance&&) noexcept = default;
WorldInstance& operator=(const WorldInstance&) = delete;
WorldInstance& operator=(WorldInstance&&) noexcept = default;
private:
std::shared_ptr<AbstractBuffer> m_instanceDataBuffer;
@ -48,6 +48,6 @@ namespace Nz
};
}
#include <Nazara/Graphics/ModelInstance.inl>
#include <Nazara/Graphics/WorldInstance.inl>
#endif // NAZARA_MODELINSTANCE_HPP

View File

@ -2,28 +2,28 @@
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ModelInstance.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline std::shared_ptr<AbstractBuffer>& ModelInstance::GetInstanceBuffer()
inline std::shared_ptr<AbstractBuffer>& WorldInstance::GetInstanceBuffer()
{
return m_instanceDataBuffer;
}
inline const std::shared_ptr<AbstractBuffer>& ModelInstance::GetInstanceBuffer() const
inline const std::shared_ptr<AbstractBuffer>& WorldInstance::GetInstanceBuffer() const
{
return m_instanceDataBuffer;
}
inline ShaderBinding& ModelInstance::GetShaderBinding()
inline ShaderBinding& WorldInstance::GetShaderBinding()
{
return *m_shaderBinding;
}
inline void ModelInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix)
inline void WorldInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix)
{
m_worldMatrix = worldMatrix;
if (!m_worldMatrix.GetInverseAffine(&m_invWorldMatrix))
@ -32,7 +32,7 @@ namespace Nz
m_dataInvalided = true;
}
inline void ModelInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix, const Matrix4f& invWorldMatrix)
inline void WorldInstance::UpdateWorldMatrix(const Matrix4f& worldMatrix, const Matrix4f& invWorldMatrix)
{
m_worldMatrix = worldMatrix;
m_invWorldMatrix = invWorldMatrix;

View File

@ -27,6 +27,7 @@ namespace Nz
template<typename T> constexpr T Approach(T value, T objective, T increment);
template<typename T> constexpr T Clamp(T value, T min, T max);
template<typename T, AngleUnit Unit> constexpr Angle<Unit, T> Clamp(Angle<Unit, T> value, T min, T max);
template<typename T> T ClearBit(T number, T bit);
template<typename T> constexpr std::size_t CountBits(T value);
template<typename T> constexpr T DegreeToRadian(T degrees);
template<typename T> constexpr T GetNearestPowerOfTwo(T number);
@ -51,6 +52,7 @@ namespace Nz
template<typename T> T SetBit(T number, T bit);
inline long long StringToNumber(const std::string_view& str, UInt8 radix = 10, bool* ok = nullptr);
template<typename T> bool TestBit(T number, T bit);
template<typename T> T ToggleBit(T number, T bit);
}
#include <Nazara/Math/Algorithm.inl>

View File

@ -172,6 +172,13 @@ namespace Nz
return std::max(std::min(value.value, max), min);
}
template<typename T>
T ClearBit(T number, T bit)
{
NazaraAssert(bit < sizeof(number) * CHAR_BIT, "bit index out of range");
return number &= ~(T(1) << bit);
}
/*!
* \ingroup math
* \brief Gets number of bits set in the number
@ -584,7 +591,7 @@ namespace Nz
template<typename T>
T SetBit(T number, T bit)
{
NazaraAssert(bit < sizeof(number)* CHAR_BIT, "bit index out of range");
NazaraAssert(bit < sizeof(number) * CHAR_BIT, "bit index out of range");
return number |= (T(1) << bit);
}
@ -659,7 +666,13 @@ namespace Nz
NazaraAssert(bit < sizeof(number) * CHAR_BIT, "bit index out of range");
return number & (T(1) << bit);
}
template<typename T>
T ToggleBit(T number, T bit)
{
NazaraAssert(bit < sizeof(number) * CHAR_BIT, "bit index out of range");
return number ^= (T(1) << bit);
}
}
#include <Nazara/Core/DebugOff.hpp>
#include "Algorithm.hpp"

View File

@ -42,8 +42,6 @@
#include <Nazara/Network/NetBuffer.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/Network.hpp>
#include <Nazara/Network/RUdpConnection.hpp>
#include <Nazara/Network/RUdpMessage.hpp>
#include <Nazara/Network/SocketHandle.hpp>
#include <Nazara/Network/SocketPoller.hpp>
#include <Nazara/Network/TcpClient.hpp>

View File

@ -21,7 +21,7 @@ namespace Nz
{
struct Binding
{
UInt32 setIndex;
UInt32 setIndex = 0;
UInt32 bindingIndex;
ShaderBindingType type;
ShaderStageTypeFlags shaderStageFlags;

View File

@ -1,20 +1,23 @@
{
"buffers": [
{
"bindingIndex": 5,
"bindingIndex": 0,
"name": "viewerData",
"setIndex": 0,
"structIndex": 2,
"type": "UniformBufferObject"
},
{
"bindingIndex": 4,
"bindingIndex": 0,
"name": "instanceData",
"setIndex": 1,
"structIndex": 1,
"type": "UniformBufferObject"
},
{
"bindingIndex": 3,
"bindingIndex": 0,
"name": "settings",
"setIndex": 2,
"structIndex": 0,
"type": "UniformBufferObject"
}
@ -43,12 +46,30 @@
"out_id": "{becdd0d4-2b28-44f5-86c2-2ed6b846326c}",
"out_index": 0
},
{
"in_id": "{92d95fe0-84f6-4d27-91ea-992d5f73c04e}",
"in_index": 1,
"out_id": "{f5a6874b-0559-4fd1-9836-27567f9696a4}",
"out_index": 0
},
{
"in_id": "{92d95fe0-84f6-4d27-91ea-992d5f73c04e}",
"in_index": 0,
"out_id": "{359a78e1-df0d-467f-907e-7bff04a55db5}",
"out_index": 0
},
{
"in_id": "{93fdbb4c-bc81-4100-89a9-b465793099b9}",
"in_index": 0,
"out_id": "{6fcfbcd0-c2df-41dd-bb50-74b455b9021f}",
"out_index": 0
},
{
"in_id": "{359a78e1-df0d-467f-907e-7bff04a55db5}",
"in_index": 1,
"out_id": "{becdd0d4-2b28-44f5-86c2-2ed6b846326c}",
"out_index": 1
},
{
"in_id": "{bed466d8-5ed0-4e8a-bba7-1c809cb4c3f7}",
"in_index": 1,
@ -56,9 +77,21 @@
"out_index": 0
},
{
"in_id": "{93fdbb4c-bc81-4100-89a9-b465793099b9}",
"in_id": "{fc7542b2-5752-4891-98c1-35b498da257b}",
"in_index": 0,
"out_id": "{6fcfbcd0-c2df-41dd-bb50-74b455b9021f}",
"out_id": "{bb071807-e65e-4c31-acf0-d296efa665fa}",
"out_index": 3
},
{
"in_id": "{d7acd173-9188-43b5-bfa1-31f17dff44ad}",
"in_index": 1,
"out_id": "{1f9d52d7-4f44-4d96-8edb-fbc1239a93bb}",
"out_index": 0
},
{
"in_id": "{7750a050-b116-4e1b-bd89-b194c366d256}",
"in_index": 1,
"out_id": "{ca2c2ac5-39e0-4814-9432-fbf3e20d3cad}",
"out_index": 0
},
{
@ -74,9 +107,15 @@
"out_index": 0
},
{
"in_id": "{92d95fe0-84f6-4d27-91ea-992d5f73c04e}",
"in_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"in_index": 0,
"out_id": "{ac98a68f-0160-4189-af31-b8278e7c119c}",
"out_index": 0
},
{
"in_id": "{e1f86d56-eb21-4267-9075-e6b0cc875a6d}",
"in_index": 1,
"out_id": "{f5a6874b-0559-4fd1-9836-27567f9696a4}",
"out_id": "{07a43c79-67e2-46b1-87d4-e00d2da22820}",
"out_index": 0
},
{
@ -98,21 +137,9 @@
"out_index": 3
},
{
"in_id": "{359a78e1-df0d-467f-907e-7bff04a55db5}",
"in_index": 2,
"out_id": "{becdd0d4-2b28-44f5-86c2-2ed6b846326c}",
"out_index": 2
},
{
"in_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"in_id": "{d7acd173-9188-43b5-bfa1-31f17dff44ad}",
"in_index": 0,
"out_id": "{ac98a68f-0160-4189-af31-b8278e7c119c}",
"out_index": 0
},
{
"in_id": "{7750a050-b116-4e1b-bd89-b194c366d256}",
"in_index": 1,
"out_id": "{ca2c2ac5-39e0-4814-9432-fbf3e20d3cad}",
"out_id": "{fc7542b2-5752-4891-98c1-35b498da257b}",
"out_index": 0
},
{
@ -121,36 +148,24 @@
"out_id": "{d7acd173-9188-43b5-bfa1-31f17dff44ad}",
"out_index": 0
},
{
"in_id": "{359a78e1-df0d-467f-907e-7bff04a55db5}",
"in_index": 2,
"out_id": "{becdd0d4-2b28-44f5-86c2-2ed6b846326c}",
"out_index": 2
},
{
"in_id": "{cf0ae20a-88cd-4788-9ed7-eaf014d8f971}",
"in_index": 0,
"out_id": "{c41cd67b-2f34-4ec4-acc6-2f7285e7c6e3}",
"out_index": 0
},
{
"in_id": "{e1f86d56-eb21-4267-9075-e6b0cc875a6d}",
"in_index": 1,
"out_id": "{07a43c79-67e2-46b1-87d4-e00d2da22820}",
"out_index": 0
},
{
"in_id": "{359a78e1-df0d-467f-907e-7bff04a55db5}",
"in_index": 1,
"out_id": "{becdd0d4-2b28-44f5-86c2-2ed6b846326c}",
"out_index": 1
},
{
"in_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"in_index": 1,
"out_id": "{db10f064-504d-4072-a49e-51a061b2efbe}",
"out_index": 0
},
{
"in_id": "{d7acd173-9188-43b5-bfa1-31f17dff44ad}",
"in_index": 1,
"out_id": "{1f9d52d7-4f44-4d96-8edb-fbc1239a93bb}",
"out_index": 0
},
{
"in_id": "{bed466d8-5ed0-4e8a-bba7-1c809cb4c3f7}",
"in_index": 0,
@ -187,23 +202,11 @@
"out_id": "{7750a050-b116-4e1b-bd89-b194c366d256}",
"out_index": 0
},
{
"in_id": "{d7acd173-9188-43b5-bfa1-31f17dff44ad}",
"in_index": 0,
"out_id": "{fc7542b2-5752-4891-98c1-35b498da257b}",
"out_index": 0
},
{
"in_id": "{fc7542b2-5752-4891-98c1-35b498da257b}",
"in_index": 1,
"out_id": "{743930bd-1d81-4d4c-b7ec-175a34838d69}",
"out_index": 0
},
{
"in_id": "{fc7542b2-5752-4891-98c1-35b498da257b}",
"in_index": 0,
"out_id": "{bb071807-e65e-4c31-acf0-d296efa665fa}",
"out_index": 3
}
],
"inputs": [
@ -671,18 +674,15 @@
],
"textures": [
{
"bindingIndex": 0,
"bindingIndex": 2,
"name": "MaterialAlphaMap",
"setIndex": 2,
"type": "Sampler2D"
},
{
"bindingIndex": 1,
"name": "MaterialDiffuseMap",
"type": "Sampler2D"
},
{
"bindingIndex": 2,
"name": "TextureOverlay",
"setIndex": 2,
"type": "Sampler2D"
}
],

View File

@ -1,20 +1,23 @@
{
"buffers": [
{
"bindingIndex": 5,
"bindingIndex": 0,
"name": "viewerData",
"setIndex": 0,
"structIndex": 2,
"type": "UniformBufferObject"
},
{
"bindingIndex": 4,
"bindingIndex": 0,
"name": "instanceData",
"setIndex": 1,
"structIndex": 1,
"type": "UniformBufferObject"
},
{
"bindingIndex": 3,
"bindingIndex": 0,
"name": "settings",
"setIndex": 2,
"structIndex": 0,
"type": "UniformBufferObject"
}
@ -46,6 +49,12 @@
"out_id": "{33840c70-4e37-4127-bab0-23c4a4cb6d7f}",
"out_index": 0
},
{
"in_id": "{0fc53363-dbce-4874-8de5-5ca05ae038b7}",
"in_index": 0,
"out_id": "{412684ce-0ec2-4db5-964c-10e5b68d43e8}",
"out_index": 0
},
{
"in_id": "{63bb13f0-55e3-451b-860e-568b65e09b04}",
"in_index": 0,
@ -58,12 +67,6 @@
"out_id": "{c3b906bc-d230-4026-a32e-34c00eaf4481}",
"out_index": 0
},
{
"in_id": "{d8f4d14a-c67a-470f-87bf-8f60d9513c3b}",
"in_index": 0,
"out_id": "{d32dfb1d-c8a4-4315-a710-90d2a51f68e8}",
"out_index": 0
},
{
"in_id": "{1bb9712b-8bff-4398-9e4e-fba79a04df0e}",
"in_index": 0,
@ -71,9 +74,9 @@
"out_index": 0
},
{
"in_id": "{0fc53363-dbce-4874-8de5-5ca05ae038b7}",
"in_id": "{d8f4d14a-c67a-470f-87bf-8f60d9513c3b}",
"in_index": 0,
"out_id": "{412684ce-0ec2-4db5-964c-10e5b68d43e8}",
"out_id": "{d32dfb1d-c8a4-4315-a710-90d2a51f68e8}",
"out_index": 0
},
{

View File

@ -1,113 +0,0 @@
{
"buffers": [
],
"conditions": [
],
"connections": [
{
"in_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"in_index": 1,
"out_id": "{db10f064-504d-4072-a49e-51a061b2efbe}",
"out_index": 0
},
{
"in_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"in_index": 0,
"out_id": "{04c30f54-5492-4b70-99fd-d6fe96c023e4}",
"out_index": 0
},
{
"in_id": "{38c8bbb6-6c85-49ff-abfa-e409bf0393ef}",
"in_index": 0,
"out_id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"out_index": 0
}
],
"inputs": [
{
"locationIndex": 0,
"name": "vertUV",
"role": "TexCoord",
"roleIndex": 0,
"type": "Float2"
}
],
"nodes": [
{
"id": "{fbaddbbe-f9cd-4e8d-b7a8-40c10c96f580}",
"model": {
"name": "SampleTexture",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"variable_name": ""
},
"position": {
"x": 167.75,
"y": 473.97222222222194
}
},
{
"id": "{db10f064-504d-4072-a49e-51a061b2efbe}",
"model": {
"input": "vertUV",
"name": "Input",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"variable_name": ""
},
"position": {
"x": -0.19444444444445708,
"y": 554.0000000000001
}
},
{
"id": "{04c30f54-5492-4b70-99fd-d6fe96c023e4}",
"model": {
"name": "Texture",
"preview_enabled": true,
"preview_height": 64,
"preview_width": 64,
"texture": "Texture",
"variable_name": ""
},
"position": {
"x": 21.666666666666668,
"y": 475
}
},
{
"id": "{38c8bbb6-6c85-49ff-abfa-e409bf0393ef}",
"model": {
"name": "Output",
"output": "RenderTarget0",
"preview_enabled": true,
"preview_height": 128,
"preview_width": 128,
"variable_name": ""
},
"position": {
"x": 343,
"y": 482
}
}
],
"outputs": [
{
"locationIndex": 0,
"name": "RenderTarget0",
"type": "Float4"
}
],
"structs": [
],
"textures": [
{
"bindingIndex": 0,
"name": "Texture",
"type": "Sampler2D"
}
],
"type": "Fragment"
}

View File

@ -1,135 +0,0 @@
{
"buffers": [
],
"conditions": [
],
"connections": [
{
"in_id": "{7ac65f09-7f55-4a6e-9380-1bee5213f079}",
"in_index": 0,
"out_id": "{c3b906bc-d230-4026-a32e-34c00eaf4481}",
"out_index": 0
},
{
"in_id": "{63bb13f0-55e3-451b-860e-568b65e09b04}",
"in_index": 0,
"out_id": "{7ac65f09-7f55-4a6e-9380-1bee5213f079}",
"out_index": 0
},
{
"in_id": "{f8e2a7b7-6780-4014-a803-34020084ceed}",
"in_index": 0,
"out_id": "{6d6b0d04-46ea-4f5f-8e0b-0502adfdc149}",
"out_index": 0
}
],
"inputs": [
{
"locationIndex": 0,
"name": "inPos",
"role": "Position",
"roleIndex": 0,
"type": "Float3"
},
{
"locationIndex": 1,
"name": "inTexCoord",
"role": "TexCoord",
"roleIndex": 0,
"type": "Float2"
}
],
"nodes": [
{
"id": "{c3b906bc-d230-4026-a32e-34c00eaf4481}",
"model": {
"input": "inPos",
"name": "Input",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"variable_name": ""
},
"position": {
"x": 216.49999999999997,
"y": 664.1666666666667
}
},
{
"id": "{7ac65f09-7f55-4a6e-9380-1bee5213f079}",
"model": {
"name": "cast_vec4",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"value": [
1,
0,
0,
0
],
"variable_name": ""
},
"position": {
"x": 345,
"y": 668
}
},
{
"id": "{63bb13f0-55e3-451b-860e-568b65e09b04}",
"model": {
"name": "PositionOutputValue",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"variable_name": ""
},
"position": {
"x": 521.6666666666667,
"y": 668.1666666666667
}
},
{
"id": "{6d6b0d04-46ea-4f5f-8e0b-0502adfdc149}",
"model": {
"input": "inTexCoord",
"name": "Input",
"preview_enabled": false,
"preview_height": 64,
"preview_width": 64,
"variable_name": ""
},
"position": {
"x": 208.33333333333334,
"y": 761.666666666667
}
},
{
"id": "{f8e2a7b7-6780-4014-a803-34020084ceed}",
"model": {
"name": "Output",
"output": "vertUV",
"preview_enabled": false,
"preview_height": 128,
"preview_width": 128,
"variable_name": ""
},
"position": {
"x": 492.5,
"y": 765
}
}
],
"outputs": [
{
"locationIndex": 0,
"name": "vertUV",
"type": "Float2"
}
],
"structs": [
],
"textures": [
],
"type": "Vertex"
}

View File

@ -105,7 +105,6 @@ namespace Nz
s_uniformOffsets.totalSize = fieldOffsets.GetSize();
MaterialSettings::Builder settings;
settings.predefinedBinding.fill(MaterialSettings::InvalidIndex);
std::vector<MaterialSettings::UniformVariable> variables;
variables.assign({
@ -127,42 +126,27 @@ namespace Nz
s_textureIndexes.alpha = settings.textures.size();
settings.textures.push_back({
"MaterialAlphaMap",
2,
"Alpha",
ImageType::E2D
});
s_textureIndexes.diffuse = settings.textures.size();
settings.textures.push_back({
"MaterialDiffuseMap",
1,
"Diffuse",
ImageType::E2D
});
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::TexOverlay)] = settings.textures.size();
settings.textures.push_back({
"TextureOverlay",
"Overlay",
ImageType::E2D
});
s_uniformBlockIndex = settings.uniformBlocks.size();
settings.uniformBlocks.assign({
{
fieldOffsets.GetSize(),
"BasicSettings",
"MaterialBasicSettings",
std::move(variables),
std::move(defaultValues)
}
settings.uniformBlocks.push_back({
0,
"BasicSettings",
fieldOffsets.GetSize(),
std::move(variables),
std::move(defaultValues)
});
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::UboInstanceData)] = settings.textures.size() + settings.uniformBlocks.size() + settings.sharedUniformBlocks.size();
settings.sharedUniformBlocks.push_back(PredefinedInstanceData::GetUniformBlock());
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::UboViewerData)] = settings.textures.size() + settings.uniformBlocks.size() + settings.sharedUniformBlocks.size();
settings.sharedUniformBlocks.push_back(PredefinedViewerData::GetUniformBlock());
// Shaders
auto& fragmentShader = settings.shaders[UnderlyingCast(ShaderStageType::Fragment)];
auto& vertexShader = settings.shaders[UnderlyingCast(ShaderStageType::Vertex)];

View File

@ -51,11 +51,11 @@ namespace Nz
MaterialPipeline::Initialize();
Nz::PredefinedViewerData viewerUboOffsets = Nz::PredefinedViewerData::GetOffsets();
RenderPipelineLayoutInfo referenceLayoutInfo;
FillViewerPipelineLayout(referenceLayoutInfo);
FillWorldPipelineLayout(referenceLayoutInfo);
m_viewerDataUBO = m_renderDevice->InstantiateBuffer(Nz::BufferType::Uniform);
if (!m_viewerDataUBO->Initialize(viewerUboOffsets.totalSize, Nz::BufferUsage::DeviceLocal | Nz::BufferUsage::Dynamic))
throw std::runtime_error("failed to initialize viewer data UBO");
m_referencePipelineLayout = m_renderDevice->InstantiateRenderPipelineLayout(std::move(referenceLayoutInfo));
}
Graphics::~Graphics()
@ -63,5 +63,23 @@ namespace Nz
MaterialPipeline::Uninitialize();
}
void Graphics::FillViewerPipelineLayout(RenderPipelineLayoutInfo& layoutInfo, UInt32 set)
{
layoutInfo.bindings.push_back({
set, 0,
ShaderBindingType::UniformBuffer,
ShaderStageType_All
});
}
void Graphics::FillWorldPipelineLayout(RenderPipelineLayoutInfo& layoutInfo, UInt32 set)
{
layoutInfo.bindings.push_back({
set, 0,
ShaderBindingType::UniformBuffer,
ShaderStageType_All
});
}
Graphics* Graphics::s_instance = nullptr;
}

View File

@ -7,6 +7,7 @@
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/RenderFrame.hpp>
#include <Nazara/Renderer/UploadPool.hpp>
#include <Nazara/Utility/MaterialData.hpp>
#include <Nazara/Graphics/Debug.hpp>
@ -28,6 +29,7 @@ namespace Nz
m_settings(std::move(settings)),
m_enabledConditions(0),
m_pipelineUpdated(false),
m_shaderBindingUpdated(false),
m_shadowCastingEnabled(true)
{
m_pipelineInfo.settings = m_settings;
@ -36,6 +38,9 @@ namespace Nz
for (std::size_t i = 0; i < ShaderStageTypeCount; ++i)
m_pipelineInfo.shaders[i].uberShader = shaders[i];
const auto& textureSettings = m_settings->GetTextures();
const auto& uboSettings = m_settings->GetUniformBlocks();
m_textures.resize(m_settings->GetTextures().size());
m_uniformBuffers.reserve(m_settings->GetUniformBlocks().size());
@ -49,14 +54,28 @@ namespace Nz
assert(uniformBufferInfo.defaultValues.size() <= uniformBufferInfo.blockSize);
uniformBuffer.buffer->Fill(uniformBufferInfo.defaultValues.data(), 0, uniformBufferInfo.defaultValues.size());
uniformBuffer.data.resize(uniformBufferInfo.blockSize);
std::memcpy(uniformBuffer.data.data(), uniformBufferInfo.defaultValues.data(), uniformBufferInfo.defaultValues.size());
}
UpdateShaderBinding();
}
void Material::UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder)
bool Material::Update(RenderFrame& renderFrame, CommandBufferBuilder& builder)
{
bool shouldRegenerateCommandBuffer = false;
if (!m_shaderBindingUpdated)
{
shouldRegenerateCommandBuffer = true;
renderFrame.PushForRelease(std::move(m_shaderBinding));
m_shaderBinding.reset();
UpdateShaderBinding();
}
UploadPool& uploadPool = renderFrame.GetUploadPool();
for (auto& ubo : m_uniformBuffers)
{
if (ubo.dataInvalidated)
@ -69,18 +88,26 @@ namespace Nz
ubo.dataInvalidated = false;
}
}
return shouldRegenerateCommandBuffer;
}
void Material::UpdateShaderBinding(ShaderBinding& shaderBinding) const
void Material::UpdateShaderBinding()
{
assert(!m_shaderBinding);
const auto& textureSettings = m_settings->GetTextures();
const auto& uboSettings = m_settings->GetUniformBlocks();
// TODO: Use StackVector
std::vector<ShaderBinding::Binding> bindings;
std::size_t bindingIndex = 0;
for (const auto& textureSlot : m_textures)
// Textures
for (std::size_t i = 0; i < m_textures.size(); ++i)
{
const auto& textureSetting = textureSettings[i];
const auto& textureSlot = m_textures[i];
if (!textureSlot.sampler)
{
TextureSamplerCache& samplerCache = Graphics::Instance()->GetSamplerCache();
@ -91,26 +118,33 @@ namespace Nz
if (textureSlot.texture)
{
bindings.push_back({
bindingIndex,
textureSetting.bindingIndex,
ShaderBinding::TextureBinding {
textureSlot.texture.get(), textureSlot.sampler.get()
}
});
}
bindingIndex++;
}
for (const auto& ubo : m_uniformBuffers)
// Shared UBO (TODO)
// Owned UBO
for (std::size_t i = 0; i < m_uniformBuffers.size(); ++i)
{
const auto& uboSetting = uboSettings[i];
const auto& uboSlot = m_uniformBuffers[i];
bindings.push_back({
bindingIndex++,
uboSetting.bindingIndex,
ShaderBinding::UniformBufferBinding {
ubo.buffer.get(), 0, ubo.buffer->GetSize()
uboSlot.buffer.get(), 0, uboSlot.buffer->GetSize()
}
});
}
shaderBinding.Update(bindings.data(), bindings.size());
m_shaderBinding = m_settings->GetRenderPipelineLayout()->AllocateShaderBinding(Graphics::MaterialBindingSet);
m_shaderBinding->Update(bindings.data(), bindings.size());
m_shaderBindingUpdated = true;
}
}

View File

@ -4,8 +4,9 @@
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/GraphicalMesh.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/ModelInstance.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Renderer/CommandBufferBuilder.hpp>
#include <Nazara/Graphics/Debug.hpp>
@ -28,9 +29,9 @@ namespace Nz
}
}
void Model::Draw(CommandBufferBuilder& commandBuffer, ModelInstance& instance) const
void Model::Draw(CommandBufferBuilder& commandBuffer, WorldInstance& instance) const
{
commandBuffer.BindShaderBinding(instance.GetShaderBinding());
commandBuffer.BindShaderBinding(Graphics::WorldBindingSet, instance.GetShaderBinding());
for (std::size_t i = 0; i < m_subMeshes.size(); ++i)
{
@ -39,6 +40,7 @@ namespace Nz
const auto& vertexBuffer = m_graphicalMesh->GetVertexBuffer(i);
const auto& renderPipeline = submeshData.material->GetPipeline()->GetRenderPipeline(submeshData.vertexBufferData);
commandBuffer.BindShaderBinding(Graphics::MaterialBindingSet, submeshData.material->GetShaderBinding());
commandBuffer.BindIndexBuffer(indexBuffer.get());
commandBuffer.BindVertexBuffer(0, vertexBuffer.get());
commandBuffer.BindPipeline(*renderPipeline);
@ -57,6 +59,13 @@ namespace Nz
return m_graphicalMesh->GetIndexCount(subMeshIndex);
}
const std::shared_ptr<Material>& Model::GetMaterial(std::size_t subMeshIndex) const
{
assert(subMeshIndex < m_subMeshes.size());
const auto& subMeshData = m_subMeshes[subMeshIndex];
return subMeshData.material;
}
const std::shared_ptr<RenderPipeline>& Model::GetRenderPipeline(std::size_t subMeshIndex) const
{
assert(subMeshIndex < m_subMeshes.size());

View File

@ -152,7 +152,6 @@ namespace Nz
s_phongUniformOffsets.specularColor = phongUniformStruct.AddField(StructFieldType::Float4);
MaterialSettings::Builder settings;
settings.predefinedBinding.fill(MaterialSettings::InvalidIndex);
std::vector<MaterialSettings::UniformVariable> phongVariables;
phongVariables.assign({
@ -189,69 +188,55 @@ namespace Nz
s_phongUniformBlockIndex = settings.uniformBlocks.size();
settings.uniformBlocks.push_back({
phongUniformStruct.GetSize(),
0,
"PhongSettings",
"MaterialPhongSettings",
phongUniformStruct.GetSize(),
std::move(phongVariables),
std::move(defaultValues)
});
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::UboInstanceData)] = settings.sharedUniformBlocks.size();
settings.sharedUniformBlocks.push_back(PredefinedInstanceData::GetUniformBlock());
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::UboLighData)] = settings.sharedUniformBlocks.size();
settings.sharedUniformBlocks.push_back(PredefinedLightData::GetUniformBlock());
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::UboViewerData)] = settings.sharedUniformBlocks.size();
settings.sharedUniformBlocks.push_back(PredefinedViewerData::GetUniformBlock());
s_textureIndexes.alpha = settings.textures.size();
settings.textures.push_back({
"MaterialAlphaMap",
2,
"Alpha",
ImageType::E2D
});
s_textureIndexes.diffuse = settings.textures.size();
settings.textures.push_back({
"MaterialDiffuseMap",
1,
"Diffuse",
ImageType::E2D
});
s_textureIndexes.emissive = settings.textures.size();
settings.textures.push_back({
"MaterialEmissiveMap",
3,
"Emissive",
ImageType::E2D
});
s_textureIndexes.height = settings.textures.size();
settings.textures.push_back({
"MaterialHeightMap",
4,
"Height",
ImageType::E2D
});
s_textureIndexes.normal = settings.textures.size();
settings.textures.push_back({
"MaterialNormalMap",
5,
"Normal",
ImageType::E2D
});
s_textureIndexes.specular = settings.textures.size();
settings.textures.push_back({
"MaterialSpecularMap",
6,
"Specular",
ImageType::E2D
});
settings.predefinedBinding[UnderlyingCast(PredefinedShaderBinding::TexOverlay)] = settings.textures.size();
settings.textures.push_back({
"TextureOverlay",
"Overlay",
ImageType::E2D,
});
s_materialSettings = std::make_shared<MaterialSettings>(std::move(settings));
return true;

View File

@ -46,8 +46,8 @@ namespace Nz
}
MaterialSettings::SharedUniformBlock uniformBlock = {
0, //< FIXME
"Light",
"LightData",
std::move(lightDataVariables)
};
@ -67,31 +67,6 @@ namespace Nz
return instanceData;
}
MaterialSettings::SharedUniformBlock PredefinedInstanceData::GetUniformBlock()
{
PredefinedInstanceData instanceData = GetOffsets();
std::vector<MaterialSettings::UniformVariable> instanceDataVariables;
instanceDataVariables.assign({
{
"WorldMatrix",
instanceData.worldMatrixOffset
},
{
"InvWorldMatrix",
instanceData.invWorldMatrixOffset
},
});
MaterialSettings::SharedUniformBlock uniformBlock = {
"Instance",
"InstanceData",
std::move(instanceDataVariables)
};
return uniformBlock;
}
PredefinedViewerData PredefinedViewerData::GetOffsets()
{
FieldOffsets viewerStruct(StructLayout::Std140);
@ -111,57 +86,4 @@ namespace Nz
return viewerData;
}
MaterialSettings::SharedUniformBlock PredefinedViewerData::GetUniformBlock()
{
PredefinedViewerData viewerData = GetOffsets();
std::vector<MaterialSettings::UniformVariable> viewerDataVariables;
viewerDataVariables.assign({
{
"ProjMatrix",
viewerData.projMatrixOffset
},
{
"InvProjMatrix",
viewerData.invProjMatrixOffset
},
{
"ViewMatrix",
viewerData.viewMatrixOffset
},
{
"InvViewMatrix",
viewerData.invViewMatrixOffset
},
{
"ViewProjMatrix",
viewerData.viewProjMatrixOffset
},
{
"InvViewProjMatrix",
viewerData.invViewProjMatrixOffset
},
{
"TargetSize",
viewerData.targetSizeOffset
},
{
"InvTargetSize",
viewerData.invTargetSizeOffset
},
{
"EyePosition",
viewerData.eyePositionOffset
}
});
MaterialSettings::SharedUniformBlock uniformBlock = {
"Viewer",
"ViewerData",
std::move(viewerDataVariables)
};
return uniformBlock;
}
}

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
78,83,72,82,0,0,0,1,0,0,0,24,0,0,0,7,0,0,0,20,0,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,1,0,0,0,2,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,1,0,0,0,1,0,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,7,0,0,0,4,0,0,0,1,0,0,0,0,20,0,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,1,0,0,0,2,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,105,110,118,87,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,20,0,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,1,0,0,0,9,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,19,105,110,118,80,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,10,118,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,13,105,110,118,86,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,118,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,17,105,110,118,86,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,16,114,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,19,105,110,118,82,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,11,101,121,101,80,111,115,105,116,105,111,110,7,0,0,0,3,0,0,0,1,0,0,0,0,17,0,0,0,0,3,0,0,0,10,118,105,101,119,101,114,68,97,116,97,6,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,5,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,6,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,4,0,0,0,8,115,101,116,116,105,110,103,115,6,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,3,0,0,0,20,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,5,105,110,80,111,115,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,9,105,110,78,111,114,109,97,108,115,7,0,0,0,3,0,0,0,1,2,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,7,0,0,0,2,0,0,0,1,2,0,0,0,2,0,0,0,20,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,10,118,101,114,116,78,111,114,109,97,108,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,6,118,101,114,116,85,86,7,0,0,0,2,0,0,0,1,2,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,7,0,0,0,4,0,0,0,1,1,0,0,0,0,0,0,0,0,18,0,0,0,4,109,97,105,110,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,4,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,5,105,110,112,117,116,2,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,5,0,0,0,21,0,0,0,0,6,111,117,116,112,117,116,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,255,255,255,255,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,10,118,101,114,116,78,111,114,109,97,108,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,9,105,110,78,111,114,109,97,108,115,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,0,0,0,0,9,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,0,0,0,0,0,0,0,9,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,10,118,105,101,119,77,97,116,114,105,120,0,0,0,0,0,0,0,9,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,0,0,0,1,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,0,0,0,6,7,0,0,0,4,0,0,0,1,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,5,105,110,80,111,115,0,0,0,8,0,0,0,1,63,128,0,0,255,255,255,255,255,255,255,255,0,0,0,26,0,0,0,9,0,0,0,6,111,117,116,112,117,116,
78,83,72,82,0,0,0,1,0,0,0,24,0,0,0,7,0,0,0,20,0,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,1,0,0,0,2,0,0,0,14,65,108,112,104,97,84,104,114,101,115,104,111,108,100,1,0,0,0,1,0,0,0,0,12,68,105,102,102,117,115,101,67,111,108,111,114,7,0,0,0,4,0,0,0,1,0,0,0,0,20,0,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,1,0,0,0,2,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,105,110,118,87,111,114,108,100,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,20,0,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,1,0,0,0,9,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,19,105,110,118,80,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,10,118,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,13,105,110,118,86,105,101,119,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,14,118,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,17,105,110,118,86,105,101,119,80,114,111,106,77,97,116,114,105,120,3,0,0,0,4,0,0,0,4,0,0,0,1,0,0,0,0,16,114,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,19,105,110,118,82,101,110,100,101,114,84,97,114,103,101,116,83,105,122,101,7,0,0,0,2,0,0,0,1,0,0,0,0,11,101,121,101,80,111,115,105,116,105,111,110,7,0,0,0,3,0,0,0,1,0,0,0,0,17,0,0,0,0,3,0,0,0,10,118,105,101,119,101,114,68,97,116,97,6,0,0,0,10,86,105,101,119,101,114,68,97,116,97,1,0,0,0,0,1,0,0,0,0,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,6,0,0,0,12,73,110,115,116,97,110,99,101,68,97,116,97,1,0,0,0,0,1,0,0,0,1,0,0,0,8,115,101,116,116,105,110,103,115,6,0,0,0,13,66,97,115,105,99,83,101,116,116,105,110,103,115,1,0,0,0,0,1,0,0,0,2,0,0,0,20,0,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,5,105,110,80,111,115,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,9,105,110,78,111,114,109,97,108,115,7,0,0,0,3,0,0,0,1,2,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,7,0,0,0,2,0,0,0,1,2,0,0,0,2,0,0,0,20,0,0,0,0,10,79,117,116,112,117,116,68,97,116,97,0,0,0,0,3,0,0,0,10,118,101,114,116,78,111,114,109,97,108,7,0,0,0,3,0,0,0,1,2,0,0,0,0,0,0,0,6,118,101,114,116,85,86,7,0,0,0,2,0,0,0,1,2,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,7,0,0,0,4,0,0,0,1,1,0,0,0,0,0,0,0,0,18,0,0,0,4,109,97,105,110,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,4,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,5,105,110,112,117,116,2,0,0,0,9,73,110,112,117,116,68,97,116,97,0,0,0,5,0,0,0,21,0,0,0,0,6,111,117,116,112,117,116,2,0,0,0,10,79,117,116,112,117,116,68,97,116,97,255,255,255,255,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,6,118,101,114,116,85,86,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,10,105,110,84,101,120,67,111,111,114,100,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,10,118,101,114,116,78,111,114,109,97,108,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,9,105,110,78,111,114,109,97,108,115,0,0,0,23,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,6,111,117,116,112,117,116,0,0,0,1,0,0,0,8,112,111,115,105,116,105,111,110,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,3,0,0,0,2,0,0,0,0,0,0,0,9,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,16,112,114,111,106,101,99,116,105,111,110,77,97,116,114,105,120,0,0,0,0,0,0,0,9,0,0,0,10,118,105,101,119,101,114,68,97,116,97,0,0,0,1,0,0,0,10,118,105,101,119,77,97,116,114,105,120,0,0,0,0,0,0,0,9,0,0,0,12,105,110,115,116,97,110,99,101,68,97,116,97,0,0,0,1,0,0,0,11,119,111,114,108,100,77,97,116,114,105,120,0,0,0,6,7,0,0,0,4,0,0,0,1,0,0,0,0,0,0,0,9,0,0,0,5,105,110,112,117,116,0,0,0,1,0,0,0,5,105,110,80,111,115,0,0,0,8,0,0,0,1,63,128,0,0,255,255,255,255,255,255,255,255,0,0,0,26,0,0,0,9,0,0,0,6,111,117,116,112,117,116,

View File

@ -20,27 +20,47 @@ namespace Nz
m_projectionMatrix(Nz::Matrix4f::Identity()),
m_viewProjMatrix(Nz::Matrix4f::Identity()),
m_viewMatrix(Nz::Matrix4f::Identity()),
m_targetSize(Nz::Vector2f(0.f, 0.f))
m_targetSize(Nz::Vector2f(0.f, 0.f)),
m_dataInvalided(true)
{
Nz::PredefinedViewerData viewerUboOffsets = Nz::PredefinedViewerData::GetOffsets();
m_viewerDataBuffer = Graphics::Instance()->GetRenderDevice()->InstantiateBuffer(BufferType::Uniform);
if (!m_viewerDataBuffer->Initialize(viewerUboOffsets.totalSize, Nz::BufferUsage::DeviceLocal | Nz::BufferUsage::Dynamic))
throw std::runtime_error("failed to initialize viewer data UBO");
m_shaderBinding = Graphics::Instance()->GetReferencePipelineLayout()->AllocateShaderBinding(Graphics::ViewerBindingSet);
m_shaderBinding->Update({
{
0,
ShaderBinding::UniformBufferBinding {
m_viewerDataBuffer.get(), 0, m_viewerDataBuffer->GetSize()
}
}
});
}
void ViewerInstance::UpdateViewBuffer(UploadPool& uploadPool, CommandBufferBuilder& builder)
void ViewerInstance::UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder)
{
Nz::PredefinedViewerData viewerDataOffsets = Nz::PredefinedViewerData::GetOffsets();
if (m_dataInvalided)
{
Nz::PredefinedViewerData viewerDataOffsets = Nz::PredefinedViewerData::GetOffsets();
auto& allocation = uploadPool.Allocate(viewerDataOffsets.totalSize);
Nz::AccessByOffset<Nz::Vector3f&>(allocation.mappedPtr, viewerDataOffsets.eyePositionOffset) = m_viewMatrix.GetTranslation();
Nz::AccessByOffset<Nz::Vector2f&>(allocation.mappedPtr, viewerDataOffsets.invTargetSizeOffset) = 1.f / m_targetSize;
Nz::AccessByOffset<Nz::Vector2f&>(allocation.mappedPtr, viewerDataOffsets.targetSizeOffset) = m_targetSize;
auto& allocation = uploadPool.Allocate(viewerDataOffsets.totalSize);
Nz::AccessByOffset<Nz::Vector3f&>(allocation.mappedPtr, viewerDataOffsets.eyePositionOffset) = m_viewMatrix.GetTranslation();
Nz::AccessByOffset<Nz::Vector2f&>(allocation.mappedPtr, viewerDataOffsets.invTargetSizeOffset) = 1.f / m_targetSize;
Nz::AccessByOffset<Nz::Vector2f&>(allocation.mappedPtr, viewerDataOffsets.targetSizeOffset) = m_targetSize;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invProjMatrixOffset) = m_invProjectionMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invViewMatrixOffset) = m_invViewMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invViewProjMatrixOffset) = m_invViewProjMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.projMatrixOffset) = m_projectionMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewProjMatrixOffset) = m_viewProjMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewMatrixOffset) = m_viewMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invProjMatrixOffset) = m_invProjectionMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invViewMatrixOffset) = m_invViewMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.invViewProjMatrixOffset) = m_invViewProjMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.projMatrixOffset) = m_projectionMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewProjMatrixOffset) = m_viewProjMatrix;
Nz::AccessByOffset<Nz::Matrix4f&>(allocation.mappedPtr, viewerDataOffsets.viewMatrixOffset) = m_viewMatrix;
const std::shared_ptr<AbstractBuffer>& instanceDataUBO = Graphics::Instance()->GetViewerDataUBO();
builder.CopyBuffer(allocation, instanceDataUBO.get());
builder.CopyBuffer(allocation, m_viewerDataBuffer.get());
m_dataInvalided = false;
}
}
}

View File

@ -2,7 +2,7 @@
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ModelInstance.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Core/StackVector.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/MaterialSettings.hpp>
@ -13,7 +13,7 @@
namespace Nz
{
ModelInstance::ModelInstance(const std::shared_ptr<const MaterialSettings>& settings) :
WorldInstance::WorldInstance() :
m_invWorldMatrix(Nz::Matrix4f::Identity()),
m_worldMatrix(Nz::Matrix4f::Identity()),
m_dataInvalided(true)
@ -24,37 +24,18 @@ namespace Nz
if (!m_instanceDataBuffer->Initialize(instanceUboOffsets.totalSize, Nz::BufferUsage::DeviceLocal | Nz::BufferUsage::Dynamic))
throw std::runtime_error("failed to initialize viewer data UBO");
m_shaderBinding = settings->GetRenderPipelineLayout()->AllocateShaderBinding();
StackVector<ShaderBinding::Binding> bindings = NazaraStackVector(ShaderBinding::Binding, 2);
if (std::size_t bindingIndex = settings->GetPredefinedBindingIndex(PredefinedShaderBinding::UboInstanceData); bindingIndex != MaterialSettings::InvalidIndex)
{
bindings.push_back({
bindingIndex,
m_shaderBinding = Graphics::Instance()->GetReferencePipelineLayout()->AllocateShaderBinding(Graphics::WorldBindingSet);
m_shaderBinding->Update({
{
0,
ShaderBinding::UniformBufferBinding {
m_instanceDataBuffer.get(), 0, m_instanceDataBuffer->GetSize()
}
});
}
if (std::size_t bindingIndex = settings->GetPredefinedBindingIndex(PredefinedShaderBinding::UboViewerData); bindingIndex != MaterialSettings::InvalidIndex)
{
const std::shared_ptr<AbstractBuffer>& instanceDataUBO = Graphics::Instance()->GetViewerDataUBO();
bindings.push_back({
bindingIndex,
ShaderBinding::UniformBufferBinding {
instanceDataUBO.get(), 0, instanceDataUBO->GetSize()
}
});
}
if (!bindings.empty())
m_shaderBinding->Update(bindings.data(), bindings.size());
}
});
}
void ModelInstance::UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder)
void WorldInstance::UpdateBuffers(UploadPool& uploadPool, CommandBufferBuilder& builder)
{
if (m_dataInvalided)
{

View File

@ -246,7 +246,14 @@ namespace Nz
unsigned int setIndex = 0;
for (const auto& [pipelineLayout, shaderBinding] : states.shaderBindings)
shaderBinding->Apply(*pipelineLayout, setIndex++, context);
{
if (shaderBinding)
shaderBinding->Apply(*pipelineLayout, setIndex, context);
else
NazaraWarning("no shader binding for set #" + std::to_string(setIndex));
setIndex++;
}
if (states.scissorRegion)
context.SetScissorBox(states.scissorRegion->x, states.scissorRegion->y, states.scissorRegion->width, states.scissorRegion->height);