Crafter.Graphics/MeshShaderMixedVoxelGrid.glsl

159 lines
6.1 KiB
Text
Raw Normal View History

2025-06-10 22:47:47 +02:00
/*
* Crafter®.Graphics
* Copyright (C) 2025 Catcrafts®
* Catcrafts.net
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#version 450
#extension GL_EXT_mesh_shader : require
layout (binding = 0) uniform UBO
{
mat4 modelProjectionView;
uint sizeX;
uint sizeY;
uint sizeZ;
} ubo;
struct VoxelType
{
uint type;
};
layout (binding = 1) buffer VOXELS
{
VoxelType voxel[];
} voxels;
2025-06-10 23:55:44 +02:00
2025-06-10 22:47:47 +02:00
layout (location = 0) out PerVertexData
{
vec4 color;
} outVert[];
2025-06-10 23:55:44 +02:00
2025-06-10 22:47:47 +02:00
shared uint writeCounter;
layout(local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
layout(triangles, max_vertices = 128, max_primitives = 192) out;
void main()
{
if (gl_LocalInvocationIndex == 0) {
writeCounter = 0;
}
2025-06-10 22:47:47 +02:00
barrier();
2025-06-10 22:47:47 +02:00
uint type = voxels.voxel[gl_GlobalInvocationID.x].type;
2025-06-10 22:47:47 +02:00
if(type != 0) {
2025-06-10 22:47:47 +02:00
uint old = atomicAdd(writeCounter, 1);
uint z = gl_GlobalInvocationID.x / (ubo.sizeX * ubo.sizeY);
uint y = (gl_GlobalInvocationID.x % (ubo.sizeX * ubo.sizeY)) / ubo.sizeX;
uint x = gl_GlobalInvocationID.x % ubo.sizeX;
float zF = float(z);
float yF = float(y);
float xF = float(x);
uint oldVertexOffset = old*8;
2025-06-10 23:55:44 +02:00
float xC = xF / float((ubo.sizeX) - 1);
float yC = yF / float((ubo.sizeY) - 1);
float zC = zF / float((ubo.sizeZ) - 1);
2025-06-10 22:58:24 +02:00
gl_MeshVerticesEXT[oldVertexOffset + 0].gl_Position = ubo.modelProjectionView * vec4(xF - 0.5, yF - 0.5, zF - 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 1].gl_Position = ubo.modelProjectionView * vec4(xF + 0.5, yF - 0.5, zF - 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 2].gl_Position = ubo.modelProjectionView * vec4(xF + 0.5, yF + 0.5, zF - 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 3].gl_Position = ubo.modelProjectionView * vec4(xF - 0.5, yF + 0.5, zF - 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 4].gl_Position = ubo.modelProjectionView * vec4(xF - 0.5, yF - 0.5, zF + 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 5].gl_Position = ubo.modelProjectionView * vec4(xF + 0.5, yF - 0.5, zF + 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 6].gl_Position = ubo.modelProjectionView * vec4(xF + 0.5, yF + 0.5, zF + 0.5, 1.0);
gl_MeshVerticesEXT[oldVertexOffset + 7].gl_Position = ubo.modelProjectionView * vec4(xF - 0.5, yF + 0.5, zF + 0.5, 1.0);
2025-06-10 22:47:47 +02:00
2025-06-10 23:55:44 +02:00
outVert[oldVertexOffset + 0].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 1].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 2].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 3].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 4].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 5].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 6].color = vec4(xC, yC, zC, 1);
outVert[oldVertexOffset + 7].color = vec4(xC, yC, zC, 1);
2025-06-10 22:47:47 +02:00
uint oldPrimOffset = old*12;
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 0] = uvec3(oldVertexOffset + 2, oldVertexOffset + 1, oldVertexOffset + 0);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 1] = uvec3(oldVertexOffset + 0, oldVertexOffset + 3, oldVertexOffset + 2);
2025-06-10 22:58:24 +02:00
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 2] = uvec3(oldVertexOffset + 4, oldVertexOffset + 5, oldVertexOffset + 6);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 3] = uvec3(oldVertexOffset + 4, oldVertexOffset + 6, oldVertexOffset + 7);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 4] = uvec3(oldVertexOffset + 0, oldVertexOffset + 1, oldVertexOffset + 5);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 5] = uvec3(oldVertexOffset + 5, oldVertexOffset + 4, oldVertexOffset + 0);
2025-06-10 22:58:24 +02:00
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 6] = uvec3(oldVertexOffset + 3, oldVertexOffset + 6, oldVertexOffset + 2);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 7] = uvec3(oldVertexOffset + 3, oldVertexOffset + 7, oldVertexOffset + 6);
2025-06-10 22:58:24 +02:00
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 8] = uvec3(oldVertexOffset + 7, oldVertexOffset + 3, oldVertexOffset + 0);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 9] = uvec3(oldVertexOffset + 4, oldVertexOffset + 7, oldVertexOffset + 0);
2025-06-10 22:58:24 +02:00
2025-06-10 22:47:47 +02:00
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 10] = uvec3(oldVertexOffset + 1, oldVertexOffset + 2, oldVertexOffset + 6);
gl_PrimitiveTriangleIndicesEXT[oldPrimOffset + 11] = uvec3(oldVertexOffset + 6, oldVertexOffset + 5, oldVertexOffset + 1);
}
2025-06-10 22:47:47 +02:00
barrier();
2025-06-10 22:47:47 +02:00
SetMeshOutputsEXT(writeCounter*8, writeCounter*12);
2025-06-10 22:47:47 +02:00
}
/**
int z = gl_GlobalInvocationID.x / (ubo.sizeX * ubo.sizeY);
int y = (gl_GlobalInvocationID.x % (ubo.sizeX * ubo.sizeY)) / ubo.sizeX;
int x = gl_GlobalInvocationID.x % ubo.sizeX;
// Top (y+1) if within bounds
if (y < ubo.sizeY - 1 && vertex.pos[gl_GlobalInvocationID.x + ubo.sizeX]) {
}
// Bottom (y-1) if within bounds
if (y > 0 && vertex.pos[gl_GlobalInvocationID.x - ubo.sizeX]) {
}
// Left (x-1) if within bounds
if (x > 0 && vertex.pos[gl_GlobalInvocationID.x - 1]) {
}
// Right (x+1) if within bounds
if (x < ubo.sizeX - 1 && vertex.pos[gl_GlobalInvocationID.x + 1]) {
}
// Front (z+1) if within bounds
if (z < ubo.sizeZ - 1 && vertex.pos[gl_GlobalInvocationID.x + ubo.sizeX * ubo.sizeY]) {
}
// Back (z-1) if within bounds
if (z > 0 && vertex.pos[gl_GlobalInvocationID.x - ubo.sizeX * ubo.sizeY]) {
}
**/