Skip to content
Snippets Groups Projects
Verified Commit 49588295 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Added red point rendering to clipping in skull demo

parent a3575e03
No related branches found
No related tags found
No related merge requests found
#define CLIP_SCALE 10000.0f
vec4 clipPosition(vec4 pos) {
return vec4(
max(clipX, pos.x),
max(clipY, pos.y),
max(clipZ, pos.z),
1.0f / CLIP_SCALE
);
}
vec4 clipByLimit(vec4 pos) {
if (pos.x / pos.w < clipLimit) {
return vec4(pos.xyz / pos.w, 1.0f);
} else {
return vec4(clipLimit, pos.y / pos.w, pos.z / pos.w, 1.0f);
}
}
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 passNormal;
layout(location = 0) out vec3 outColor;
void main() {
outColor = (vec3(0.3f, 0, 0) + max(dot(passNormal, vec3(1.0f, -1.0f, 0.5f)), 0.0f) * vec3(0.7f, 0, 0));
}
\ No newline at end of file
#version 450 #version 450
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
layout(triangles) in; layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out; layout(triangle_strip, max_vertices = 3) out;
...@@ -19,24 +20,7 @@ layout( push_constant ) uniform constants{ ...@@ -19,24 +20,7 @@ layout( push_constant ) uniform constants{
mat4 mvp; mat4 mvp;
}; };
#define CLIP_SCALE 10000.0f #include "clip.inc"
vec4 clipPosition(vec4 pos) {
return vec4(
min(clipX, pos.x),
min(clipY, pos.y),
min(clipZ, pos.z),
1.0f / CLIP_SCALE
);
}
vec4 clipByLimit(vec4 pos) {
if (pos.x / pos.w < clipLimit) {
return vec4(pos.xyz / pos.w, 1.0f);
} else {
return vec4(clipLimit, pos.y / pos.w, pos.z / pos.w, 1.0f);
}
}
void main() { void main() {
vec4 v0 = gl_in[0].gl_Position; vec4 v0 = gl_in[0].gl_Position;
...@@ -52,24 +36,18 @@ void main() { ...@@ -52,24 +36,18 @@ void main() {
float dz = abs(v0.z - clipZ) + abs(v1.z - clipZ) + abs(v2.z - clipZ); float dz = abs(v0.z - clipZ) + abs(v1.z - clipZ) + abs(v2.z - clipZ);
if (dx * dy * dz > 0.0f) { if (dx * dy * dz > 0.0f) {
v0 = clipByLimit(mvp * (v0 * CLIP_SCALE)); gl_Position = mvp * (v0 * CLIP_SCALE);
v1 = clipByLimit(mvp * (v1 * CLIP_SCALE)); passNormal = geomNormal[0];
v2 = clipByLimit(mvp * (v2 * CLIP_SCALE)); EmitVertex();
if ((v0.x < clipLimit) || (v1.x < clipLimit) || (v2.x < clipLimit)) {
gl_Position = v0;
passNormal = geomNormal[0];
EmitVertex();
gl_Position = v1; gl_Position = mvp * (v1 * CLIP_SCALE);
passNormal = geomNormal[1]; passNormal = geomNormal[1];
EmitVertex(); EmitVertex();
gl_Position = v2; gl_Position = mvp * (v2 * CLIP_SCALE);
passNormal = geomNormal[2]; passNormal = geomNormal[2];
EmitVertex(); EmitVertex();
EndPrimitive(); EndPrimitive();
}
} }
} }
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
layout(triangles) in;
layout(points, max_vertices = 1) out;
layout(location = 0) in vec3 geomNormal[];
layout(location = 0) out vec3 passNormal;
layout(set=1, binding=0) uniform clipBuffer {
float clipLimit;
float clipX;
float clipY;
float clipZ;
};
layout( push_constant ) uniform constants{
mat4 mvp;
};
#include "clip.inc"
void main() {
vec4 v0 = gl_in[0].gl_Position;
vec4 v1 = gl_in[1].gl_Position;
vec4 v2 = gl_in[2].gl_Position;
v0 = clipPosition(v0 / CLIP_SCALE);
v1 = clipPosition(v1 / CLIP_SCALE);
v2 = clipPosition(v2 / CLIP_SCALE);
float dx = abs(v0.x - clipX) + abs(v1.x - clipX) + abs(v2.x - clipX);
float dy = abs(v0.y - clipY) + abs(v1.y - clipY) + abs(v2.y - clipY);
float dz = abs(v0.z - clipZ) + abs(v1.z - clipZ) + abs(v2.z - clipZ);
if (dx * dy * dz <= 0.0f) {
v0 = clipByLimit(mvp * gl_in[0].gl_Position);
v1 = clipByLimit(mvp * gl_in[1].gl_Position);
v2 = clipByLimit(mvp * gl_in[2].gl_Position);
if ((v0.x < clipLimit) || (v1.x < clipLimit) || (v2.x < clipLimit)) {
gl_Position = (v0 + v1 + v2) / 3;
passNormal = (geomNormal[0] + geomNormal[1] + geomNormal[2]) / 3;
EmitVertex();
EndPrimitive();
}
}
}
...@@ -38,27 +38,43 @@ int main(int argc, const char** argv) { ...@@ -38,27 +38,43 @@ int main(int argc, const char** argv) {
argc > 1 ? argv[1] : "assets/skull_scaled/scene.gltf" argc > 1 ? argv[1] : "assets/skull_scaled/scene.gltf"
)); ));
const vkcv::AttachmentDescription present_color_attachment( const vkcv::AttachmentDescription present_color_attachment0(
vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::CLEAR, vkcv::AttachmentOperation::CLEAR,
core.getSwapchain(windowHandle).getFormat() core.getSwapchain(windowHandle).getFormat()
); );
const vkcv::AttachmentDescription depth_attachment( const vkcv::AttachmentDescription depth_attachment0(
vkcv::AttachmentOperation::STORE, vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::CLEAR, vkcv::AttachmentOperation::CLEAR,
vk::Format::eD32Sfloat vk::Format::eD32Sfloat
); );
vkcv::PassConfig scenePassDefinition({ present_color_attachment, depth_attachment }); vkcv::PassConfig scenePassDefinition({ present_color_attachment0, depth_attachment0 });
vkcv::PassHandle scenePass = core.createPass(scenePassDefinition); vkcv::PassHandle scenePass = core.createPass(scenePassDefinition);
if (!scenePass) { const vkcv::AttachmentDescription present_color_attachment1(
vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::LOAD,
core.getSwapchain(windowHandle).getFormat()
);
const vkcv::AttachmentDescription depth_attachment1(
vkcv::AttachmentOperation::STORE,
vkcv::AttachmentOperation::LOAD,
vk::Format::eD32Sfloat
);
vkcv::PassConfig linePassDefinition({ present_color_attachment1, depth_attachment1 });
vkcv::PassHandle linePass = core.createPass(linePassDefinition);
if ((!scenePass) || (!linePass)) {
std::cout << "Error. Could not create renderpass. Exiting." << std::endl; std::cout << "Error. Could not create renderpass. Exiting." << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
vkcv::ShaderProgram sceneShaderProgram; vkcv::ShaderProgram sceneShaderProgram;
vkcv::ShaderProgram lineShaderProgram;
vkcv::shader::GLSLCompiler compiler; vkcv::shader::GLSLCompiler compiler;
compiler.compileProgram(sceneShaderProgram, { compiler.compileProgram(sceneShaderProgram, {
...@@ -67,6 +83,12 @@ int main(int argc, const char** argv) { ...@@ -67,6 +83,12 @@ int main(int argc, const char** argv) {
{ vkcv::ShaderStage::FRAGMENT, "assets/shaders/shader.frag" } { vkcv::ShaderStage::FRAGMENT, "assets/shaders/shader.frag" }
}, nullptr); }, nullptr);
compiler.compileProgram(lineShaderProgram, {
{ vkcv::ShaderStage::VERTEX, "assets/shaders/shader.vert" },
{ vkcv::ShaderStage::GEOMETRY, "assets/shaders/wired.geom" },
{ vkcv::ShaderStage::FRAGMENT, "assets/shaders/red.frag" }
}, nullptr);
const std::vector<vkcv::VertexAttachment> vertexAttachments = sceneShaderProgram.getVertexAttachments(); const std::vector<vkcv::VertexAttachment> vertexAttachments = sceneShaderProgram.getVertexAttachments();
std::vector<vkcv::VertexBinding> bindings; std::vector<vkcv::VertexBinding> bindings;
for (size_t i = 0; i < vertexAttachments.size(); i++) { for (size_t i = 0; i < vertexAttachments.size(); i++) {
...@@ -84,7 +106,7 @@ int main(int argc, const char** argv) { ...@@ -84,7 +106,7 @@ int main(int argc, const char** argv) {
float clipZ = 0.0f; float clipZ = 0.0f;
auto clipBuffer = core.createBuffer<float>(vkcv::BufferType::UNIFORM, 4); auto clipBuffer = core.createBuffer<float>(vkcv::BufferType::UNIFORM, 4);
clipBuffer.fill({ clipLimit, clipX, clipY, clipZ }); clipBuffer.fill({ clipLimit, -clipX, -clipY, -clipZ });
vkcv::DescriptorWrites clipWrites; vkcv::DescriptorWrites clipWrites;
clipWrites.uniformBufferWrites = { clipWrites.uniformBufferWrites = {
...@@ -128,9 +150,21 @@ int main(int argc, const char** argv) { ...@@ -128,9 +150,21 @@ int main(int argc, const char** argv) {
{ material0.getDescriptorSetLayout(), clipDescriptorSetLayout }, { material0.getDescriptorSetLayout(), clipDescriptorSetLayout },
true true
}; };
const vkcv::GraphicsPipelineConfig linePipelineDefinition{
lineShaderProgram,
UINT32_MAX,
UINT32_MAX,
linePass,
{sceneLayout},
{ material0.getDescriptorSetLayout(), clipDescriptorSetLayout },
true
};
vkcv::GraphicsPipelineHandle scenePipeline = core.createGraphicsPipeline(scenePipelineDefinition); vkcv::GraphicsPipelineHandle scenePipeline = core.createGraphicsPipeline(scenePipelineDefinition);
vkcv::GraphicsPipelineHandle linePipeline = core.createGraphicsPipeline(linePipelineDefinition);
if (!scenePipeline) { if ((!scenePipeline) || (!linePipeline)) {
std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
...@@ -170,7 +204,7 @@ int main(int argc, const char** argv) { ...@@ -170,7 +204,7 @@ int main(int argc, const char** argv) {
start = end; start = end;
cameraManager.update(0.000001 * static_cast<double>(deltatime.count())); cameraManager.update(0.000001 * static_cast<double>(deltatime.count()));
clipBuffer.fill({ clipLimit, clipX, clipY, clipZ }); clipBuffer.fill({ clipLimit, -clipX, -clipY, -clipZ });
const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer }; const std::vector<vkcv::ImageHandle> renderTargets = { swapchainInput, depthBuffer };
auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics);
...@@ -193,6 +227,15 @@ int main(int argc, const char** argv) { ...@@ -193,6 +227,15 @@ int main(int argc, const char** argv) {
renderTargets, renderTargets,
windowHandle); windowHandle);
scene.recordDrawcalls(cmdStream,
cameraManager.getActiveCamera(),
linePass,
linePipeline,
sizeof(glm::mat4),
recordMesh,
renderTargets,
windowHandle);
core.prepareSwapchainImageForPresent(cmdStream); core.prepareSwapchainImageForPresent(cmdStream);
core.submitCommandStream(cmdStream); core.submitCommandStream(cmdStream);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment