Skip to content

Commit 371c382

Browse files
committed
Smooth shading with vertex normals from compute
Pretty... :)
1 parent ab9e6e3 commit 371c382

8 files changed

+28
-15
lines changed

comp.spv

72 Bytes
Binary file not shown.

compositor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ bool Compositor::Destroy(VkDevice& device)
182182
return true;
183183
}
184184

185-
//bool once = true;
185+
bool once = true;
186186

187187
bool Compositor::Draw(VkDevice& device)
188188
{
@@ -196,11 +196,11 @@ bool Compositor::Draw(VkDevice& device)
196196
vkQueueSubmit(computeQueue, 1, &submitInfo, VK_NULL_HANDLE);
197197
vkQueueWaitIdle(computeQueue);
198198

199-
/*if(once)
199+
if(once)
200200
{
201201
computer->PrintResults(device);
202202
once = false;
203-
}*/
203+
}
204204

205205
transferCommandBuffer = &graphicsEngine->TransferDynamicBuffers(device);
206206

compute.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Compute::Compute(VkExtent3D inputExtent, VkPhysicalDeviceMemoryProperties& props
2727
extent(inputExtent),
2828
uniformBufferSize(sizeof(float) * numWaveComponents),
2929
storageBufferSize(sizeof(float) * inputExtent.width * inputExtent.height),
30-
normalBufferSize(sizeof(float[3]) * inputExtent.width * inputExtent.height),
30+
normalBufferSize(sizeof(float[4]) * inputExtent.width * inputExtent.height),
3131
startTime(std::chrono::high_resolution_clock::now())
3232
{
3333
//GetMemoryTypeIndexCallback = memTypeIndexCallback;
@@ -61,7 +61,7 @@ void Compute::Init(VkDevice& device)
6161

6262
SetupBuffer(device, storageBuffer, storageBufferMemory, storageBufferSize, properties, usage);
6363

64-
properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
64+
properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
6565
usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
6666

6767
SetupBuffer(device, normalBuffer, normalBufferMemory, normalBufferSize, properties, usage);
@@ -385,17 +385,25 @@ void Compute::UpdateWave(VkDevice& device)
385385
void Compute::PrintResults(VkDevice& device)
386386
{
387387
void* data;
388-
vkMapMemory(device, storageBufferMemory, 0, storageBufferSize, 0, &data);
388+
//vkMapMemory(device, storageBufferMemory, 0, storageBufferSize, 0, &data);
389+
390+
vkMapMemory(device, normalBufferMemory, 0, normalBufferSize, 0, &data);
391+
392+
float* mem = static_cast<float*>(data);
389393

390394
for(uint32_t i = 0; i < extent.width; ++i)
391395
{
392396
for(uint32_t j = 0; j < extent.height; ++j)
393397
{
394-
std::cout << static_cast<float*>(data)[i*extent.width + j] << ", ";
398+
//std::cout << static_cast<float*>(data)[i*extent.width + j] << ", ";
399+
std::cout << "{ " << mem[i*extent.width + j*4] << ", "
400+
<< mem[i*extent.width + j*4 + 1] << ", "
401+
<< mem[i*extent.width + j*4 + 2] << ", "
402+
<< mem[i*extent.width + j*4 + 3] << " }";
395403
}
396404

397405
std::cout << std::endl;
398406
}
399407

400-
vkUnmapMemory(device, storageBufferMemory);
408+
vkUnmapMemory(device, normalBufferMemory);
401409
}

controller.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,15 @@ bool Controller::Init()
180180

181181
bool foundDiscreteGPU = false;
182182

183+
std::cout << "Device count: " << deviceCount << std::endl;
184+
183185
for (uint32_t i = 0; i < deviceCount; ++i)
184186
{
185187
vkGetPhysicalDeviceProperties(devices[i], &deviceProperties);
186188

187-
std::cout << deviceProperties.deviceType << std::endl;
189+
std::cout << "Device type: " << deviceProperties.deviceType << std::endl;
190+
191+
std::cout << "Storage buffer offset alignment: " << deviceProperties.limits.minStorageBufferOffsetAlignment << std::endl;
188192

189193
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
190194
{

renderer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ void Renderer::ConstructFrames(VkBuffer& heightBuffer, VkBuffer& normalBuffer)
498498
vkCmdBeginRenderPass(drawCommandBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
499499
vkCmdBindDescriptorSets(drawCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
500500
vkCmdBindPipeline(drawCommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
501-
vkCmdBindVertexBuffers(drawCommandBuffers[i], 0, 3, buffers, offsets);
501+
vkCmdBindVertexBuffers(drawCommandBuffers[i], 0, numBindDesc, buffers, offsets);
502502
vkCmdBindIndexBuffer(drawCommandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
503503
vkCmdDrawIndexed(drawCommandBuffers[i], numIndices, 1, 0, 0, 0);
504504
vkCmdEndRenderPass(drawCommandBuffers[i]);
@@ -523,7 +523,7 @@ bool Renderer::SetupShaderParameters(VkDevice& device)
523523
bindingDescriptions[1].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
524524

525525
bindingDescriptions[2].binding = 2;
526-
bindingDescriptions[2].stride = sizeof(float[3]);
526+
bindingDescriptions[2].stride = sizeof(float[4]);
527527
bindingDescriptions[2].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
528528

529529
attributeDescriptions[0].binding = 0;

shader.comp

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ layout(std430, binding = 1) buffer Height
3232
float height[];
3333
};
3434

35-
layout(std430, binding = 2) buffer Normal
35+
layout(std140, binding = 2) buffer Normal
3636
{
37-
vec3 normal[];
37+
vec4 normal[];
3838
};
3939

4040
layout (local_size_x = 10, local_size_y = 10) in;
@@ -63,4 +63,5 @@ void main()
6363
normal[index].x = -ubo.amplitude*cos(kx - ubo.omega*ubo.time);
6464
normal[index].y = 1.0;
6565
normal[index].z = 0.0;
66+
normal[index].w = 1.0;
6667
}

shader.vert

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
layout(location = 0) in vec3 inPosition;
2020
layout(location = 1) in vec3 inColor;
21-
layout(location = 3) in vec3 inNormal;
21+
layout(location = 3) in vec4 inNormal;
2222
layout(location = 2) in float inHeight;
2323

2424
layout(binding = 0) uniform UBO {
@@ -48,7 +48,7 @@ void main() {
4848
outSpecularConst = 0.25;
4949
outSpecularLight = vec4(0.5, 0.5, 0.5, 1.0);
5050

51-
outNormal = normalize(inNormal);
51+
outNormal = normalize(inNormal.xyz);
5252
mat4 modelView = ubo.view * ubo.model;
5353
vec4 pos = modelView * vec4(inPosition.x, inHeight, inPosition.z, 1.0);
5454
outEyePos = vec3(modelView * pos);

vert.spv

48 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)