Skip to content

Commit f0a0b0e

Browse files
get compute MVP update working
1 parent e17cb9e commit f0a0b0e

File tree

10 files changed

+427
-147
lines changed

10 files changed

+427
-147
lines changed

examples_tests/22.RaytracedAO/InstanceDataPerCamera.glsl

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples_tests/22.RaytracedAO/common.glsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
#define MAX_ACCUMULATED_SAMPLES (1024*1024)
55

6+
#define WORKGROUP_SIZE 256
67

78
#ifdef __cplusplus
9+
#define uint uint32_t
10+
struct vec3
11+
{
12+
float x,y,z;
13+
};
814
#define mat4 irr::core::matrix4SIMD
915
#define mat4x3 irr::core::matrix3x4SIMD
1016
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#version 430 core
2+
3+
4+
#include "drawCommon.glsl"
5+
layout(local_size_x = WORKGROUP_SIZE) in;
6+
7+
8+
#include <irr/builtin/glsl/utils/indirect_commands.glsl>
9+
layout(set=1, binding=0, std430, row_major) restrict readonly buffer PerInstanceStatic
10+
{
11+
ObjectStaticData_t staticData[];
12+
};
13+
layout(set=1, binding=1, row_major) writeonly restrict buffer PerInstancePerCamera
14+
{
15+
DrawData_t data[];
16+
} instanceDataPerCamera;
17+
layout(set=1, binding=2, std430, row_major) restrict readonly buffer PerInstanceCull
18+
{
19+
CullData_t cullData[];
20+
};
21+
layout(set=1, binding=3, std430) restrict coherent buffer IndirectDraws
22+
{
23+
irr_glsl_DrawElementsIndirectCommand_t draws[];
24+
} commandBuff[2];
25+
26+
27+
28+
layout(push_constant, row_major) uniform PushConstants
29+
{
30+
CullShaderData_t data;
31+
} pc;
32+
33+
34+
35+
#include <irr/builtin/glsl/utils/culling.glsl>
36+
#include <irr/builtin/glsl/utils/transform.glsl>
37+
38+
39+
// base instance remains unchanged
40+
// we just do atomic add on the instance count
41+
void main()
42+
{
43+
uint globalObjectID = gl_GlobalInvocationID.x;
44+
if (globalObjectID>=pc.data.maxObjectCount)
45+
return;
46+
47+
const mat4x3 worldMatrix = cullData[globalObjectID].worldMatrix;
48+
const uint drawID = floatBitsToUint(cullData[globalObjectID].drawID);
49+
50+
// clear drawcount for next buffer
51+
commandBuff[pc.data.currentCommandBufferIx^0x1u].draws[drawID].instanceCount = 0u;
52+
53+
// cull
54+
const mat4 MVP = irr_glsl_pseudoMul4x4with4x3(pc.data.viewProjMatrix,worldMatrix);
55+
bool notCulled = true;
56+
if (false)
57+
{
58+
mat2x3 bbox;
59+
bbox[0] = cullData[globalObjectID].aabbMinEdge;
60+
bbox[1] = cullData[globalObjectID].aabbMaxEdge;
61+
notCulled = irr_glsl_couldBeVisible(MVP,bbox);
62+
}
63+
64+
if (notCulled)
65+
{
66+
const uint instanceID = atomicAdd(commandBuff[pc.data.currentCommandBufferIx].draws[drawID].instanceCount,1u)+cullData[globalObjectID].baseInstance;
67+
68+
instanceDataPerCamera.data[instanceID].MVP = MVP;
69+
instanceDataPerCamera.data[instanceID].detMVP = pc.data.viewProjDeterminant*staticData[globalObjectID].detWorldMatrix;
70+
instanceDataPerCamera.data[instanceID].objectID = globalObjectID;
71+
}
72+
}

examples_tests/22.RaytracedAO/dirty_source/ExtraCrap.cpp

Lines changed: 246 additions & 81 deletions
Large diffs are not rendered by default.

examples_tests/22.RaytracedAO/dirty_source/ExtraCrap.h

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Renderer : public irr::core::IReferenceCounted, public irr::core::InterfaceUnmovable
2020
{
2121
public:
22-
#include "../InstanceDataPerCamera.glsl"
22+
#include "../drawCommon.glsl"
2323
#ifdef __cplusplus
2424
#undef mat4
2525
#undef mat4x3
@@ -70,27 +70,7 @@ class Renderer : public irr::core::IReferenceCounted, public irr::core::Interfac
7070
globalMeta = other.globalMeta;
7171
return *this;
7272
}
73-
7473

