Skip to content

Commit fa3e968

Browse files
committed
Still working on atomics
1 parent 7159a3e commit fa3e968

File tree

14 files changed

+208
-45
lines changed

14 files changed

+208
-45
lines changed

examples/resources/scenes/sponza.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<albedo r="0.5" g="0.5" b="0.5" />
5959
<Textures>
6060
<albedo path="textures/Columns/Column_C_Diffuse.png" />
61-
<normals path="textures/Columns/Column_C_Normal.png" />
61+
<!-- <normals path="textures/Columns/Column_C_Normal.png" /> -->
6262
<!-- <mask path="textures/Columns/Column_C_MaskMap.png" type="UnityHDRP" /> -->
6363
</Textures>
6464
</Material>
@@ -281,7 +281,7 @@
281281

282282
<Enviroment type="skybox">
283283
<Filename value="textures/cloudy.hdr" />
284-
<intensity value="0.01" />
284+
<intensity value="0.4" />
285285
</Enviroment>
286286

287287

include/engine/common.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ typedef enum ColorFormatTypeFlagBits
286286
SRGB_8 = VK_FORMAT_R8G8B8_SRGB, // RGB
287287
SRGBA_8 = VK_FORMAT_R8G8B8A8_SRGB, // RGB with Alpha
288288
SBGRA_8 = VK_FORMAT_B8G8R8A8_SRGB, // Other order
289+
SR_16F = VK_FORMAT_R16_SFLOAT,
290+
SR_32F = VK_FORMAT_R32_SFLOAT,
289291
SRG_16F = VK_FORMAT_R16G16_SFLOAT,
290292
SRG_32F = VK_FORMAT_R32G32_SFLOAT,
291293
SRGB_16F = VK_FORMAT_R16G16B16_SFLOAT,
@@ -296,6 +298,7 @@ typedef enum ColorFormatTypeFlagBits
296298
RG_8U = VK_FORMAT_R8G8_UNORM,
297299
RGB_8U = VK_FORMAT_R8G8B8_UNORM,
298300
RGBA_8U = VK_FORMAT_R8G8B8A8_UNORM,
301+
R_32_UINT = VK_FORMAT_R32_UINT,
299302
DEPTH_16F = VK_FORMAT_D16_UNORM,
300303
DEPTH_32F = VK_FORMAT_D32_SFLOAT
301304
} ColorFormatType;
@@ -403,8 +406,8 @@ typedef enum TextureFormatTypeFlagBits
403406
{
404407
TEXTURE_FORMAT_SRGB = 0x00000000,
405408
TEXTURE_FORMAT_UNORM = 0x00000001,
406-
TEXTURE_FORMAT_DEPTH = 0x00000002,
407-
TEXTURE_FORMAT_HDR = 0x00000003
409+
TEXTURE_FORMAT_DEPTH = 0x00000002,
410+
TEXTURE_FORMAT_HDR = 0x00000003
408411
} TextureFormatType;
409412
typedef enum BindingTypeFlagBits
410413
{

include/engine/core/passes/composition_pass.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct VXGI { // Settings for Voxel Based GI
4444
uint32_t resolution = 256;
4545
uint32_t samples = 8;
4646
uint32_t enabled = 1;
47+
uint32_t updateMode = 0;
4748
};
4849

4950
class CompositionPass : public GraphicPass

include/engine/core/passes/voxelization_pass.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
#include <engine/core/passes/pass.h>
1212
#include <engine/core/resource_manager.h>
1313

14+
#define USE_IMG_ATOMIC_OPERATION
15+
1416
VULKAN_ENGINE_NAMESPACE_BEGIN
1517

1618
namespace Core {
1719

1820
class VoxelizationPass : public GraphicPass
1921
{
22+
#ifdef USE_IMG_ATOMIC_OPERATION
2023
const uint16_t RESOURCE_IMAGES = 4;
24+
#else
25+
const uint16_t RESOURCE_IMAGES = 1;
26+
#endif
2127

2228
/*Descriptors*/
2329
struct FrameDescriptors {
@@ -32,7 +38,7 @@ class VoxelizationPass : public GraphicPass
3238
public:
3339
VoxelizationPass(Graphics::Device* ctx, uint32_t resolution)
3440
: BasePass(ctx, {resolution, resolution}, 1, 1, false, "VOXELIZATION") {
35-
m_resourceImages.resize(RESOURCE_IMAGES);
41+
m_resourceImages.resize(RESOURCE_IMAGES);
3642
}
3743

3844
void setup_attachments(std::vector<Graphics::AttachmentInfo>& attachments,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#shader compute
2+
#version 460 core
3+
4+
// In this case the voxelizer atomically writes to 3 seperate r32ui textures
5+
// instead of only one rgba16f texture. Here we merge them together into one.
6+
7+
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
8+
9+
layout(set = 0, binding = 6, r32f) uniform image3D finalVoxelImage;
10+
layout(set = 0, binding = 7, r32ui) uniform uimage3D interVoxelImages[3];
11+
12+
void main()
13+
{
14+
ivec3 imgCoord = ivec3(gl_GlobalInvocationID);
15+
16+
float a = imageLoad(finalVoxelImage, imgCoord).a;
17+
if (a > 0.0)
18+
{
19+
float r = imageLoad(interVoxelImages[0], imgCoord).r;
20+
float g = imageLoad(interVoxelImages[1], imgCoord).r;
21+
float b = imageLoad(interVoxelImages[2], imgCoord).r;
22+
imageStore(finalVoxelImage, imgCoord, vec4(r, g, b, a));
23+
}
24+
}

resources/shaders/VXGI/voxelization.glsl

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void main() {
4343
}
4444
#shader geometry
4545
#version 460
46+
#extension GL_NV_geometry_shader_passthrough : enable
47+
4648

4749
layout(triangles) in;
4850
layout(triangle_strip, max_vertices = 3) out;
@@ -80,12 +82,15 @@ void main(){
8082

8183
#shader fragment
8284
#version 460
85+
86+
#define USE_IMG_ATOMIC_OPERATION
87+
8388
#extension GL_EXT_ray_tracing : enable
8489
#extension GL_EXT_ray_query : enable
85-
#extension GL_EXT_shader_atomic_float2 : enable
86-
// #extension GL_EXT_shader_16bit_storage : require
90+
91+
92+
#extension GL_EXT_shader_atomic_float2 : require
8793
// #extension GL_EXT_gpu_shader5 : require
88-
// #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
8994
// #extension GL_EXT_shader_atomic_fp16_vector : require
9095
#include light.glsl
9196
#include scene.glsl
@@ -95,6 +100,9 @@ void main(){
95100
#include raytracing.glsl
96101
#include material_defines.glsl
97102

103+
104+
105+
98106
layout(location = 0) in vec3 _pos;
99107
layout(location = 1) in vec3 _normal;
100108
layout(location = 2) in vec2 _uv;
@@ -103,7 +111,11 @@ layout(set = 0, binding = 2) uniform sampler2DArray shadowMap;
103111
layout(set = 0, binding = 3) uniform samplerCube irradianceMap;
104112
layout(set = 0, binding = 4) uniform accelerationStructureEXT TLAS;
105113
layout(set = 0, binding = 5) uniform sampler2D samplerMap;
114+
106115
layout(set = 0, binding = 6, r32f) uniform image3D voxelImage;
116+
#ifdef USE_IMG_ATOMIC_OPERATION
117+
layout(set = 0, binding = 7, r32ui) uniform uimage3D auxVoxelImages[3];
118+
#endif
107119

108120
layout(set = 1, binding = 1) uniform MaterialUniforms {
109121
vec4 slot1;
@@ -217,9 +229,16 @@ void main() {
217229

218230
vec4 result = g_opacity * vec4(vec3(color), 1);
219231
ivec3 voxelPos = worldSpaceToVoxelSpace(_pos);
220-
221-
// imageAtomicMax(voxelImage, voxelPos, 1);
222-
// imageAtomicMax(voxelImage, voxelPos, f16vec4(color, 1.0));
232+
233+
#ifdef USE_IMG_ATOMIC_OPERATION
234+
imageAtomicMax(auxVoxelImages[0], voxelPos, floatBitsToUint(result.r));
235+
imageAtomicMax(auxVoxelImages[1], voxelPos, floatBitsToUint(result.g));
236+
imageAtomicMax(auxVoxelImages[2], voxelPos, floatBitsToUint(result.b));
237+
// imageAtomicMax(voxelImage, voxelPos, f16vec4(vec4(result, 1.0)));
238+
// imageStore(voxelImage, voxelPos, vec4(0.0, 0.0, 0.0, 1.0));
239+
imageStore(voxelImage, voxelPos, result);
240+
#else
223241
imageStore(voxelImage, voxelPos, result);
242+
#endif
224243

225244
}

resources/shaders/deferred/composition.glsl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void main()
124124

125125
vec3 direct = vec3(0.0);
126126
vec3 ambient = vec3(0.0);
127-
vec3 indirect = vec3(0.0);
128-
vec3 indirect2 = vec3(0.0);
127+
vec4 indirect = vec4(0.0);
128+
vec3 indirectSpecular = vec3(0.0);
129129

130130
vec3 modelPos = (camera.invView * vec4(g_pos.xyz, 1.0)).xyz;
131131
vec3 modelNormal = (camera.invView * vec4(g_normal.xyz, 0.0)).xyz;
@@ -150,16 +150,20 @@ void main()
150150
// Indirect Component ____________________________
151151
if(settings.vxgi.enabled == 1){
152152

153-
vec4 diffuseIndirect = diffuseVoxelGI(voxelMap, modelPos, modelNormal, settings.vxgi, scene.maxCoord.x-scene.minCoord.x);
153+
// Diffuse
154+
indirect = diffuseVoxelGI2(voxelMap, modelPos, modelNormal, settings.vxgi, scene.maxCoord.x-scene.minCoord.x);
155+
indirect.rgb *= g_albedo;
156+
indirect.rgb *= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao; //Add ambient occlusion
154157

155-
indirect = diffuseIndirect.rgb * g_albedo;
156-
indirect*= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao; //Add ambient occlusion
158+
// Specular
159+
vec3 specularConeDirection = reflect(-normalize(camera.position.xyz-modelPos), modelNormal);
160+
float voxelWorldSize = (scene.maxCoord.x-scene.minCoord.x) / float(settings.vxgi.resolution);
161+
vec3 startPos = modelPos + modelNormal * voxelWorldSize * settings.vxgi.offset;
157162

158-
// vec3 specularConeDirection = reflect(normalize(camera.position.xyz-modelPos), modelNormal);
159-
// vec3 specularIndirect = vec3(0.0);
160-
// specularIndirect = traceVoxelCone(voxelMap, startPos, specularConeDirection, max(brdf.roughness, 0.05) , VOXEL_SIZE );
161-
// // specularIndirect = castCone(startPos, specularConeDirection, max(roughness, MIN_SPECULAR_APERTURE), MAX_TRACE_DISTANCE, minLevel).rgb * specColor.rgb * u_indirectSpecularIntensity;
162-
// indirect2+=specularIndirect;
163+
const float CONE_SPREAD = mix(0.005, settings.vxgi.diffuseConeSpread, brdf.roughness);
164+
indirectSpecular = traceCone(voxelMap, startPos, specularConeDirection, settings.vxgi.maxDistance, CONE_SPREAD, voxelWorldSize ).rgb;
165+
166+
// indirect+=specularIndirect.rgb;
163167
}
164168
//Direct Component ________________________
165169
for(int i = 0; i < scene.numLights; i++) {
@@ -262,7 +266,8 @@ void main()
262266

263267

264268

265-
color = direct + indirect + ambient + reflectedColor;
269+
color = direct + indirect.rgb + ambient + reflectedColor;
270+
// color = direct + indirect.rgb + ambient + reflectedColor;
266271

267272
//////////////////////////////////////
268273
// IF UNLIT MATERIAL

resources/shaders/include/VXGI.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct VXGI {
1818
uint resolution;
1919
uint samples;
2020
uint enabled;
21+
uint updateMode;
2122
};
2223

2324
vec4 traceCone(sampler3D voxelization, vec3 origin, vec3 direction, const float MAX_DISTANCE ,const float CONE_SPREAD, const float VOXEL_WORLD_SIZE)

0 commit comments

Comments
 (0)