Skip to content

Commit

Permalink
adding a grass shader
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jun 16, 2024
1 parent 85cafaa commit db31a19
Show file tree
Hide file tree
Showing 28 changed files with 533 additions and 108 deletions.
2 changes: 1 addition & 1 deletion application/lucre/models/assets
3 changes: 2 additions & 1 deletion application/lucre/terrainDescriptions/lucre island.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
{
"roughness": 0.5,
"metallic": 0.1
}
},
"grass": "application/lucre/models/assets/grass/grass1.glb"
}
13 changes: 11 additions & 2 deletions engine/platform/Vulkan/VKbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ namespace GfxRenderEngine
virtual void MapBuffer() override;
void Unmap();

void WriteToBuffer(const void* data, VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
VkResult Flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
void WriteToBuffer(const void* data, VkDeviceSize size, VkDeviceSize offset);
virtual void WriteToBuffer(const void* data) override
{
WriteToBuffer(data, VK_WHOLE_SIZE /*VkDeviceSize size*/, 0 /*VkDeviceSize offset*/);
}
VkResult Flush(VkDeviceSize size, VkDeviceSize offset);
virtual bool Flush() override
{
VkResult result = Flush(VK_WHOLE_SIZE /*VkDeviceSize size*/, 0 /*VkDeviceSize offset*/);
return result == VkResult::VK_SUCCESS;
}
VkDescriptorBufferInfo DescriptorInfo(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
VkResult Invalidate(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);

Expand Down
16 changes: 16 additions & 0 deletions engine/platform/Vulkan/VKmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ namespace GfxRenderEngine
}
}

void VK_Model::DrawGrass(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, int instanceCount)
{
for (auto& submesh : m_SubmeshesPbrMap)
{
BindDescriptors(frameInfo, pipelineLayout, submesh, true /*bind resources*/);
PushConstantsPbr(frameInfo, pipelineLayout, submesh);
vkCmdDrawIndexed(frameInfo.m_CommandBuffer, // VkCommandBuffer commandBuffer
submesh.m_IndexCount, // uint32_t indexCount
instanceCount, // uint32_t instanceCount
submesh.m_FirstIndex, // uint32_t firstIndex
submesh.m_FirstVertex, // int32_t vertexOffset
0 // uint32_t firstInstance
);
}
}

