feat: draw meshes with instanced multi-draw indirect
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#ifndef INSTANCE_DATA_GLSL
|
||||
#define INSTANCE_DATA_GLSL
|
||||
|
||||
#extension GL_EXT_buffer_reference : require
|
||||
#extension GL_EXT_scalar_block_layout : require
|
||||
|
||||
#include "vertex.glsl"
|
||||
|
||||
// Must match GPUInstanceData in IndirectDrawList.h
|
||||
struct InstanceData {
|
||||
mat4 transform;
|
||||
VertexBuffer vertexBuffer;
|
||||
uint materialID;
|
||||
uint padding;
|
||||
};
|
||||
|
||||
layout (buffer_reference, scalar) readonly buffer InstanceBuffer {
|
||||
InstanceData instances[];
|
||||
};
|
||||
|
||||
#endif // INSTANCE_DATA_GLSL
|
||||
@@ -12,6 +12,7 @@ layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec2 inUV;
|
||||
layout (location = 2) in vec3 inNormal;
|
||||
layout (location = 3) in vec4 inTangent;
|
||||
layout (location = 4) flat in uint inMaterialID;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
@@ -62,7 +63,7 @@ float calculateShadow(Light light, vec3 fragPos, vec3 geometryNormal)
|
||||
|
||||
void main()
|
||||
{
|
||||
MaterialData material = pcs.sceneData.materials.data[pcs.materialID];
|
||||
MaterialData material = pcs.sceneData.materials.data[inMaterialID];
|
||||
uint samplerID = material.textureFilteringMode;
|
||||
|
||||
vec4 diffuse = sampleTexture2D(material.diffuseTex, samplerID, inUV) * material.baseColor;
|
||||
|
||||
@@ -8,6 +8,7 @@ layout (location = 0) out vec3 outPos;
|
||||
layout (location = 1) out vec2 outUV;
|
||||
layout (location = 2) out vec3 outNormal;
|
||||
layout (location = 3) out vec4 outTangent;
|
||||
layout (location = 4) flat out uint outMaterialID;
|
||||
|
||||
// The depth pre-pass reuses this shader, positions have to match bit for bit
|
||||
// so the main pass can depth test against it with LESS_OR_EQUAL
|
||||
@@ -15,9 +16,11 @@ invariant gl_Position;
|
||||
|
||||
void main()
|
||||
{
|
||||
Vertex v = pcs.vertexBuffer.vertices[gl_VertexIndex];
|
||||
// gl_InstanceIndex already includes the indirect command's firstInstance
|
||||
InstanceData instance = pcs.instanceBuffer.instances[gl_InstanceIndex];
|
||||
Vertex v = instance.vertexBuffer.vertices[gl_VertexIndex];
|
||||
|
||||
vec4 worldPos = pcs.transform * vec4(v.position, 1.0f);
|
||||
vec4 worldPos = instance.transform * vec4(v.position, 1.0f);
|
||||
|
||||
gl_Position = pcs.sceneData.viewProj * worldPos;
|
||||
outPos = worldPos.xyz;
|
||||
@@ -25,9 +28,10 @@ void main()
|
||||
// A bit inefficient, but okay - this is needed for non-uniform scale
|
||||
// models. See: http://www.lighthouse3d.com/tutorials/glsl-12-tutorial/the-normal-matrix/
|
||||
// Simpler case, when everything is uniform
|
||||
// outNormal = (pcs.transform * vec4(v.normal, 0.0)).xyz;
|
||||
outNormal = mat3(transpose(inverse(pcs.transform))) * v.normal;
|
||||
// outNormal = (instance.transform * vec4(v.normal, 0.0)).xyz;
|
||||
outNormal = mat3(transpose(inverse(instance.transform))) * v.normal;
|
||||
|
||||
// w is the bitangent sign, the fragment shader builds the TBN from the interpolated vectors
|
||||
outTangent = vec4(mat3(pcs.transform) * v.tangent.xyz, v.tangent.w);
|
||||
outTangent = vec4(mat3(instance.transform) * v.tangent.xyz, v.tangent.w);
|
||||
outMaterialID = instance.materialID;
|
||||
}
|
||||
|
||||
@@ -2,14 +2,10 @@
|
||||
#extension GL_EXT_scalar_block_layout: require
|
||||
|
||||
#include "scene_data.glsl"
|
||||
#include "vertex.glsl"
|
||||
#include "instance_data.glsl"
|
||||
|
||||
layout (push_constant, scalar) uniform constants
|
||||
{
|
||||
mat4 transform;
|
||||
SceneDataBuffer sceneData;
|
||||
VertexBuffer vertexBuffer;
|
||||
uint materialID;
|
||||
uint padding;
|
||||
InstanceBuffer instanceBuffer;
|
||||
} pcs;
|
||||
|
||||
|
||||
@@ -4,17 +4,18 @@
|
||||
#extension GL_EXT_buffer_reference : require
|
||||
#extension GL_EXT_scalar_block_layout : require
|
||||
|
||||
#include "vertex.glsl"
|
||||
#include "instance_data.glsl"
|
||||
|
||||
layout (push_constant, scalar) uniform constants
|
||||
{
|
||||
mat4 modelToLightClip;
|
||||
VertexBuffer vertexBuffer;
|
||||
mat4 lightViewProj;
|
||||
InstanceBuffer instanceBuffer;
|
||||
} pcs;
|
||||
|
||||
void main()
|
||||
{
|
||||
Vertex v = pcs.vertexBuffer.vertices[gl_VertexIndex];
|
||||
InstanceData instance = pcs.instanceBuffer.instances[gl_InstanceIndex];
|
||||
Vertex v = instance.vertexBuffer.vertices[gl_VertexIndex];
|
||||
|
||||
gl_Position = pcs.modelToLightClip * vec4(v.position, 1.0f);
|
||||
gl_Position = pcs.lightViewProj * instance.transform * vec4(v.position, 1.0f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user