Calculate normals only on last iteration
This commit is contained in:
parent
db2aee7535
commit
f383849c24
2 changed files with 55 additions and 42 deletions
|
|
@ -251,11 +251,20 @@ public:
|
||||||
|
|
||||||
vkCmdBindPipeline(compute.commandBuffers[i], VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipeline);
|
vkCmdBindPipeline(compute.commandBuffers[i], VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipeline);
|
||||||
|
|
||||||
|
uint32_t calculateNormals = 0;
|
||||||
|
vkCmdPushConstants(compute.commandBuffers[i], compute.pipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &calculateNormals);
|
||||||
|
|
||||||
// Dispatch the compute job
|
// Dispatch the compute job
|
||||||
for (uint32_t j = 0; j < 64; j++) {
|
const uint32_t iterations = 64;
|
||||||
|
for (uint32_t j = 0; j < iterations; j++) {
|
||||||
readSet = 1 - readSet;
|
readSet = 1 - readSet;
|
||||||
vkCmdBindDescriptorSets(compute.commandBuffers[i], VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipelineLayout, 0, 1, &compute.descriptorSets[readSet], 0, 0);
|
vkCmdBindDescriptorSets(compute.commandBuffers[i], VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipelineLayout, 0, 1, &compute.descriptorSets[readSet], 0, 0);
|
||||||
|
|
||||||
|
if (j == iterations - 1) {
|
||||||
|
calculateNormals = 1;
|
||||||
|
vkCmdPushConstants(compute.commandBuffers[i], compute.pipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(uint32_t), &calculateNormals);
|
||||||
|
}
|
||||||
|
|
||||||
vkCmdDispatch(compute.commandBuffers[i], cloth.gridsize.x / 10, cloth.gridsize.y / 10, 1);
|
vkCmdDispatch(compute.commandBuffers[i], cloth.gridsize.x / 10, cloth.gridsize.y / 10, 1);
|
||||||
|
|
||||||
for (auto &barrier : bufferBarriers) {
|
for (auto &barrier : bufferBarriers) {
|
||||||
|
|
@ -559,24 +568,23 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
||||||
vks::initializers::descriptorSetLayoutCreateInfo(
|
vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
||||||
setLayoutBindings.data(),
|
|
||||||
static_cast<uint32_t>(setLayoutBindings.size()));
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout));
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo =
|
||||||
vks::initializers::pipelineLayoutCreateInfo(
|
vks::initializers::pipelineLayoutCreateInfo(&compute.descriptorSetLayout, 1);
|
||||||
&compute.descriptorSetLayout,
|
|
||||||
1);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
|
// Push constants used to pass some parameters
|
||||||
|
VkPushConstantRange pushConstantRange =
|
||||||
|
vks::initializers::pushConstantRange(VK_SHADER_STAGE_COMPUTE_BIT, sizeof(uint32_t), 0);
|
||||||
|
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
|
||||||
|
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
|
||||||
|
|
||||||
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
|
||||||
|
|
||||||
VkDescriptorSetAllocateInfo allocInfo =
|
VkDescriptorSetAllocateInfo allocInfo =
|
||||||
vks::initializers::descriptorSetAllocateInfo(
|
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &compute.descriptorSetLayout, 1);
|
||||||
descriptorPool,
|
|
||||||
&compute.descriptorSetLayout,
|
|
||||||
1);
|
|
||||||
|
|
||||||
// Create two descriptor sets with input and output buffers switched
|
// Create two descriptor sets with input and output buffers switched
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSets[0]));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSets[0]));
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,10 @@ layout (binding = 2) uniform UBO
|
||||||
ivec2 particleCount;
|
ivec2 particleCount;
|
||||||
} params;
|
} params;
|
||||||
|
|
||||||
|
layout (push_constant) uniform PushConsts {
|
||||||
|
uint calculateNormals;
|
||||||
|
} pushConsts;
|
||||||
|
|
||||||
vec3 springForce(vec3 p0, vec3 p1, float restDist)
|
vec3 springForce(vec3 p0, vec3 p1, float restDist)
|
||||||
{
|
{
|
||||||
vec3 dist = p0 - p1;
|
vec3 dist = p0 - p1;
|
||||||
|
|
@ -113,36 +117,37 @@ void main()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normals
|
// Normals
|
||||||
// todo: Only once (use push const to check)
|
if (pushConsts.calculateNormals == 1) {
|
||||||
vec3 normal = vec3(0.0);
|
vec3 normal = vec3(0.0);
|
||||||
vec3 a, b, c;
|
vec3 a, b, c;
|
||||||
if (id.y > 0) {
|
if (id.y > 0) {
|
||||||
if (id.x > 0) {
|
if (id.x > 0) {
|
||||||
a = particleIn[index - 1].pos.xyz - pos;
|
a = particleIn[index - 1].pos.xyz - pos;
|
||||||
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
|
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
|
||||||
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||||
normal += cross(a,b) + cross(b,c);
|
normal += cross(a,b) + cross(b,c);
|
||||||
|
}
|
||||||
|
if (id.x < params.particleCount.x - 1) {
|
||||||
|
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
||||||
|
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
|
||||||
|
c = particleIn[index + 1].pos.xyz - pos;
|
||||||
|
normal += cross(a,b) + cross(b,c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (id.x < params.particleCount.x - 1) {
|
if (id.y < params.particleCount.y - 1) {
|
||||||
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
|
if (id.x > 0) {
|
||||||
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
|
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||||
c = particleIn[index + 1].pos.xyz - pos;
|
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
|
||||||
normal += cross(a,b) + cross(b,c);
|
c = particleIn[index - 1].pos.xyz - pos;
|
||||||
|
normal += cross(a,b) + cross(b,c);
|
||||||
|
}
|
||||||
|
if (id.x < params.particleCount.x - 1) {
|
||||||
|
a = particleIn[index + 1].pos.xyz - pos;
|
||||||
|
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
|
||||||
|
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
||||||
|
normal += cross(a,b) + cross(b,c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
particleOut[index].normal = vec4(normalize(normal), 0.0f);
|
||||||
}
|
}
|
||||||
if (id.y < params.particleCount.y - 1) {
|
|
||||||
if (id.x > 0) {
|
|
||||||
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
|
||||||
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
|
|
||||||
c = particleIn[index - 1].pos.xyz - pos;
|
|
||||||
normal += cross(a,b) + cross(b,c);
|
|
||||||
}
|
|
||||||
if (id.x < params.particleCount.x - 1) {
|
|
||||||
a = particleIn[index + 1].pos.xyz - pos;
|
|
||||||
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
|
|
||||||
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
|
|
||||||
normal += cross(a,b) + cross(b,c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
particleOut[index].normal = vec4(normalize(normal), 0.0f);
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue