Skip to content
Snippets Groups Projects
Commit d9959b4d authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#70] Voxels can now store color

parent 4318961c
No related branches found
No related tags found
1 merge request!57Resolve "Basic voxelization"
Pipeline #25816 passed
......@@ -5,4 +5,21 @@ struct VoxelInfo{
uint flattenVoxelUVToIndex(ivec3 UV, ivec3 voxelImageSize){
return UV.x + UV.y * voxelImageSize.x + UV.z * voxelImageSize.x* voxelImageSize.y;
}
uint packVoxelInfo(vec3 color){
uint opaqueBit = 1 << 31;
uint redBits = uint(color.r * 255);
uint greenBits = uint(color.g * 255) << 8;
uint blueBits = uint(color.b * 255) << 16;
return opaqueBit | redBits | greenBits | blueBits;
}
vec4 unpackVoxelInfo(uint packed){
vec4 rgba;
rgba.r = (packed >> 0 & 0x000000FF) / 255.f;
rgba.g = (packed >> 8 & 0x000000FF) / 255.f;
rgba.b = (packed >> 16 & 0x000000FF) / 255.f;
rgba.a = packed >> 31;
return rgba;
}
\ No newline at end of file
......@@ -2,11 +2,11 @@
#extension GL_GOOGLE_include_directive : enable
#include "voxel.inc"
layout(set=0, binding=0) buffer voxelBuffer{
layout(set=0, binding=0, std430) buffer voxelBuffer{
uint isFilled[];
};
layout(set=0, binding=1, r8) uniform image3D voxelImage;
layout(set=0, binding=1, rgba16f) uniform image3D voxelImage;
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
......@@ -18,5 +18,7 @@ void main(){
}
ivec3 UV = ivec3(gl_GlobalInvocationID);
uint flatIndex = flattenVoxelUVToIndex(UV, voxelImageSize);
imageStore(voxelImage, UV, vec4(isFilled[flatIndex], vec3(0)));
vec4 color = unpackVoxelInfo(isFilled[flatIndex]);
imageStore(voxelImage, UV, vec4(color));
}
\ No newline at end of file
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location=0) in vec3 inColor;
layout(location=0) in vec3 passColorToFrag;
layout(location=0) out vec3 outColor;
void main() {
outColor = inColor;
outColor = passColorToFrag;
}
\ No newline at end of file
......@@ -9,95 +9,96 @@ layout( push_constant ) uniform constants{
};
layout(location = 0) in float passCubeHalf[1];
layout(location = 1) in vec3 passColorToGeom[1];
layout(location = 0) out vec3 passColor;
layout(location = 0) out vec3 passColorToFrag;
void main() {
float cubeHalf = passCubeHalf[0];
// right
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, 1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, -1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, 1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, -1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
// left
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, 1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, -1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, 1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, -1), 1);
passColor = vec3(1, 0, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
// back
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, -1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, -1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, -1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, -1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
// front
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, 1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, 1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, 1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, 1), 1);
passColor = vec3(0, 0, 1);
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
// bot
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, 1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, 1, -1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, 1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, 1, -1), 1);
passColor = vec3(0, 1, 0);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
// top
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, 1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(1, -1, -1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, 1), 1);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
gl_Position = viewProjection * vec4(gl_in[0].gl_Position.xyz + cubeHalf * vec3(-1, -1, -1), 1);
passColor = vec3(0, 1, 0);
passColor = vec3(0, 1, 0);
passColorToFrag = passColorToGeom[0];
EmitVertex();
EndPrimitive();
}
\ No newline at end of file
......@@ -5,12 +5,13 @@
#include "voxel.inc"
layout(location = 0) out float passCubeHalf;
layout(location = 1) out vec3 passColorToGeom;
layout( push_constant ) uniform constants{
mat4 viewProjection;
};
layout(set=0, binding=0, r8) uniform image3D voxelImage;
layout(set=0, binding=0, rgba16f) uniform image3D voxelImage;
layout(set=0, binding=1) uniform voxelizationInfo{
VoxelInfo voxelInfo;
};
......@@ -27,7 +28,9 @@ void main() {
vec3 position = (vec3(x, y, z) / voxelResolution - 0.5) * voxelInfo.extent + passCubeHalf + voxelInfo.offset;
gl_Position = vec4(position, 1.0);
if(imageLoad(voxelImage, ivec3(x,y,z)).x == 0){
vec4 voxelColor = imageLoad(voxelImage, ivec3(x,y,z));
if(voxelColor.a == 0){
gl_Position.x /= 0; // clip
}
passColorToGeom = voxelColor.rgb;
}
\ No newline at end of file
......@@ -6,7 +6,7 @@
layout(location = 0) in vec3 passPos;
layout(set=0, binding=0) buffer voxelizationBuffer{
layout(set=0, binding=0, std430) buffer voxelizationBuffer{
uint isFilled[];
};
......@@ -32,5 +32,7 @@ void main() {
return;
}
uint flatIndex = flattenVoxelUVToIndex(UV, voxelImageSize);
isFilled[flatIndex] = 1;
vec3 color = vec3(1, 1, 0);
isFilled[flatIndex] = packVoxelInfo(color);
}
\ No newline at end of file
......@@ -65,7 +65,7 @@ const vk::Format voxelizationDummyFormat = vk::Format::eR8Unorm;
Voxelization::Voxelization(vkcv::Core* corePtr, const Dependencies& dependencies)
:
m_corePtr(corePtr),
m_voxelImage(m_corePtr->createImage(vk::Format::eR8Unorm, voxelResolution, voxelResolution, voxelResolution, true)),
m_voxelImage(m_corePtr->createImage(vk::Format::eR16G16B16A16Sfloat, voxelResolution, voxelResolution, voxelResolution, true)),
m_dummyRenderTarget(m_corePtr->createImage(voxelizationDummyFormat, voxelResolution, voxelResolution, 1, false, true)),
m_voxelInfoBuffer(m_corePtr->createBuffer<VoxelizationInfo>(vkcv::BufferType::UNIFORM, 1)),
m_visualisationIndexBuffer(m_corePtr->createBuffer<uint16_t>(vkcv::BufferType::INDEX, voxelCount)),
......
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