Compare commits

...

2 Commits

Author SHA1 Message Date
Attila Uygun 3125bb9c95 Activate imgui shader only once. 2023-10-22 23:39:29 +02:00
Attila Uygun 4cc2c03afe Vulkan: Fix for activate-texture 2023-10-22 23:34:38 +02:00
3 changed files with 29 additions and 26 deletions

View File

@ -15,6 +15,10 @@ using namespace base;
namespace eng {
namespace {
const char vertex_description[] = "p2f;t2f;c4b";
} // namespace
ImguiBackend::ImguiBackend() : shader_{std::make_unique<Shader>(nullptr)} {}
ImguiBackend::~ImguiBackend() = default;
@ -54,7 +58,6 @@ void ImguiBackend::CreateRenderResources(Renderer* renderer) {
auto source = std::make_unique<ShaderSource>();
if (source->Load("engine/imgui.glsl")) {
static const char vertex_description[] = "p2f;t2f;c4b";
VertexDescription vd;
if (!ParseVertexDescription(vertex_description, vd)) {
DLOG(0) << "Failed to parse vertex description.";
@ -113,9 +116,12 @@ void ImguiBackend::Render() {
base::Matrix4f ortho_projection;
ortho_projection.CreateOrthoProjection(L, R, B, T);
shader_->Activate();
shader_->SetUniform("projection", ortho_projection);
shader_->UploadUniforms();
for (int n = 0; n < draw_data->CmdListsCount; n++) {
const ImDrawList* cmd_list = draw_data->CmdLists[n];
static const char vertex_description[] = "p2f;t2f;c4b";
auto mesh = std::make_unique<Mesh>();
mesh->Create(kPrimitive_Triangles, vertex_description,
cmd_list->VtxBuffer.Size, cmd_list->VtxBuffer.Data,
@ -127,12 +133,6 @@ void ImguiBackend::Render() {
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
reinterpret_cast<Texture*>(pcmd->GetTexID())->Activate();
// Vulkan renderer needs activating the shader again after a texture.
// TODO: Fix it in vulkan renderer and active the shader only once.
shader_->Activate();
shader_->SetUniform("projection", ortho_projection);
shader_->UploadUniforms();
if (pcmd->ClipRect.z > pcmd->ClipRect.x &&
pcmd->ClipRect.w > pcmd->ClipRect.y) {
renderer_->SetScissor(int(pcmd->ClipRect.x), int(pcmd->ClipRect.y),

View File

@ -564,8 +564,19 @@ void RendererVulkan::ActivateTexture(uint64_t resource_id) {
if (it == textures_.end())
return;
// Keep as pending and bind later in ActivateShader.
pending_descriptor_sets_[/*TODO*/ 0] = std::get<0>(it->second.desc_set);
if (active_descriptor_sets_[/*TODO*/ 0] != std::get<0>(it->second.desc_set)) {
active_descriptor_sets_[/*TODO*/ 0] = std::get<0>(it->second.desc_set);
if (active_shader_id_ != 0) {
auto active_shader = shaders_.find(active_shader_id_);
if (active_shader != shaders_.end() &&
active_shader->second.desc_set_count > 0) {
vkCmdBindDescriptorSets(frames_[current_frame_].draw_command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
active_shader->second.pipeline_layout, 0, 1,
&active_descriptor_sets_[0], 0, nullptr);
}
}
}
}
uint64_t RendererVulkan::CreateShader(
@ -773,19 +784,16 @@ void RendererVulkan::ActivateShader(uint64_t resource_id) {
if (it == shaders_.end())
return;
if (active_pipeline_ != it->second.pipeline) {
active_pipeline_ = it->second.pipeline;
if (active_shader_id_ != resource_id) {
active_shader_id_ = resource_id;
vkCmdBindPipeline(frames_[current_frame_].draw_command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS, it->second.pipeline);
}
for (size_t i = 0; i < it->second.desc_set_count; ++i) {
if (active_descriptor_sets_[i] != pending_descriptor_sets_[i]) {
active_descriptor_sets_[i] = pending_descriptor_sets_[i];
if (it->second.desc_set_count > 0 &&
active_descriptor_sets_[/*TODO*/ 0] != VK_NULL_HANDLE) {
vkCmdBindDescriptorSets(frames_[current_frame_].draw_command_buffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
it->second.pipeline_layout, 0, 1,
&active_descriptor_sets_[i], 0, nullptr);
break;
&active_descriptor_sets_[0], 0, nullptr);
}
}
}
@ -1832,10 +1840,8 @@ bool RendererVulkan::CreatePipelineLayout(
}
}
if (active_descriptor_sets_.size() < shader.desc_set_count) {
if (active_descriptor_sets_.size() < shader.desc_set_count)
active_descriptor_sets_.resize(shader.desc_set_count);
pending_descriptor_sets_.resize(shader.desc_set_count);
}
// Parse push constants.
auto enumerate_pc = [&](SpvReflectShaderModule& module, uint32_t& pc_count,
@ -2047,11 +2053,9 @@ void RendererVulkan::SwapBuffers() {
context_.SwapBuffers();
current_frame_ = (current_frame_ + 1) % frames_.size();
active_pipeline_ = VK_NULL_HANDLE;
active_shader_id_ = 0;
for (auto& ds : active_descriptor_sets_)
ds = VK_NULL_HANDLE;
for (auto& ds : pending_descriptor_sets_)
ds = VK_NULL_HANDLE;
BeginFrame();
}

View File

@ -179,12 +179,11 @@ class RendererVulkan final : public Renderer {
uint64_t max_staging_buffer_size_ = 16 * 1024 * 1024;
bool staging_buffer_used_ = false;
VkPipeline active_pipeline_ = VK_NULL_HANDLE;
uint64_t active_shader_id_ = 0;
std::vector<std::unique_ptr<DescPool>> desc_pools_;
VkDescriptorSetLayout descriptor_set_layout_ = VK_NULL_HANDLE;
std::vector<VkDescriptorSet> active_descriptor_sets_;
std::vector<VkDescriptorSet> pending_descriptor_sets_;
VkSampler sampler_ = VK_NULL_HANDLE;