75-
struct VisibilityBufferPipelineKey
76-
{
77-
inline bool operator==(const VisibilityBufferPipelineKey& other) const
78-
{
79-
return vertexParams==other.vertexParams&&frontFaceIsCCW==other.frontFaceIsCCW;
80-
}
81-
82-
irr::asset::SVertexInputParams vertexParams;
83-
uint8_t frontFaceIsCCW;
84-
};
85-
struct VisibilityBufferPipelineKeyHash
86-
{
87-
inline std::size_t operator()(const VisibilityBufferPipelineKey& key) const
88-
{
89-
std::basic_string_view view(reinterpret_cast<const char*>(&key),sizeof(key));
90-
return std::hash<decltype(view)>()(view);
91-
}
92-
};
93-
irr::core::unordered_map<VisibilityBufferPipelineKey,irr::core::smart_refctd_ptr<irr::video::IGPURenderpassIndependentPipeline>,VisibilityBufferPipelineKeyHash> m_visibilityBufferFillPipelines;
9474

9575
irr::core::vector<SLight> lights;
9676
irr::core::vector<irr::core::vectorSIMDf> lightRadiances;
@@ -128,9 +108,14 @@ class Renderer : public irr::core::IReferenceCounted, public irr::core::Interfac
128108

129109
irr::core::smart_refctd_ptr<irr::ext::RadeonRays::Manager> m_rrManager;
130110

131-
irr::core::smart_refctd_ptr<irr::video::IGPUSpecializedShader> m_visibilityBufferFillShaders[2];
132-
irr::core::smart_refctd_ptr<irr::video::IGPUDescriptorSetLayout> m_perCameraRasterDSLayout;
133-
irr::core::smart_refctd_ptr<irr::video::IGPUPipelineLayout> m_visibilityBufferFillPipelineLayout;
111+
irr::core::smart_refctd_ptr<irr::asset::ICPUSpecializedShader> m_visibilityBufferFillShaders[2];
112+
irr::core::smart_refctd_ptr<irr::asset::ICPUPipelineLayout> m_visibilityBufferFillPipelineLayoutCPU;
113+
irr::core::smart_refctd_ptr<irr::video::IGPUPipelineLayout> m_visibilityBufferFillPipelineLayoutGPU;
114+
irr::core::smart_refctd_ptr<const irr::video::IGPUDescriptorSetLayout> m_perCameraRasterDSLayout;
115+
116+
irr::core::smart_refctd_ptr<irr::video::IGPUDescriptorSetLayout> m_cullDSLayout;
117+
irr::core::smart_refctd_ptr<irr::video::IGPUPipelineLayout> m_cullPipelineLayout;
118+
irr::core::smart_refctd_ptr<irr::video::IGPUComputePipeline> m_cullPipeline;
134119

135120
irr::core::smart_refctd_ptr<irr::video::IGPUComputePipeline> m_raygenPipeline,m_resolvePipeline;
136121

@@ -140,12 +125,23 @@ class Renderer : public irr::core::IReferenceCounted, public irr::core::Interfac
140125
uint32_t m_renderSize[2u];
141126
bool m_rightHanded;
142127

128+
irr::core::smart_refctd_ptr<irr::video::IGPUBuffer> m_indirectDrawBuffers[2];
129+
struct MDICall
130+
{
131+
irr::asset::SBufferBinding<irr::video::IGPUBuffer> vertexBindings[irr::video::IGPUMeshBuffer::MAX_ATTR_BUF_BINDING_COUNT];
132+
irr::core::smart_refctd_ptr<irr::video::IGPUBuffer> indexBuffer;
133+
irr::core::smart_refctd_ptr<irr::video::IGPURenderpassIndependentPipeline> pipeline;
134+
uint32_t mdiOffset,mdiCount;
135+
};
136+
irr::core::vector<MDICall> m_mdiDrawCalls;
137+
CullShaderData_t m_cullPushConstants;
138+
143139
uint32_t m_lightCount;
144140
irr::core::smart_refctd_ptr<irr::video::IGPUBuffer> m_lightCDFBuffer;
145141
irr::core::smart_refctd_ptr<irr::video::IGPUBuffer> m_lightBuffer;
146142
irr::core::smart_refctd_ptr<irr::video::IGPUBuffer> m_lightRadianceBuffer;
147143

148-
irr::core::smart_refctd_ptr<irr::video::IGPUDescriptorSet> m_globalBackendDataDS,m_perCameraRasterDS; // TODO: do we need to keep track of this?
144+
irr::core::smart_refctd_ptr<irr::video::IGPUDescriptorSet> m_globalBackendDataDS,m_cullDS,m_perCameraRasterDS; // TODO: do we need to keep track of this?
149145

