MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
walk::RawPillarRenderer Class Reference

Classes

struct  PillarUniforms
struct  PillarVertex

Public Member Functions

void cleanup (mxvk::VK_Window *targetWindow)
void cleanup (mxvk::VK_Window *targetWindow)
void load (mxvk::VK_Window *targetWindow, const std::string &textureManifestPath, const std::string &textureBasePath, const std::vector< char > &vertSpv, const std::vector< char > &fragSpv)
void load (mxvk::VK_Window *targetWindow, const std::string &textureManifestPath, const std::string &textureBasePath, const std::vector< char > &vertSpv, const std::vector< char > &fragSpv)
void reloadFragShader (const std::vector< char > &newFragSpv)
 Hot-swap the fragment shader without rebuilding geometry or descriptors.
void reloadFragShader (const std::vector< char > &newFragSpv)
 Hot-swap the fragment shader without rebuilding geometry or descriptors.
void render (VkCommandBuffer cmd, uint32_t imageIndex, const std::vector< PillarInstance > &pillars, const glm::mat4 &view, const glm::mat4 &proj, const glm::vec4 &fx)
void render (VkCommandBuffer cmd, uint32_t imageIndex, const std::vector< PillarInstance > &pillars, const glm::mat4 &view, const glm::mat4 &proj, const glm::vec4 &fx)
void resize (mxvk::VK_Window *targetWindow)
void resize (mxvk::VK_Window *targetWindow)

Detailed Description

Definition at line 532 of file room.cpp.

Member Function Documentation

◆ cleanup() [1/2]

void walk::RawPillarRenderer::cleanup ( mxvk::VK_Window * targetWindow)
inline

Definition at line 595 of file room.cpp.

595 {
596 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
597 return;
598 }
599
600 window = targetWindow;
601 destroyPipeline();
602 destroyDescriptors();
603 destroyTexture();
604 destroyBuffers();
605 window = nullptr;
606 }
VkDevice getDevice() const noexcept
Get the Vulkan logical device handle.
Definition mxvk.hpp:198

◆ cleanup() [2/2]

void walk::RawPillarRenderer::cleanup ( mxvk::VK_Window * targetWindow)
inline

Definition at line 597 of file room.cpp.

597 {
598 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
599 return;
600 }
601
602 window = targetWindow;
603 destroyPipeline();
604 destroyDescriptors();
605 destroyTexture();
606 destroyBuffers();
607 window = nullptr;
608 }

◆ load() [1/2]

void walk::RawPillarRenderer::load ( mxvk::VK_Window * targetWindow,
const std::string & textureManifestPath,
const std::string & textureBasePath,
const std::vector< char > & vertSpv,
const std::vector< char > & fragSpv )
inline

Definition at line 546 of file room.cpp.

546 {
547 if (targetWindow == nullptr) {
548 throw mxvk::Exception("walk: raw pillar renderer requires a valid window");
549 }
550 window = targetWindow;
551 vertexSpv = vertSpv;
552 fragmentSpv = fragSpv;
553
554 if (!window->ensureRenderResources()) {
555 throw mxvk::Exception("walk: raw pillar renderer requires render resources");
556 }
557
558 buildGeometry();
559 loadTexture(textureManifestPath, textureBasePath);
560 createTextureSampler();
561 createDescriptorSetLayout();
562 createUniformBuffers();
563 createDescriptorPool();
564 createDescriptorSets();
565 createPipeline();
566 }

◆ load() [2/2]

void walk::RawPillarRenderer::load ( mxvk::VK_Window * targetWindow,
const std::string & textureManifestPath,
const std::string & textureBasePath,
const std::vector< char > & vertSpv,
const std::vector< char > & fragSpv )
inline

Definition at line 548 of file room.cpp.

548 {
549 if (targetWindow == nullptr) {
550 throw mxvk::Exception("walk: raw pillar renderer requires a valid window");
551 }
552 window = targetWindow;
553 vertexSpv = vertSpv;
554 fragmentSpv = fragSpv;
555
556 if (!window->ensureRenderResources()) {
557 throw mxvk::Exception("walk: raw pillar renderer requires render resources");
558 }
559
560 buildGeometry();
561 loadTexture(textureManifestPath, textureBasePath);
562 createTextureSampler();
563 createDescriptorSetLayout();
564 createUniformBuffers();
565 createDescriptorPool();
566 createDescriptorSets();
567 createPipeline();
568 }

◆ reloadFragShader() [1/2]

void walk::RawPillarRenderer::reloadFragShader ( const std::vector< char > & newFragSpv)
inline

Hot-swap the fragment shader without rebuilding geometry or descriptors.

Parameters
newFragSpvCompiled SPIR-V bytecode for the new fragment shader.

Definition at line 585 of file room.cpp.

585 {
586 if (window == nullptr || window->getDevice() == VK_NULL_HANDLE || newFragSpv.empty()) {
587 return;
588 }
589 vkDeviceWaitIdle(window->getDevice());
590 fragmentSpv = newFragSpv;
591 destroyPipeline();
592 createPipeline();
593 }

◆ reloadFragShader() [2/2]

void walk::RawPillarRenderer::reloadFragShader ( const std::vector< char > & newFragSpv)
inline

