Compare commits

..

No commits in common. "3125bb9c950a740e9e224efd42e82b3dfb988145" and "06e42ead575675a0018032d057af122e2fbb0d67" have entirely different histories.

3 changed files with 26 additions and 29 deletions

View File

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

View File

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

View File

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