void VK_Model::DrawShadowInstanced(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout,
const VkDescriptorSet& shadowDescriptorSet)
{
Expand Down
1 change: 1 addition & 0 deletions engine/platform/Vulkan/VKmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace GfxRenderEngine

// draw pbr materials
void DrawPbr(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout);
void DrawGrass(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout, int instanceCount);

// draw shadow
void DrawShadowInstanced(const VK_FrameInfo& frameInfo, const VkPipelineLayout& pipelineLayout,
Expand Down
30 changes: 28 additions & 2 deletions engine/platform/Vulkan/VKrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace GfxRenderEngine
{
std::shared_ptr<Texture> gTextureSpritesheet;
std::shared_ptr<Texture> gTextureFontAtlas;
std::shared_ptr<Buffer> gDummyBuffer;

std::unique_ptr<VK_DescriptorPool> VK_Renderer::m_DescriptorPool;

Expand Down Expand Up @@ -148,6 +149,13 @@ namespace GfxRenderEngine
.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for animation
.Build();

std::unique_ptr<VK_DescriptorSetLayout> grassResourceDescriptorSetLayout =
VK_DescriptorSetLayout::Builder()
.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances
.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // dummy
.AddBinding(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for height map
.Build();

std::unique_ptr<VK_DescriptorSetLayout> instanceDescriptorSetLayout =
VK_DescriptorSetLayout::Builder()
.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT) // shader data for instances
Expand Down Expand Up @@ -193,6 +201,10 @@ namespace GfxRenderEngine
m_GlobalDescriptorSetLayout, pbrMaterialDescriptorSetLayout->GetDescriptorSetLayout(),
pbrResourceDescriptorSetLayout->GetDescriptorSetLayout()};

std::vector<VkDescriptorSetLayout> descriptorSetLayoutsGrass = {
m_GlobalDescriptorSetLayout, pbrMaterialDescriptorSetLayout->GetDescriptorSetLayout(),
grassResourceDescriptorSetLayout->GetDescriptorSetLayout()};

std::vector<VkDescriptorSetLayout> descriptorSetLayoutsPbrSA = {
m_GlobalDescriptorSetLayout, pbrMaterialDescriptorSetLayout->GetDescriptorSetLayout(),
pbrSAResourceDescriptorSetLayout->GetDescriptorSetLayout()};
Expand Down Expand Up @@ -237,6 +249,14 @@ namespace GfxRenderEngine
gTextureFontAtlas = textureFontAtlas; // copy from VK_Texture to Texture
VkDescriptorImageInfo imageInfo1 = textureFontAtlas->GetDescriptorImageInfo();

{ // a dummy buffer
uint dummy = 0xffffffff;
gDummyBuffer = Buffer::Create(sizeof(uint));
gDummyBuffer->MapBuffer();
gDummyBuffer->WriteToBuffer(&dummy);
gDummyBuffer->Flush();
}

for (uint i = 0; i < VK_SwapChain::MAX_FRAMES_IN_FLIGHT; i++)
{
VkDescriptorBufferInfo shadowUBObufferInfo = m_ShadowUniformBuffers0[i]->DescriptorInfo();
Expand Down Expand Up @@ -267,6 +287,9 @@ namespace GfxRenderEngine
m_RenderSystemPbrSA =
std::make_unique<VK_RenderSystemPbrSA>(m_RenderPass->Get3DRenderPass(), descriptorSetLayoutsPbrSA);

m_RenderSystemGrass =
std::make_unique<VK_RenderSystemGrass>(m_RenderPass->Get3DRenderPass(), descriptorSetLayoutsGrass);

m_RenderSystemShadowInstanced = std::make_unique<VK_RenderSystemShadowInstanced>(
m_ShadowMap[ShadowMaps::HIGH_RES]->GetShadowRenderPass(),
m_ShadowMap[ShadowMaps::LOW_RES]->GetShadowRenderPass(), descriptorSetLayoutsShadowInstanced);
Expand Down Expand Up @@ -800,6 +823,7 @@ namespace GfxRenderEngine
// 3D objects
m_RenderSystemPbr->RenderEntities(m_FrameInfo, registry);
m_RenderSystemPbrSA->RenderEntities(m_FrameInfo, registry);
m_RenderSystemGrass->RenderEntities(m_FrameInfo, registry);
}
}

Expand Down Expand Up @@ -908,6 +932,7 @@ namespace GfxRenderEngine
LOG_CORE_WARN("creating bin directory for spirv files");
EngineCore::CreateDirectory("bin-int");
}
// clang-format off
std::vector<std::string> shaderFilenames = {
// 2D
"spriteRenderer.vert",
Expand All @@ -924,6 +949,7 @@ namespace GfxRenderEngine
"pbr.vert",
"pbr.frag",
"pbrSA.vert",
"grass.vert",
"deferredShading.vert",
"deferredShading.frag",
"skybox.vert",
Expand All @@ -939,9 +965,9 @@ namespace GfxRenderEngine
"bloomUp.vert",
"bloomUp.frag",
"bloomDown.vert",
"bloomDown.frag",
"bloomDown.frag"
};