Hot-swap the fragment shader without rebuilding geometry or descriptors.

Parameters
newFragSpvCompiled SPIR-V bytecode for the new fragment shader.

Definition at line 587 of file room.cpp.

587 {
588 if (window == nullptr || window->getDevice() == VK_NULL_HANDLE || newFragSpv.empty()) {
589 return;
590 }
591 vkDeviceWaitIdle(window->getDevice());
592 fragmentSpv = newFragSpv;
593 destroyPipeline();
594 createPipeline();
595 }

◆ render() [1/2]

void walk::RawPillarRenderer::render ( VkCommandBuffer cmd,
uint32_t imageIndex,
const std::vector< PillarInstance > & pillars,
const glm::mat4 & view,
const glm::mat4 & proj,
const glm::vec4 & fx )
inline

Definition at line 608 of file room.cpp.

608 {
609 if (cmd == VK_NULL_HANDLE || pipeline == VK_NULL_HANDLE || pipelineLayout == VK_NULL_HANDLE) {
610 return;
611 }
612 if (imageIndex >= uniformBuffersMapped.size() || descriptorSets.empty() || vertexBuffer == VK_NULL_HANDLE || indexBuffer == VK_NULL_HANDLE) {
613 return;
614 }
615
616 PillarUniforms uniforms{};
617 uniforms.view = view;
618 uniforms.proj = proj;
619 uniforms.fx = fx;
620 std::memcpy(uniformBuffersMapped[imageIndex], &uniforms, sizeof(PillarUniforms));
621
622 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
623 vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[imageIndex], 0, nullptr);
624
625 const VkDeviceSize offset = 0;
626 vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, &offset);
627 vkCmdBindIndexBuffer(cmd, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
628
629 for (const PillarInstance &pillar : pillars) {
630 // Vertex data is defined with Y in [0..1] (base at 0.0, top at 1.0).
631 // To avoid z-fighting with the floor, sink the base slightly into the floor
632 // and place the translation at the pillar base Y.
633 constexpr float baseSink = 0.02f;
634 glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pillar.position.x, pillar.position.y - baseSink, pillar.position.z));
635 model = glm::scale(model, glm::vec3(pillar.radius, pillar.height, pillar.radius));
636 vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &model);
637 vkCmdDrawIndexed(cmd, indexCount, 1, 0, 0, 0);
638 }
639 }

◆ render() [2/2]

void walk::RawPillarRenderer::render ( VkCommandBuffer cmd,
uint32_t imageIndex,
const std::vector< PillarInstance > & pillars,
const glm::mat4 & view,
const glm::mat4 & proj,
const glm::vec4 & fx )
inline

Definition at line 610 of file room.cpp.

610 {
611 if (cmd == VK_NULL_HANDLE || pipeline == VK_NULL_HANDLE || pipelineLayout == VK_NULL_HANDLE) {
612 return;
613 }
614 if (imageIndex >= uniformBuffersMapped.size() || descriptorSets.empty() || vertexBuffer == VK_NULL_HANDLE || indexBuffer == VK_NULL_HANDLE) {
615 return;
616 }
617
618 PillarUniforms uniforms{};
619 uniforms.view = view;
620 uniforms.proj = proj;
621 uniforms.fx = fx;
622 std::memcpy(uniformBuffersMapped[imageIndex], &uniforms, sizeof(PillarUniforms));
623
624 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
625 vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[imageIndex], 0, nullptr);
626
627 const VkDeviceSize offset = 0;
628 vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, &offset);
629 vkCmdBindIndexBuffer(cmd, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
630
631 for (const PillarInstance &pillar : pillars) {
632 // Vertex data is defined with Y in [0..1] (base at 0.0, top at 1.0).
633 // To avoid z-fighting with the floor, sink the base slightly into the floor
634 // and place the translation at the pillar base Y.
635 constexpr float baseSink = 0.02f;
636 glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pillar.position.x, pillar.position.y - baseSink, pillar.position.z));
637 model = glm::scale(model, glm::vec3(pillar.radius, pillar.height, pillar.radius));
638 vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &model);
639 vkCmdDrawIndexed(cmd, indexCount, 1, 0, 0, 0);
640 }
641 }

◆ resize() [1/2]

void walk::RawPillarRenderer::resize ( mxvk::VK_Window * targetWindow)
inline

Definition at line 568 of file room.cpp.

568 {
569 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
570 return;
571 }
572
573 window = targetWindow;
574 destroyPipeline();
575 destroyDescriptors();
576 createDescriptorSetLayout();
577 createUniformBuffers();
578 createDescriptorPool();
579 createDescriptorSets();
580 createPipeline();
581 }

◆ resize() [2/2]

void walk::RawPillarRenderer::resize ( mxvk::VK_Window * targetWindow)
inline

Definition at line 570 of file room.cpp.

570 {
571 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
572 return;
573 }
574
575 window = targetWindow;
576 destroyPipeline();
577 destroyDescriptors();
578 createDescriptorSetLayout();
579 createUniformBuffers();
580 createDescriptorPool();
581 createDescriptorSets();
582 createPipeline();
583 }

The documentation for this class was generated from the following files: