MXVK Vulkan Framework 0.24.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 538 of file room.cpp.

Member Function Documentation

◆ cleanup() [1/2]

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

Definition at line 605 of file room.cpp.

605 {
606 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
607 return;
608 }
609
610 window = targetWindow;
611 destroyPipeline();
612 destroyDescriptors();
613 destroyTexture();
614 destroyBuffers();
615 window = nullptr;
616 }
VkDevice getDevice() const noexcept
Get the Vulkan logical device handle.
Definition mxvk.hpp:168

◆ cleanup() [2/2]

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

Definition at line 607 of file room.cpp.

607 {
608 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
609 return;
610 }
611
612 window = targetWindow;
613 destroyPipeline();
614 destroyDescriptors();
615 destroyTexture();
616 destroyBuffers();
617 window = nullptr;
618 }

◆ 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 552 of file room.cpp.

556 {
557 if (targetWindow == nullptr) {
558 throw mxvk::Exception("walk: raw pillar renderer requires a valid window");
559 }
560 window = targetWindow;
561 vertexSpv = vertSpv;
562 fragmentSpv = fragSpv;
563
564 if (!window->ensureRenderResources()) {
565 throw mxvk::Exception("walk: raw pillar renderer requires render resources");
566 }
567
568 buildGeometry();
569 loadTexture(textureManifestPath, textureBasePath);
570 createTextureSampler();
571 createDescriptorSetLayout();
572 createUniformBuffers();
573 createDescriptorPool();
574 createDescriptorSets();
575 createPipeline();
576 }

◆ 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 554 of file room.cpp.

558 {
559 if (targetWindow == nullptr) {
560 throw mxvk::Exception("walk: raw pillar renderer requires a valid window");
561 }
562 window = targetWindow;
563 vertexSpv = vertSpv;
564 fragmentSpv = fragSpv;
565
566 if (!window->ensureRenderResources()) {
567 throw mxvk::Exception("walk: raw pillar renderer requires render resources");
568 }
569
570 buildGeometry();
571 loadTexture(textureManifestPath, textureBasePath);
572 createTextureSampler();
573 createDescriptorSetLayout();
574 createUniformBuffers();
575 createDescriptorPool();
576 createDescriptorSets();
577 createPipeline();
578 }

◆ 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 595 of file room.cpp.

595 {
596 if (window == nullptr || window->getDevice() == VK_NULL_HANDLE || newFragSpv.empty()) {
597 return;
598 }
599 vkDeviceWaitIdle(window->getDevice());
600 fragmentSpv = newFragSpv;
601 destroyPipeline();
602 createPipeline();
603 }

◆ 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 597 of file room.cpp.

597 {
598 if (window == nullptr || window->getDevice() == VK_NULL_HANDLE || newFragSpv.empty()) {
599 return;
600 }
601 vkDeviceWaitIdle(window->getDevice());
602 fragmentSpv = newFragSpv;
603 destroyPipeline();
604 createPipeline();
605 }

◆ 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 618 of file room.cpp.

623 {
624 if (cmd == VK_NULL_HANDLE || pipeline == VK_NULL_HANDLE || pipelineLayout == VK_NULL_HANDLE) {
625 return;
626 }
627 if (imageIndex >= uniformBuffersMapped.size() || descriptorSets.empty() || vertexBuffer == VK_NULL_HANDLE || indexBuffer == VK_NULL_HANDLE) {
628 return;
629 }
630
631 PillarUniforms uniforms{};
632 uniforms.view = view;
633 uniforms.proj = proj;
634 uniforms.fx = fx;
635 std::memcpy(uniformBuffersMapped[imageIndex], &uniforms, sizeof(PillarUniforms));
636
637 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
638 vkCmdBindDescriptorSets(cmd,
639 VK_PIPELINE_BIND_POINT_GRAPHICS,
640 pipelineLayout,
641 0,
642 1,
643 &descriptorSets[imageIndex],
644 0,
645 nullptr);
646
647 const VkDeviceSize offset = 0;
648 vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, &offset);
649 vkCmdBindIndexBuffer(cmd, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
650
651 for (const PillarInstance &pillar : pillars) {
652 // Vertex data is defined with Y in [0..1] (base at 0.0, top at 1.0).
653 // To avoid z-fighting with the floor, sink the base slightly into the floor
654 // and place the translation at the pillar base Y.
655 constexpr float baseSink = 0.02f;
656 glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pillar.position.x, pillar.position.y - baseSink, pillar.position.z));
657 model = glm::scale(model, glm::vec3(pillar.radius, pillar.height, pillar.radius));
658 vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &model);
659 vkCmdDrawIndexed(cmd, indexCount, 1, 0, 0, 0);
660 }
661 }

◆ 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 620 of file room.cpp.

625 {
626 if (cmd == VK_NULL_HANDLE || pipeline == VK_NULL_HANDLE || pipelineLayout == VK_NULL_HANDLE) {
627 return;
628 }
629 if (imageIndex >= uniformBuffersMapped.size() || descriptorSets.empty() || vertexBuffer == VK_NULL_HANDLE || indexBuffer == VK_NULL_HANDLE) {
630 return;
631 }
632
633 PillarUniforms uniforms{};
634 uniforms.view = view;
635 uniforms.proj = proj;
636 uniforms.fx = fx;
637 std::memcpy(uniformBuffersMapped[imageIndex], &uniforms, sizeof(PillarUniforms));
638
639 vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
640 vkCmdBindDescriptorSets(cmd,
641 VK_PIPELINE_BIND_POINT_GRAPHICS,
642 pipelineLayout,
643 0,
644 1,
645 &descriptorSets[imageIndex],
646 0,
647 nullptr);
648
649 const VkDeviceSize offset = 0;
650 vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, &offset);
651 vkCmdBindIndexBuffer(cmd, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
652
653 for (const PillarInstance &pillar : pillars) {
654 // Vertex data is defined with Y in [0..1] (base at 0.0, top at 1.0).
655 // To avoid z-fighting with the floor, sink the base slightly into the floor
656 // and place the translation at the pillar base Y.
657 constexpr float baseSink = 0.02f;
658 glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pillar.position.x, pillar.position.y - baseSink, pillar.position.z));
659 model = glm::scale(model, glm::vec3(pillar.radius, pillar.height, pillar.radius));
660 vkCmdPushConstants(cmd, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &model);
661 vkCmdDrawIndexed(cmd, indexCount, 1, 0, 0, 0);
662 }
663 }

◆ resize() [1/2]

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

Definition at line 578 of file room.cpp.

578 {
579 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
580 return;
581 }
582
583 window = targetWindow;
584 destroyPipeline();
585 destroyDescriptors();
586 createDescriptorSetLayout();
587 createUniformBuffers();
588 createDescriptorPool();
589 createDescriptorSets();
590 createPipeline();
591 }

◆ resize() [2/2]

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

Definition at line 580 of file room.cpp.

580 {
581 if (targetWindow == nullptr || targetWindow->getDevice() == VK_NULL_HANDLE) {
582 return;
583 }
584
585 window = targetWindow;
586 destroyPipeline();
587 destroyDescriptors();
588 createDescriptorSetLayout();
589 createUniformBuffers();
590 createDescriptorPool();
591 createDescriptorSets();
592 createPipeline();
593 }

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