Skip to content

Commit 6daafe0

Browse files
committed
Work on shadow rendering, copy testing folder
1 parent cf55f63 commit 6daafe0

11 files changed

Lines changed: 371 additions & 7 deletions

File tree

.github/workflows/cpp-test-windows-x64.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
shell: cmd
2020
working-directory: ${{ github.workspace }}
2121
run: xcopy 3DRadSpace\Data\ build\Release\Data\ /s /e /h /y
22+
- name: Copy Test folders
23+
shell: cmd
24+
working-directory: ${{ github.workspace }}
25+
run: xcopy 3DRadSpace\Testing\ build\Release\Testing\ /s /e /h /y
2226
- name: Run tests
2327
shell: cmd
2428
working-directory: ${{ github.workspace }}\build\Release\
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Screen-Space Shadow Application Shader
2+
// Applied as a fullscreen pass to composite shadows onto the rendered scene
3+
4+
#include "PostProcessBase.hlsl"
5+
6+
cbuffer ShadowData : register(b0)
7+
{
8+
row_major matrix matLightViewProj; // Light space transformation
9+
row_major matrix matInvViewProj; // Inverse of camera view-projection (for position reconstruction)
10+
float3 LightDirection;
11+
float ShadowBias;
12+
float ShadowIntensity; // How dark shadows are (0 = black, 1 = no shadow)
13+
float2 padding;
14+
}
15+
16+
Texture2D colorTexture : register(t0); // Scene color (backbuffer)
17+
Texture2D depthTexture : register(t1); // Scene depth
18+
Texture2D shadowMap : register(t2); // Shadow map from light
19+
20+
SamplerState colorSampler : register(s0);
21+
SamplerState depthSampler : register(s1);
22+
SamplerState shadowSampler : register(s2);
23+
24+
// Reconstruct world position from depth buffer
25+
float3 ReconstructWorldPosition(float2 uv, float depth)
26+
{
27+
// Convert UV and depth to NDC coordinates
28+
float4 ndc;
29+
ndc.x = uv.x * 2.0 - 1.0;
30+
ndc.y = (1.0 - uv.y) * 2.0 - 1.0; // Flip Y
31+
ndc.z = depth;
32+
ndc.w = 1.0;
33+
34+
// Transform to world space
35+
float4 worldPos = mul(ndc, matInvViewProj);
36+
worldPos /= worldPos.w; // Perspective divide
37+
38+
return worldPos.xyz;
39+
}
40+
41+
float CalculateShadow(float3 worldPos)
42+
{
43+
// Transform world position to light space
44+
float4 lightSpacePos = mul(float4(worldPos, 1.0), matLightViewProj);
45+
46+
// Perform perspective divide
47+
float3 projCoords = lightSpacePos.xyz / lightSpacePos.w;
48+
49+
// Transform to [0,1] range for texture sampling
50+
projCoords.x = projCoords.x * 0.5 + 0.5;
51+
projCoords.y = projCoords.y * -0.5 + 0.5; // Flip Y for D3D
52+
53+
// Outside shadow map bounds = not in shadow
54+
if (projCoords.x < 0.0 || projCoords.x > 1.0 ||
55+
projCoords.y < 0.0 || projCoords.y > 1.0 ||
56+
projCoords.z > 1.0)
57+
return 1.0;
58+
59+
// Get closest depth from shadow map
60+
float closestDepth = shadowMap.Sample(shadowSampler, projCoords.xy).r;
61+
62+
// Get current fragment depth
63+
float currentDepth = projCoords.z;
64+
65+
// Compare depths with bias
66+
float shadow = (currentDepth - ShadowBias) > closestDepth ? ShadowIntensity : 1.0;
67+
68+
return shadow;
69+
}
70+
71+
float4 PS_Main(VertexOut v) : SV_TARGET
72+
{
73+
// Sample scene color
74+
float4 color = colorTexture.Sample(colorSampler, v.UV);
75+
76+
// Sample depth
77+
float depth = depthTexture.Sample(depthSampler, v.UV).r;
78+
79+
// Skip skybox (depth = 1.0 or 0.0 depending on depth configuration)
80+
if (depth >= 0.9999)
81+
return color;
82+
83+
// Reconstruct world position from depth
84+
float3 worldPos = ReconstructWorldPosition(v.UV, depth);
85+
86+
// Calculate shadow factor
87+
float shadowFactor = CalculateShadow(worldPos);
88+
89+
// Apply shadow to color
90+
return color * shadowFactor;
91+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Shadow Map Depth Shader
2+
// Renders depth from light's perspective for shadow mapping
3+
4+
cbuffer ShadowData : register(b0)
5+
{
6+
row_major matrix matLightViewProj; // Light space transformation matrix
7+
}
8+
9+
struct VertexIn
10+
{
11+
float3 Position : POSITION;
12+
};
13+
14+
struct VertexOut
15+
{
16+
float4 Position : SV_POSITION;
17+
float Depth : TEXCOORD0;
18+
};
19+
20+
VertexOut VS_Main(VertexIn v)
21+
{
22+
VertexOut r;
23+
r.Position = mul(float4(v.Position.xyz, 1), matLightViewProj);
24+
r.Depth = r.Position.z / r.Position.w;
25+
return r;
26+
}
27+
28+
// Pixel shader can be empty for depth-only rendering
29+
// The depth buffer is automatically written by the hardware
30+
void PS_Main(VertexOut v)
31+
{
32+
// No output needed - depth is written automatically
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "IService.hpp"
2+
#include "IGame.hpp"
3+
4+
using namespace Engine3DRadSpace;
5+
6+
void IService::SetOwner(IGame* owner) noexcept
7+
{
8+
_owner = owner;
9+
}
10+
11+
IGame* IService::GetOwner() const noexcept
12+
{
13+
return _owner;
14+
}

3DRadSpace/Engine3DRadSpace/Core/IService.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33

44
namespace Engine3DRadSpace
55
{
6+
class IGame;
7+
68
/// <summary>
79
/// Polymorphic base for all services used by the engine.
810
/// </summary>
911
class E3DRSP_CORE_EXPORT IService
1012
{
13+
protected:
14+
IGame* _owner = nullptr;
1115
public:
16+
void SetOwner(IGame* owner) noexcept;
17+
IGame* GetOwner() const noexcept;
18+
1219
virtual ~IService() = default;
1320
};
1421
}

3DRadSpace/Engine3DRadSpace/Graphics/Rendering/IRenderer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "IRenderer.hpp"
22
#include "../Effect.hpp"
3+
#include "../../Core/IGame.hpp"
34

5+
using namespace Engine3DRadSpace;
46
using namespace Engine3DRadSpace::Graphics;
57
using namespace Engine3DRadSpace::Graphics::Rendering;
68

@@ -23,4 +25,14 @@ void IRenderer::Draw(IVertexBuffer* vertices, IIndexBuffer* indices, Effect* eff
2325
IGraphicsDevice* IRenderer::GetDevice() const noexcept
2426
{
2527
return _device;
28+
}
29+
30+
void IRenderer::SetOwner(IGame* owner) noexcept
31+
{
32+
_owner = owner;
33+
}
34+
35+
IGame* IRenderer::GetOwner() const noexcept
36+
{
37+
return _owner;
2638
}

3DRadSpace/Engine3DRadSpace/Graphics/Rendering/IRenderer.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#pragma once
22
#include "../ModelMeshPart.hpp"
33

4+
namespace Engine3DRadSpace
5+
{
6+
class IGame;
7+
}
8+
49
namespace Engine3DRadSpace::Graphics::Rendering
510
{
611
/// <summary>
@@ -10,6 +15,7 @@ namespace Engine3DRadSpace::Graphics::Rendering
1015
{
1116
protected:
1217
IGraphicsDevice* _device;
18+
IGame* _owner = nullptr;
1319

1420
IRenderer(IGraphicsDevice* device);
1521
public:
@@ -19,6 +25,9 @@ namespace Engine3DRadSpace::Graphics::Rendering
1925
IRenderer(IRenderer&&) noexcept = default;
2026
IRenderer& operator=(IRenderer&&) noexcept = default;
2127

28+
void SetOwner(IGame* owner) noexcept;
29+
IGame* GetOwner() const noexcept;
30+
2231
/// <summary>
2332
/// Prepares the graphics pipeline for this effect.
2433
/// </summary>

3DRadSpace/Engine3DRadSpace/Graphics/Rendering/RenderingManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ using namespace Engine3DRadSpace;
55
using namespace Engine3DRadSpace::Graphics;
66
using namespace Engine3DRadSpace::Graphics::Rendering;
77

8-
RenderingManager::RenderingManager(IGraphicsDevice* device)
8+
RenderingManager::RenderingManager(IGraphicsDevice* device) :
9+
_device(device)
910
{
1011
_renderers.push_back(std::make_unique<ForwardRenderer>(device));
1112
}
1213

1314
void RenderingManager::Add(std::unique_ptr<IRenderer>&& renderPass)
1415
{
16+
renderPass->SetOwner(_owner);
1517
_renderers.push_back(std::move(renderPass));
1618
}
1719

3DRadSpace/Engine3DRadSpace/Graphics/Rendering/RenderingManager.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ namespace Engine3DRadSpace::Graphics::Rendering
1515
std::vector<std::unique_ptr<IRenderer>> _renderers;
1616
public:
1717
RenderingManager(IGraphicsDevice* device);
18-
18+
1919
template<typename R, typename... Args>
2020
void Add(Args&&... args)
2121
{
22-
_renderers.push_back(std::make_unique<R>(_device, std::forward<Args>(args)...));
22+
auto renderer = std::make_unique<R>(_device, std::forward<Args>(args)...);
23+
renderer->SetOwner(_owner);
24+
_renderers.push_back(std::move(renderer));
2325
}
2426

2527
void Add(std::unique_ptr<IRenderer>&& renderPass);

0 commit comments

Comments
 (0)