Skip to content

Commit

Permalink
Terrain shadows are rendered
Browse files Browse the repository at this point in the history
Only terrain tiles that might be visible for fullscreen viewport will cast shadows for now
  • Loading branch information
aleksigron committed Oct 24, 2024
1 parent 44f1bcc commit 845b0f2
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 144 deletions.
4 changes: 1 addition & 3 deletions engine/res/shaders/deferred_geometry/shadow_depth.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
#include "engine/shaders/common/constants.glsl"
#include "engine/shaders/common/transform_block.glsl"

layout(location = 0) in vec3 position;
layout(location = VERTEX_ATTR_INDEX_POS) in vec3 position;

void main()
{
gl_Position = transform.MVP * vec4(position, 1.0);
}

#stage fragment
layout(location = 0) out float depth;

void main()
{
depth = gl_FragCoord.z;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"hash":2341155833246859277,"uid":"e91b1d720b676e23ccf1697af9fa436c"}
{"hash":2415660123109180683,"uid":"e91b1d720b676e23ccf1697af9fa436c"}
32 changes: 32 additions & 0 deletions engine/res/shaders/deferred_geometry/terrain_depth.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 450
#property height_map tex2d

#stage vertex
#include "engine/shaders/common/constants.glsl"
#include "engine/shaders/deferred_geometry/terrain_uniform.glsl"

layout(location = VERTEX_ATTR_INDEX_POS) in vec2 position;

uniform sampler2D height_map;

vec4 calc_position()
{
const float texel_size = 1.0 / (uniforms.terrain_side_verts + 2);
const vec2 origin = vec2(1.5) * texel_size;
const float border_scale_factor = (uniforms.terrain_side_verts - 1) / (uniforms.terrain_side_verts + 2);
vec2 tex_coord = origin + position * border_scale_factor;
float height_sample = texture(height_map, tex_coord).r;
vec2 xy_pos = (position + uniforms.tile_offset) * uniforms.terrain_size * uniforms.tile_scale;
return vec4(xy_pos.x, uniforms.height_origin + height_sample * uniforms.height_range, xy_pos.y, 1.0);
}

void main()
{
gl_Position = uniforms.MVP * calc_position();
}

#stage fragment

void main()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"hash":10448652787744739778,"uid":"0e8c4babf498a0c3ea517de77fb54f72"}
13 changes: 9 additions & 4 deletions engine/src/Graphics/GraphicsFeature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ class GraphicsFeature

const RenderDebugSettings& renderDebug;
const CameraParameters& cameraParameters;
const RenderViewport& fullscreenViewport;
ArrayView<const RenderViewport> shadowViewports;

ArrayView<const RenderViewport> viewports;
uint32_t fullscreenViewportIndex;
uint32_t shadowViewportsBeginIndex;
uint32_t shadowViewportsEndIndex;

const RenderGraphResources* renderGraphResources;
render::FramebufferId finalTargetFramebufferId;
};
Expand All @@ -71,15 +75,16 @@ class GraphicsFeature
render::Device* renderDevice;
};

struct SubmitParameters
struct SubmitParameters : public CommonRenderParameters
{
GraphicsFeatureCommandList& commandList;
GraphicsFeatureCommandList* commandList;
};

struct RenderParameters : public CommonRenderParameters
{
render::CommandEncoder* encoder;

uint32_t renderingViewportIndex;
uint16_t featureObjectId;
};

Expand Down
12 changes: 8 additions & 4 deletions engine/src/Graphics/GraphicsFeatureBloom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void GraphicsFeatureBloom::Upload(const UploadParameters& parameters)

kokko::render::Device* renderDevice = parameters.renderDevice;
PostProcessRenderer* postProcessRenderer = parameters.postProcessRenderer;
Vec2i framebufferSize = parameters.fullscreenViewport.viewportRectangle.size;
Vec2i framebufferSize = parameters.viewports[parameters.fullscreenViewportIndex].viewportRectangle.size;

RenderTarget* currentSource = nullptr;
RenderTarget* currentDestination = nullptr;
Expand Down Expand Up @@ -248,7 +248,9 @@ void GraphicsFeatureBloom::Upload(const UploadParameters& parameters)