// clang-format on
for (auto& filename : shaderFilenames)
{
std::string spirvFilename = std::string("bin-int/") + filename + std::string(".spv");
Expand Down
2 changes: 2 additions & 0 deletions engine/platform/Vulkan/VKrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "systems/VKpbrSys.h"
#include "systems/VKpbrSASys.h"
#include "systems/VKgrassSys.h"
#include "systems/bloom/VKbloomRenderSystem.h"
#include "systems/VKpostprocessingSys.h"
#include "systems/VKdeferredShading.h"
Expand Down Expand Up @@ -145,6 +146,7 @@ namespace GfxRenderEngine

std::unique_ptr<VK_RenderSystemPbr> m_RenderSystemPbr;
std::unique_ptr<VK_RenderSystemPbrSA> m_RenderSystemPbrSA;
std::unique_ptr<VK_RenderSystemGrass> m_RenderSystemGrass;
std::unique_ptr<VK_RenderSystemShadowInstanced> m_RenderSystemShadowInstanced;
std::unique_ptr<VK_RenderSystemShadowAnimatedInstanced> m_RenderSystemShadowAnimatedInstanced;
std::unique_ptr<VK_RenderSystemDeferredShading> m_RenderSystemDeferredShading;
Expand Down
69 changes: 38 additions & 31 deletions engine/platform/Vulkan/VKresourceDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,72 +28,79 @@

namespace GfxRenderEngine
{
extern std::shared_ptr<Buffer> gDummyBuffer;
VK_ResourceDescriptor::VK_ResourceDescriptor(Resources::ResourceBuffers& buffers)
{

auto& instBuffer = buffers[Resources::INSTANCE_BUFFER_INDEX];
auto& skelBuffer = buffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX];
auto& hBuffer = buffers[Resources::HEIGHTMAP];

// instance buffer
std::shared_ptr<Buffer>& instanceUbo = buffers[Resources::INSTANCE_BUFFER_INDEX];
VK_Buffer* instanceBuffer = nullptr;
VkDescriptorBufferInfo instanceBufferInfo;
if (instanceUbo)
{
instanceBuffer = static_cast<VK_Buffer*>(instanceUbo.get());
instanceBufferInfo = instanceBuffer->DescriptorInfo();
}
std::shared_ptr<Buffer>& instanceUbo = instBuffer ? instBuffer : gDummyBuffer;
VK_Buffer* instanceBuffer = static_cast<VK_Buffer*>(instanceUbo.get());
VkDescriptorBufferInfo instanceBufferInfo = instanceBuffer->DescriptorInfo();

// joint/bone matrices
std::shared_ptr<Buffer>& skeletalAnimationUbo = buffers[Resources::SKELETAL_ANIMATION_BUFFER_INDEX];
VK_Buffer* skeletalAnimationBuffer = nullptr;
VkDescriptorBufferInfo skeletalAnimationBufferInfo;
if (skeletalAnimationUbo)
{
skeletalAnimationBuffer = static_cast<VK_Buffer*>(skeletalAnimationUbo.get());
skeletalAnimationBufferInfo = skeletalAnimationBuffer->DescriptorInfo();
}
std::shared_ptr<Buffer>& skeletalAnimationUbo = skelBuffer ? skelBuffer : gDummyBuffer;
VK_Buffer* skeletalAnimationBuffer = static_cast<VK_Buffer*>(skeletalAnimationUbo.get());
VkDescriptorBufferInfo skeletalAnimationBufferInfo = skeletalAnimationBuffer->DescriptorInfo();

// height map
std::shared_ptr<Buffer>& heightmapUbo = hBuffer ? hBuffer : gDummyBuffer;
VK_Buffer* heightmapBuffer = static_cast<VK_Buffer*>(heightmapUbo.get());
VkDescriptorBufferInfo heightmapBufferInfo = heightmapBuffer->DescriptorInfo();

{
VK_DescriptorSetLayout::Builder builder{};
if (instanceUbo)
if (instBuffer || skelBuffer || hBuffer)
{
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
}
if (skeletalAnimationUbo)
if (skelBuffer || hBuffer)
{
builder.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
}

if (builder.Size())
if (hBuffer)
{
std::unique_ptr<VK_DescriptorSetLayout> localDescriptorSetLayout = builder.Build();
builder.AddBinding(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
}
std::unique_ptr<VK_DescriptorSetLayout> localDescriptorSetLayout = builder.Build();

VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool);
VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool);
if (instBuffer || skelBuffer || hBuffer)
{
descriptorWriter.WriteBuffer(0, instanceBufferInfo);
if (skeletalAnimationBuffer)
{
descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo);
}
descriptorWriter.Build(m_DescriptorSet);
}
if (skelBuffer || hBuffer)
{
descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo);
}
if (hBuffer)
{
descriptorWriter.WriteBuffer(2, heightmapBufferInfo);
}
descriptorWriter.Build(m_DescriptorSet);
}

// shadows
{
VK_DescriptorSetLayout::Builder builder{};
if (instanceUbo)
if (instBuffer)
{
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
}
if (skeletalAnimationUbo)
if (skelBuffer)
{
builder.AddBinding(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
}
if(builder.Size())
if (builder.Size())
{
std::unique_ptr<VK_DescriptorSetLayout> localDescriptorSetLayout = builder.Build();

VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool);
descriptorWriter.WriteBuffer(0, instanceBufferInfo);
if (skeletalAnimationBuffer)
if (skelBuffer)
{
descriptorWriter.WriteBuffer(1, skeletalAnimationBufferInfo);
}
Expand Down
1 change: 1 addition & 0 deletions engine/platform/Vulkan/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@

#define GLSL_HAS_INSTANCING (0x1 << 0x0)
#define GLSL_HAS_SKELETAL_ANIMATION (0x1 << 0x1)
#define GLSL_HAS_HEIGHTMAP (0x1 << 0x2)

#define MAX_INSTANCE 64
Loading

0 comments on commit db31a19

Please sign in to comment.