Files
2025-09-04 10:54:47 +08:00

109 lines
2.6 KiB
Plaintext

/* Copyright (c) 2023, Mobica Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#version 450
#extension GL_EXT_mesh_shader : require
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(lines, max_vertices = 64, max_primitives = 126) out;
layout(std140, binding = 2) uniform UniformBufferObject
{
mat4 model;
mat4 view;
mat4 proj;
} ubo;
struct s_vertex
{
vec4 position;
vec4 normal;
};
layout (std430, binding = 4) buffer _vertices
{
s_vertex vertices[];
} vb;
struct s_meshlet
{
uint vertices[64];
uint indices[126];
uint vertex_count;
uint index_count;
};
layout (std430, binding = 3) buffer _meshlets
{
s_meshlet meshlets[];
} mbuf;
layout (location = 0) out PerVertexData
{
vec4 color;
} v_out[];
void main(void)
{
uint meshlet_index = gl_WorkGroupID.x;
uint thread_id = gl_LocalInvocationID.x;
uint vertex_count = mbuf.meshlets[meshlet_index].vertex_count;
uint index_count = mbuf.meshlets[meshlet_index].index_count;
uint primitive_count = index_count/3;
SetMeshOutputsEXT( primitive_count * 2, primitive_count);
float normalLength = 0.1f;
mat4 MVP = ubo.proj * ubo.view * ubo.model;
uint j = 0;
uint k = 0;
for (uint i = 0; i < primitive_count; ++i)
{
//triangle indices
uint vi1 = mbuf.meshlets[meshlet_index].indices[j];
uint vi2 = mbuf.meshlets[meshlet_index].indices[j + 1];
uint vi3 = mbuf.meshlets[meshlet_index].indices[j + 2];
//middle point of triangle
vec3 pos = (vb.vertices[vi1].position.xyz + vb.vertices[vi2].position.xyz + vb.vertices[vi3].position.xyz)/3.0f;
vec3 normal = vb.vertices[vi1].normal.xyz;
//line vertices
gl_MeshVerticesEXT[k].gl_Position = MVP * vec4(pos ,1.0f);
v_out[k].color = vec4(0.0f, 0.0f, 1.0f, 1.0f);
gl_MeshVerticesEXT[k + 1].gl_Position = MVP * vec4(pos + normal * normalLength, 1.0f);
v_out[k + 1].color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
k = k + 2;
j= j + 3;
}
//indices for line vertices
k = 0;
for (uint i = 0; i < primitive_count; i++)
{
gl_PrimitiveLineIndicesEXT[i] = uvec2(k,k+1);
k = k + 2;
}
}