// APPLY PASS

ApplyUniforms* applyBlock = reinterpret_cast<ApplyUniforms*>(&uniformStagingBuffer[uniformBlockStride * renderPasses.GetCount()]);
ApplyUniforms* applyBlock =
reinterpret_cast<ApplyUniforms*>(&uniformStagingBuffer[uniformBlockStride * renderPasses.GetCount()]);

for (size_t i = 0; i < MaxKernelSize; ++i)
applyBlock->kernel[i] = blurKernel[i];

Expand All @@ -267,12 +269,14 @@ void GraphicsFeatureBloom::Upload(const UploadParameters& parameters)
renderPasses.PushBack(pass);

// Update uniform buffer
renderDevice->SetBufferSubData(uniformBufferId, 0, uniformBlockStride * renderPasses.GetCount(), uniformStagingBuffer.GetData());
renderDevice->SetBufferSubData(
uniformBufferId, 0, uniformBlockStride * renderPasses.GetCount(), uniformStagingBuffer.GetData());
}

void GraphicsFeatureBloom::Submit(const SubmitParameters& parameters)
{
parameters.commandList.AddToFullscreenViewportWithOrder(RenderPassType::PostProcess, renderOrder, 0);
parameters.commandList->AddToViewportWithOrder(
parameters.fullscreenViewportIndex, RenderPassType::PostProcess, renderOrder, 0);
}

void GraphicsFeatureBloom::Render(const RenderParameters& parameters)
Expand Down
14 changes: 5 additions & 9 deletions engine/src/Graphics/GraphicsFeatureCommandList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace kokko
{

GraphicsFeatureCommandList::GraphicsFeatureCommandList(
RendererCommandList& list,
uint64_t fullscreenViewport,
uint64_t featureIndex) :
GraphicsFeatureCommandList::GraphicsFeatureCommandList(RendererCommandList& list, uint64_t featureIndex) :
list(list),
fullscreenViewportIndex(fullscreenViewport),
featureIndex(featureIndex)
{
}
Expand All @@ -20,14 +16,14 @@ void GraphicsFeatureCommandList::AddToStartOfFrame(uint16_t object)
list.AddDrawWithCallback(0, RenderPassType::Setup, 0.0f, featureIndex, object);
}

void GraphicsFeatureCommandList::AddToFullscreenViewport(RenderPassType pass, float depth, uint16_t object)
void GraphicsFeatureCommandList::AddToViewport(uint32_t viewportIndex, RenderPassType pass, float depth, uint16_t object)
{
list.AddDrawWithCallback(fullscreenViewportIndex, pass, depth, featureIndex, object);
list.AddDrawWithCallback(viewportIndex, pass, depth, featureIndex, object);
}

