Skip to content

Commit d09532f

Browse files
[WIP] Add BRDF reflection
1 parent f6b125c commit d09532f

5 files changed

Lines changed: 250 additions & 103 deletions

File tree

samples/extensions/subgroups_operations/subgroups_operations.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ void SubgroupsOperations::create_descriptor_set_layout()
593593
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
594594
3u),
595595
vkb::initializers::descriptor_set_layout_binding(
596-
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
596+
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
597597
4u),
598598
vkb::initializers::descriptor_set_layout_binding(
599599
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
@@ -627,7 +627,7 @@ void SubgroupsOperations::create_descriptor_set()
627627
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1u, &displacement_descriptor),
628628
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2u, &tessellation_params_descriptor),
629629
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3u, &camera_pos_buffer_descriptor),
630-
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 4u, &normal_map_descriptor),
630+
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4u, &normal_map_descriptor),
631631
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 5u, &ocean_params_buffer_descriptor),
632632
vkb::initializers::write_descriptor_set(ocean.descriptor_set, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6u, &skybox_cubemap_descriptor)};
633633

@@ -1066,6 +1066,21 @@ void SubgroupsOperations::create_image_attachement(VkFormat format, uint32_t wid
10661066
image_view_create_info.image = attachment.image;
10671067
VK_CHECK(vkCreateImageView(get_device().get_handle(), &image_view_create_info, nullptr, &attachment.view));
10681068

1069+
VkSamplerCreateInfo sampler_info = vkb::initializers::sampler_create_info();
1070+
sampler_info.magFilter = VK_FILTER_LINEAR;
1071+
sampler_info.minFilter = VK_FILTER_LINEAR;
1072+
sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1073+
sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1074+
sampler_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1075+
sampler_info.anisotropyEnable = VK_TRUE;
1076+
sampler_info.maxAnisotropy = get_device().get_gpu().get_properties().limits.maxSamplerAnisotropy;
1077+
sampler_info.compareEnable = VK_FALSE;
1078+
sampler_info.compareOp = VK_COMPARE_OP_ALWAYS;
1079+
sampler_info.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
1080+
sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
1081+
1082+
VK_CHECK(vkCreateSampler(get_device().get_handle(), &sampler_info, nullptr, &attachment.sampler));
1083+
10691084
VkImageMemoryBarrier imgMemBarrier = vkb::initializers::image_memory_barrier();
10701085
imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
10711086
imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
@@ -1326,10 +1341,10 @@ void SubgroupsOperations::create_fft_normal_map()
13261341

13271342
VkDescriptorImageInfo SubgroupsOperations::create_ia_descriptor(ImageAttachment &attachment)
13281343
{
1329-
VkDescriptorImageInfo image_descriptor{};
1330-
image_descriptor.imageView = attachment.view;
1331-
image_descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1332-
image_descriptor.sampler = nullptr;
1344+
VkDescriptorImageInfo image_descriptor = {};
1345+
image_descriptor.imageView = attachment.view;
1346+
image_descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1347+
image_descriptor.sampler = attachment.sampler;
13331348
return image_descriptor;
13341349
}
13351350

samples/extensions/subgroups_operations/subgroups_operations.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class SubgroupsOperations : public ApiVulkanSample
147147
float length = {1900.0f};
148148
Wind wind;
149149

150-
glm::vec3 light_pos = {100.0f, 15.0f, -1.0f};
150+
glm::vec3 light_pos = {100.0f, 15.0f, 10.0f};
151151
glm::vec3 light_color = {1.0f, 1.0f, 1.0f};
152152
glm::vec3 ocean_color = {0.0f, 0.2423423f, 0.434335435f};
153153
} ui;
@@ -171,8 +171,10 @@ class SubgroupsOperations : public ApiVulkanSample
171171
VkDeviceMemory memory;
172172
VkImageView view;
173173
VkFormat format;
174+
VkSampler sampler;
174175
void destroy(VkDevice device) const
175176
{
177+
vkDestroySampler(device, sampler, nullptr);
176178
vkDestroyImageView(device, view, nullptr);
177179
vkDestroyImage(device, image, nullptr);
178180
vkFreeMemory(device, memory, nullptr);

shaders/subgroups_operations/fft_normal_map.comp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,23 @@ void main()
3232
{
3333
uint N = fftUbo.grid_size;
3434
ivec2 pixel_pos = ivec2(gl_GlobalInvocationID.xy);
35-
float tex_dim = 1.0f;
35+
const float offset = 1.0f;
3636

37-
vec3 v0 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0.0f, 0.0f))).rgb;
38-
vec3 v1 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, 0.0f))).rgb;
39-
vec3 v2 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, tex_dim))).rgb;
40-
vec3 normal = cross(v1 - v0, v2 - v0);
37+
vec3 v0 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0.0f, -offset))).rgb;
38+
vec3 v1 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0.0f, offset))).rgb;
39+
float v01_length = length(v1 - v0);
4140

42-
// float left_y = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(-tex_dim, 0.0f))).g;
43-
// float right_y = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, 0.0f))).g;
44-
// float bottom_y = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0.0f, tex_dim))).g;
45-
// float top_y = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0.0f, tex_dim))).g;
41+
vec3 v2 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(-offset, 0.0f))).rgb;
42+
vec3 v3 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(offset, 0.0f))).rgb;
43+
float v23_length = length(v3 - v2);
4644

47-
// float z0 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(-tex_dim, -tex_dim))).g;
48-
// float z1 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0, -tex_dim))).g;
49-
// float z2 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, -tex_dim))).g;
50-
// float z3 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(-tex_dim, 0))).g;
51-
// float z4 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, 0))).g;
52-
// float z5 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(-tex_dim, tex_dim))).g;
53-
// float z6 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(0, tex_dim))).g;
54-
// float z7 = imageLoad(fft_displacement_map, ivec2(pixel_pos + vec2(tex_dim, tex_dim))).g;
45+
vec3 c0 = v0 - v1 / 2.0f * v01_length;
46+
vec3 c1 = v2 - v3 / 2.0f * v23_length;
5547

56-
vec3 result = vec3(0.0f);
57-
// result.z = (bottom_y - top_y) / float(N);
58-
// result.x = (left_y - right_y) / float(N);
59-
// result.z = z0 + 2 * z1 + z2 - z5 - 2 * z6 - z7;
60-
// result.x = z0 + 2 * z3 + z5 - z2 - 2 * z4 - z7;
61-
// result.y = 1.0f / float(N);
48+
// c0 = f(n - 1) - f(n + 1) / 2h; h - length(f(n + 1) - f(n - 1)) // left - right
49+
// c1 = f(m - 1) - f(m + 1) / 2h; h - length(f(m + 1) - f(m - 1)) // top - bottom
50+
// n = cross(c0, c1)
6251

63-
imageStore(fft_normal_map, pixel_pos, vec4(normalize(normal), 1.0f));
52+
vec3 result = cross(c0, c1);
53+
imageStore(fft_normal_map, pixel_pos, vec4(normalize(result), 1.0f));
6454
}

0 commit comments

Comments
 (0)