150146

151147
irr::ext::RadeonRays::Manager::MeshBufferRRShapeCache rrShapeCache;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef _INSTANCE_DATA_PER_CAMERA_INCLUDED_
2+
#define _INSTANCE_DATA_PER_CAMERA_INCLUDED_
3+
4+
#include "common.glsl"
5+
6+
struct CullShaderData_t
7+
{
8+
mat4 viewProjMatrix;
9+
uint maxObjectCount;
10+
uint currentCommandBufferIx;
11+
float viewProjDeterminant;
12+
uint padding;
13+
};
14+
15+
struct ObjectStaticData_t
16+
{
17+
vec3 normalMatrixRow0;
18+
float detWorldMatrix;
19+
vec3 normalMatrixRow1;
20+
uint padding0;
21+
vec3 normalMatrixRow2;
22+
uint padding1;
23+
};
24+
25+
struct CullData_t
26+
{
27+
mat4x3 worldMatrix;
28+
vec3 aabbMinEdge;
29+
uint drawID;
30+
vec3 aabbMaxEdge;
31+
uint baseInstance;
32+
};
33+
34+
struct DrawData_t
35+
{
36+
mat4 MVP;
37+
float detMVP;
38+
uint objectID;
39+
uint padding0;
40+
uint padding1;
41+
};
42+
43+
#endif

examples_tests/22.RaytracedAO/mesh.frag renamed to examples_tests/22.RaytracedAO/fillVisBuffer.frag

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#version 430 core
22

3-
#include "irr/builtin/glsl/utils/normal_encode.glsl"
3+
#include <irr/builtin/glsl/utils/normal_encode.glsl>
44

55

66
layout(location = 0) flat in uint ObjectID;
@@ -14,6 +14,7 @@ layout(location = 2) out vec2 uv;
1414
void main()
1515
{
1616
objectTriangleFrontFacing = uvec2(ObjectID^(gl_FrontFacing ? 0x0u:0x80000000u),gl_PrimitiveID);
17+
// these will disappear once we finally have MeshPackerV2 and settle on a way to obtain barycentrics
1718
encodedNormal = irr_glsl_NormalEncode_signedSpherical(normalize(Normal));
1819
uv = UV;
1920
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#version 430 core
2+
3+
#include "drawCommon.glsl"
4+
layout(set=1, binding=0, std430, row_major) restrict readonly buffer PerInstanceStatic
5+
{
6+
ObjectStaticData_t staticData[];
7+
};
8+
layout(set=1, binding=1, row_major) readonly restrict buffer PerInstancePerCamera
9+
{
10+
DrawData_t data[];
11+
} instanceDataPerCamera;
12+
13+
layout(location = 0) in vec3 vPosition;
14+
layout(location = 2) in vec2 vUV;
15+
layout(location = 3) in vec3 vNormal;
16+
17+
layout(location = 0) flat out uint ObjectID;
18+
layout(location = 1) out vec3 Normal;
19+
layout(location = 2) out vec2 UV;
20+
21+
#include <irr/builtin/glsl/utils/transform.glsl>
22+
23+
void main()
24+
{
25+
DrawData_t self = instanceDataPerCamera.data[gl_InstanceIndex];
26+
ObjectID = self.objectID|(floatBitsToUint(self.detMVP)&0x80000000u); // use MSB to denote if face orientation should be flipped
27+
28+
gl_Position = irr_glsl_pseudoMul4x4with3x1(self.MVP,vPosition);
29+
30+
const vec3 localNormal = normalize(vNormal); //have to normalize twice because of normal quantization
31+
Normal[0] = dot(staticData[self.objectID].normalMatrixRow0,localNormal);
32+
Normal[1] = dot(staticData[self.objectID].normalMatrixRow1,localNormal);
33+
Normal[2] = dot(staticData[self.objectID].normalMatrixRow2,localNormal);
34+
35+
UV = vUV;
36+
}

examples_tests/22.RaytracedAO/mesh.vert

Lines changed: 0 additions & 27 deletions
This file was deleted.

include/irr/builtin/glsl/utils/normal_encode.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
vec2 irr_glsl_NormalEncode_signedSpherical(in vec3 n)
77
{
8-
return vec2(atan(n.y,n.x)/kPI, n.z);
8+
return vec2(atan(n.y,n.x)/irr_glsl_PI, n.z);
99
}
1010

1111
#endif

0 commit comments

Comments
 (0)