void GraphicsFeatureCommandList::AddToFullscreenViewportWithOrder(RenderPassType pass, uint64_t order, uint16_t object)
void GraphicsFeatureCommandList::AddToViewportWithOrder(uint32_t viewportIndex, RenderPassType pass, uint64_t order, uint16_t object)
{
list.AddGraphicsFeatureWithOrder(fullscreenViewportIndex, pass, order, featureIndex, object);
list.AddGraphicsFeatureWithOrder(viewportIndex, pass, order, featureIndex, object);
}

}
7 changes: 3 additions & 4 deletions engine/src/Graphics/GraphicsFeatureCommandList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ enum class RenderPassType;
class GraphicsFeatureCommandList
{
public:
GraphicsFeatureCommandList(RendererCommandList& list, uint64_t fullscreenViewport, uint64_t featureIndex);
GraphicsFeatureCommandList(RendererCommandList& list, uint64_t featureIndex);

void AddToStartOfFrame(uint16_t object);
void AddToFullscreenViewport(RenderPassType pass, float depth, uint16_t object);
void AddToFullscreenViewportWithOrder(RenderPassType pass, uint64_t order, uint16_t object);
void AddToViewport(uint32_t viewportIndex, RenderPassType pass, float depth, uint16_t object);
void AddToViewportWithOrder(uint32_t viewportIndex, RenderPassType pass, uint64_t order, uint16_t object);

private:
RendererCommandList& list;
uint64_t fullscreenViewportIndex;
uint64_t featureIndex;

friend class Renderer;
Expand Down
12 changes: 7 additions & 5 deletions engine/src/Graphics/GraphicsFeatureDeferredLighting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void GraphicsFeatureDeferredLighting::Upload(const UploadParameters& parameters)
{
LightingUniformBlock lightingUniforms;

const RenderViewport& fsvp = parameters.fullscreenViewport;
const RenderViewport& fsvp = parameters.viewports[parameters.fullscreenViewportIndex];

// Update directional light viewports
Array<LightId>& directionalLights = lightResultArray;
Expand Down Expand Up @@ -317,9 +317,10 @@ void GraphicsFeatureDeferredLighting::Upload(const UploadParameters& parameters)
bias[13] = 0.5f;

// Update transforms and split depths for each shadow cascade
for (size_t vpIdx = 0, vpCount = parameters.shadowViewports.GetCount(); vpIdx != vpCount; ++vpIdx)
const size_t vpEnd = parameters.shadowViewportsEndIndex;
for (size_t vpIdx = parameters.shadowViewportsBeginIndex; vpIdx != vpEnd; ++vpIdx)
{
Mat4x4f viewToLight = parameters.shadowViewports[vpIdx].viewProjection * fsvp.view.forward;
Mat4x4f viewToLight = parameters.viewports[vpIdx].viewProjection * fsvp.view.forward;
Mat4x4f shadowMat = bias * viewToLight;

lightingUniforms.shadowMatrices[vpIdx] = shadowMat;
Expand All @@ -343,10 +344,11 @@ void GraphicsFeatureDeferredLighting::Submit(const SubmitParameters& parameters)
{
if (brdfLutFramebufferId != 0)
{
parameters.commandList.AddToStartOfFrame(RenderOrderConfiguration::MaxFeatureObjectId);
parameters.commandList->AddToStartOfFrame(RenderOrderConfiguration::MaxFeatureObjectId);
}

parameters.commandList.AddToFullscreenViewportWithOrder(RenderPassType::OpaqueLighting, renderOrder, 0);
parameters.commandList->AddToViewportWithOrder(
parameters.fullscreenViewportIndex, RenderPassType::OpaqueLighting, renderOrder, 0);
}

void GraphicsFeatureDeferredLighting::Render(const RenderParameters& parameters)
Expand Down
2 changes: 1 addition & 1 deletion engine/src/Graphics/GraphicsFeatureSkybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void GraphicsFeatureSkybox::Upload(const UploadParameters& parameters)

void GraphicsFeatureSkybox::Submit(const SubmitParameters& parameters)
{
parameters.commandList.AddToFullscreenViewport(RenderPassType::Skybox, 0.0f, 0);
parameters.commandList->AddToViewport(parameters.fullscreenViewportIndex, RenderPassType::Skybox, 0.0f, 0);
}

void GraphicsFeatureSkybox::Render(const RenderParameters& parameters)
Expand Down
7 changes: 4 additions & 3 deletions engine/src/Graphics/GraphicsFeatureSsao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void GraphicsFeatureSsao::Upload(const UploadParameters& parameters)
KOKKO_PROFILE_FUNCTION();

kokko::render::Device* device = parameters.renderDevice;
Vec2i framebufferSize = parameters.fullscreenViewport.viewportRectangle.size;
Vec2i framebufferSize = parameters.viewports[parameters.fullscreenViewportIndex].viewportRectangle.size;

float noiseSizef = static_cast<float>(NoiseTextureSize);

Expand Down Expand Up @@ -169,12 +169,13 @@ void GraphicsFeatureSsao::Upload(const UploadParameters& parameters)

void GraphicsFeatureSsao::Submit(const SubmitParameters& parameters)
{
parameters.commandList.AddToFullscreenViewportWithOrder(RenderPassType::OpaqueLighting, renderOrder, 0);
parameters.commandList->AddToViewportWithOrder(
parameters.fullscreenViewportIndex, RenderPassType::OpaqueLighting, renderOrder, 0);
}

void GraphicsFeatureSsao::Render(const RenderParameters& parameters)
{
Vec2i framebufferSize = parameters.fullscreenViewport.viewportRectangle.size;
Vec2i framebufferSize = parameters.viewports[parameters.fullscreenViewportIndex].viewportRectangle.size;

// Get render targets

Expand Down
5 changes: 3 additions & 2 deletions engine/src/Graphics/GraphicsFeatureTonemapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void GraphicsFeatureTonemapping::Upload(const UploadParameters& parameters)

void GraphicsFeatureTonemapping::Submit(const SubmitParameters& parameters)
{
parameters.commandList.AddToFullscreenViewportWithOrder(RenderPassType::PostProcess, renderOrder, 0);
parameters.commandList->AddToViewportWithOrder(
parameters.fullscreenViewportIndex, RenderPassType::PostProcess, renderOrder, 0);
}

void GraphicsFeatureTonemapping::Render(const RenderParameters& parameters)
Expand All @@ -108,7 +109,7 @@ void GraphicsFeatureTonemapping::Render(const RenderParameters& parameters)
pass.uniformBufferRangeSize = sizeof(TonemapUniformBlock);

pass.framebufferId = parameters.finalTargetFramebufferId;
pass.viewportSize = parameters.fullscreenViewport.viewportRectangle.size;
pass.viewportSize = parameters.viewports[parameters.fullscreenViewportIndex].viewportRectangle.size;
pass.shaderId = shaderId;
pass.enableBlending = false;

Expand Down
11 changes: 7 additions & 4 deletions engine/src/Graphics/ParticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ void ParticleSystem::Upload(const UploadParameters& parameters)

renderDevice->SetBufferSubData(emitter.bufferIds[Buffer_UpdateUniforms], 0, sizeof(UpdateParticleBlock), &updateUniforms);

const RenderViewport& fullscreenViewport = parameters.viewports[parameters.fullscreenViewportIndex];
TransformUniformBlock transformUniforms;
transformUniforms.M = transform;
transformUniforms.MV = parameters.fullscreenViewport.view.inverse * transform;
transformUniforms.MVP = parameters.fullscreenViewport.viewProjection * transform;
transformUniforms.MV = fullscreenViewport.view.inverse * transform;
transformUniforms.MVP = fullscreenViewport.viewProjection * transform;

renderDevice->SetBufferSubData(emitter.bufferIds[Buffer_RenderTransform], 0, sizeof(TransformUniformBlock), &transformUniforms);
}
Expand All @@ -185,7 +186,8 @@ void ParticleSystem::Submit(const SubmitParameters& parameters)
{
EmitterData& emitter = data.emitter[i];
float depth = 0.0f; // TODO: Calculate
parameters.commandList.AddToFullscreenViewport(RenderPassType::Transparent, depth, static_cast<uint16_t>(i));
parameters.commandList->AddToViewport(
parameters.fullscreenViewportIndex, RenderPassType::Transparent, depth, static_cast<uint16_t>(i));
}
}

Expand Down Expand Up @@ -261,7 +263,8 @@ void ParticleSystem::Render(const RenderParameters& parameters)
encoder->MemoryBarrier(shaderStorageAndDrawIndirectBarrier);

encoder->BindBuffer(RenderBufferTarget::DrawIndirectBuffer, emitter.bufferIds[Buffer_Indirect]);
encoder->BindBufferBase(RenderBufferTarget::UniformBuffer, UniformBlockBinding::Viewport, parameters.fullscreenViewport.uniformBlockObject);
encoder->BindBufferBase(RenderBufferTarget::UniformBuffer, UniformBlockBinding::Viewport,
parameters.viewports[parameters.fullscreenViewportIndex].uniformBlockObject);
encoder->BindBufferBase(RenderBufferTarget::UniformBuffer, UniformBlockBinding::Object, emitter.bufferIds[Buffer_RenderTransform]);

const ShaderData& renderShader = shaderManager->GetShaderData(renderShaderId);
Expand Down
Loading

0 comments on commit 845b0f2

Please sign in to comment.