add ubo to store screen size
This commit is contained in:
parent
9d23816363
commit
8f144a28e1
|
|
@ -66,14 +66,17 @@ namespace Nz
|
||||||
{
|
{
|
||||||
std::shared_ptr<Nz::RenderPipeline> Pipeline;
|
std::shared_ptr<Nz::RenderPipeline> Pipeline;
|
||||||
std::unordered_map<Nz::Texture*, Nz::ShaderBindingPtr> TextureShaderBindings;
|
std::unordered_map<Nz::Texture*, Nz::ShaderBindingPtr> TextureShaderBindings;
|
||||||
|
Nz::ShaderBindingPtr UboShaderBinding;
|
||||||
std::shared_ptr<Nz::TextureSampler> TextureSampler;
|
std::shared_ptr<Nz::TextureSampler> TextureSampler;
|
||||||
} m_texturedPipeline;
|
} m_texturedPipeline;
|
||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
std::shared_ptr<Nz::RenderPipeline> Pipeline;
|
std::shared_ptr<Nz::RenderPipeline> Pipeline;
|
||||||
|
Nz::ShaderBindingPtr UboShaderBinding;
|
||||||
} m_untexturedPipeline;
|
} m_untexturedPipeline;
|
||||||
|
|
||||||
|
std::shared_ptr<Nz::RenderBuffer> m_uboBuffer;
|
||||||
std::shared_ptr<Nz::Texture> m_fontTexture;
|
std::shared_ptr<Nz::Texture> m_fontTexture;
|
||||||
|
|
||||||
static Imgui* s_instance;
|
static Imgui* s_instance;
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,12 @@ namespace
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
struct ImguiUbo
|
||||||
|
{
|
||||||
|
float screenWidth;
|
||||||
|
float screenHeight;
|
||||||
|
};
|
||||||
|
|
||||||
Imgui* Imgui::s_instance = nullptr;
|
Imgui* Imgui::s_instance = nullptr;
|
||||||
|
|
||||||
Imgui::Imgui(Config config)
|
Imgui::Imgui(Config config)
|
||||||
|
|
@ -100,8 +106,10 @@ namespace Nz
|
||||||
|
|
||||||
Imgui::~Imgui()
|
Imgui::~Imgui()
|
||||||
{
|
{
|
||||||
|
m_untexturedPipeline.UboShaderBinding.reset();
|
||||||
m_untexturedPipeline.Pipeline.reset();
|
m_untexturedPipeline.Pipeline.reset();
|
||||||
|
|
||||||
|
m_texturedPipeline.UboShaderBinding.reset();
|
||||||
m_texturedPipeline.TextureShaderBindings.clear();
|
m_texturedPipeline.TextureShaderBindings.clear();
|
||||||
m_texturedPipeline.TextureSampler.reset();
|
m_texturedPipeline.TextureSampler.reset();
|
||||||
m_texturedPipeline.Pipeline.reset();
|
m_texturedPipeline.Pipeline.reset();
|
||||||
|
|
@ -396,6 +404,12 @@ namespace Nz
|
||||||
|
|
||||||
Nz::RenderPipelineLayoutInfo pipelineLayoutInfo;
|
Nz::RenderPipelineLayoutInfo pipelineLayoutInfo;
|
||||||
|
|
||||||
|
auto& uboBinding = pipelineLayoutInfo.bindings.emplace_back();
|
||||||
|
uboBinding.setIndex = 0;
|
||||||
|
uboBinding.bindingIndex = 0;
|
||||||
|
uboBinding.shaderStageFlags = nzsl::ShaderStageType::Vertex;
|
||||||
|
uboBinding.type = Nz::ShaderBindingType::UniformBuffer;
|
||||||
|
|
||||||
auto& textureBinding = pipelineLayoutInfo.bindings.emplace_back();
|
auto& textureBinding = pipelineLayoutInfo.bindings.emplace_back();
|
||||||
textureBinding.setIndex = 1;
|
textureBinding.setIndex = 1;
|
||||||
textureBinding.bindingIndex = 0;
|
textureBinding.bindingIndex = 0;
|
||||||
|
|
@ -424,6 +438,19 @@ namespace Nz
|
||||||
pipelineVertexBuffer.declaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Color_UV);
|
pipelineVertexBuffer.declaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Color_UV);
|
||||||
|
|
||||||
m_texturedPipeline.Pipeline = renderDevice->InstantiateRenderPipeline(pipelineInfo);
|
m_texturedPipeline.Pipeline = renderDevice->InstantiateRenderPipeline(pipelineInfo);
|
||||||
|
|
||||||
|
m_uboBuffer = renderDevice->InstantiateBuffer(Nz::BufferType::Uniform, sizeof(ImguiUbo), Nz::BufferUsage::DeviceLocal | Nz::BufferUsage::Dynamic);
|
||||||
|
|
||||||
|
m_texturedPipeline.UboShaderBinding = renderPipelineLayout->AllocateShaderBinding(0);
|
||||||
|
m_texturedPipeline.UboShaderBinding->Update({
|
||||||
|
{
|
||||||
|
0,
|
||||||
|
Nz::ShaderBinding::UniformBufferBinding {
|
||||||
|
m_uboBuffer.get(), 0, sizeof(ImguiUbo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -449,6 +476,13 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
Nz::RenderPipelineLayoutInfo pipelineLayoutInfo;
|
Nz::RenderPipelineLayoutInfo pipelineLayoutInfo;
|
||||||
|
|
||||||
|
auto& uboBinding = pipelineLayoutInfo.bindings.emplace_back();
|
||||||
|
uboBinding.setIndex = 0;
|
||||||
|
uboBinding.bindingIndex = 0;
|
||||||
|
uboBinding.shaderStageFlags = nzsl::ShaderStageType::Vertex;
|
||||||
|
uboBinding.type = Nz::ShaderBindingType::UniformBuffer;
|
||||||
|
|
||||||
std::shared_ptr<Nz::RenderPipelineLayout> renderPipelineLayout = renderDevice->InstantiateRenderPipelineLayout(std::move(pipelineLayoutInfo));
|
std::shared_ptr<Nz::RenderPipelineLayout> renderPipelineLayout = renderDevice->InstantiateRenderPipelineLayout(std::move(pipelineLayoutInfo));
|
||||||
|
|
||||||
Nz::RenderPipelineInfo pipelineInfo;
|
Nz::RenderPipelineInfo pipelineInfo;
|
||||||
|
|
@ -471,6 +505,16 @@ namespace Nz
|
||||||
pipelineVertexBuffer.declaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Color_UV);
|
pipelineVertexBuffer.declaration = Nz::VertexDeclaration::Get(Nz::VertexLayout::XYZ_Color_UV);
|
||||||
|
|
||||||
m_untexturedPipeline.Pipeline = renderDevice->InstantiateRenderPipeline(pipelineInfo);
|
m_untexturedPipeline.Pipeline = renderDevice->InstantiateRenderPipeline(pipelineInfo);
|
||||||
|
|
||||||
|
m_untexturedPipeline.UboShaderBinding = renderPipelineLayout->AllocateShaderBinding(0);
|
||||||
|
m_untexturedPipeline.UboShaderBinding->Update({
|
||||||
|
{
|
||||||
|
0,
|
||||||
|
Nz::ShaderBinding::UniformBufferBinding {
|
||||||
|
m_uboBuffer.get(), 0, sizeof(ImguiUbo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -489,6 +533,22 @@ namespace Nz
|
||||||
if (fb_width == 0 || fb_height == 0)
|
if (fb_width == 0 || fb_height == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
ImguiUbo ubo{ fb_width / 2.f, fb_height / 2.f };
|
||||||
|
auto& allocation = frame.GetUploadPool().Allocate(sizeof(ImguiUbo));
|
||||||
|
|
||||||
|
std::memcpy(allocation.mappedPtr, &ubo, sizeof(ImguiUbo));
|
||||||
|
|
||||||
|
frame.Execute([&](Nz::CommandBufferBuilder& builder)
|
||||||
|
{
|
||||||
|
builder.BeginDebugRegion("UBO Update", Nz::Color::Yellow);
|
||||||
|
{
|
||||||
|
builder.PreTransferBarrier();
|
||||||
|
builder.CopyBuffer(allocation, m_uboBuffer.get());
|
||||||
|
builder.PostTransferBarrier();
|
||||||
|
}
|
||||||
|
builder.EndDebugRegion();
|
||||||
|
}, Nz::QueueType::Transfer);
|
||||||
|
|
||||||
drawData->ScaleClipRects(io.DisplayFramebufferScale);
|
drawData->ScaleClipRects(io.DisplayFramebufferScale);
|
||||||
|
|
||||||
auto renderDevice = Nz::Graphics::Instance()->GetRenderDevice();
|
auto renderDevice = Nz::Graphics::Instance()->GetRenderDevice();
|
||||||
|
|
@ -553,11 +613,13 @@ namespace Nz
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.BindPipeline(*m_texturedPipeline.Pipeline);
|
builder.BindPipeline(*m_texturedPipeline.Pipeline);
|
||||||
|
builder.BindShaderBinding(0, *m_texturedPipeline.UboShaderBinding);
|
||||||
builder.BindShaderBinding(1, *m_texturedPipeline.TextureShaderBindings[texture]);
|
builder.BindShaderBinding(1, *m_texturedPipeline.TextureShaderBindings[texture]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
builder.BindPipeline(*m_untexturedPipeline.Pipeline);
|
builder.BindPipeline(*m_untexturedPipeline.Pipeline);
|
||||||
|
builder.BindShaderBinding(0, *m_untexturedPipeline.UboShaderBinding);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.SetViewport(Nz::Recti{ 0, 0, fb_width, fb_height });
|
builder.SetViewport(Nz::Recti{ 0, 0, fb_width, fb_height });
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,19 @@ R"(
|
||||||
[nzsl_version("1.0")]
|
[nzsl_version("1.0")]
|
||||||
module;
|
module;
|
||||||
|
|
||||||
|
[layout(std140)]
|
||||||
|
struct Data
|
||||||
|
{
|
||||||
|
halfScreenWidth : f32,
|
||||||
|
halfScreenHeight : f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
[set(0)]
|
||||||
|
external
|
||||||
|
{
|
||||||
|
[binding(0)] data: uniform[Data]
|
||||||
|
}
|
||||||
|
|
||||||
[set(1)]
|
[set(1)]
|
||||||
external
|
external
|
||||||
{
|
{
|
||||||
|
|
@ -40,7 +53,7 @@ fn main(fragIn: VertOut) -> FragOut
|
||||||
fn main(vertIn: VertIn) -> VertOut
|
fn main(vertIn: VertIn) -> VertOut
|
||||||
{
|
{
|
||||||
let vertOut: VertOut;
|
let vertOut: VertOut;
|
||||||
vertOut.position = vec4[f32](vertIn.position, 1.0) / vec4[f32](1280.0 / 2.0, 720.0 / 2.0, 1.0, 1.0) - vec4[f32](1.0,1.0,0.0,0.0);
|
vertOut.position = vec4[f32](vertIn.position, 1.0) / vec4[f32](data.halfScreenWidth, data.halfScreenHeight, 1.0, 1.0) - vec4[f32](1.0,1.0,0.0,0.0);
|
||||||
vertOut.color = vertIn.color;
|
vertOut.color = vertIn.color;
|
||||||
vertOut.uv = vertIn.uv;
|
vertOut.uv = vertIn.uv;
|
||||||
return vertOut;
|
return vertOut;
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,19 @@ R"(
|
||||||
[nzsl_version("1.0")]
|
[nzsl_version("1.0")]
|
||||||
module;
|
module;
|
||||||
|
|
||||||
|
[layout(std140)]
|
||||||
|
struct Data
|
||||||
|
{
|
||||||
|
halfScreenWidth : f32,
|
||||||
|
halfScreenHeight : f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
[set(0)]
|
||||||
|
external
|
||||||
|
{
|
||||||
|
[binding(0)] data: uniform[Data]
|
||||||
|
}
|
||||||
|
|
||||||
struct VertIn
|
struct VertIn
|
||||||
{
|
{
|
||||||
[location(0)] position: vec3[f32],
|
[location(0)] position: vec3[f32],
|
||||||
|
|
@ -34,7 +47,7 @@ fn main(fragIn: VertOut) -> FragOut
|
||||||
fn main(vertIn: VertIn) -> VertOut
|
fn main(vertIn: VertIn) -> VertOut
|
||||||
{
|
{
|
||||||
let vertOut: VertOut;
|
let vertOut: VertOut;
|
||||||
vertOut.position = vec4[f32](vertIn.position, 1.0) / vec4[f32](1280.0 / 2.0, 720.0 / 2.0, 1.0, 1.0) - vec4[f32](1.0,1.0,0.0,0.0);
|
vertOut.position = vec4[f32](vertIn.position, 1.0) / vec4[f32](data.halfScreenWidth, data.halfScreenHeight, 1.0, 1.0) - vec4[f32](1.0,1.0,0.0,0.0);
|
||||||
vertOut.color = vertIn.color;
|
vertOut.color = vertIn.color;
|
||||||
vertOut.uv = vertIn.uv;
|
vertOut.uv = vertIn.uv;
|
||||||
return vertOut;
|
return vertOut;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue