diff --git a/assets/shaders/deferred.frag b/assets/shaders/deferred.frag new file mode 100644 index 0000000..11ba61e --- /dev/null +++ b/assets/shaders/deferred.frag @@ -0,0 +1,24 @@ +#version 450 + +layout(set = 0, binding = 0) uniform sampler2D positionTexture; + +layout(set = 0, binding = 1) uniform sampler2D normalTexture; + +layout(set = 0, binding = 2) uniform sampler2D albedoTexture; + +layout(set = 0, binding = 3) uniform sampler2D specularTexture; + +layout (location = 0) in vec2 inUV; + +layout(location = 0) out vec4 outColor; + +void main() +{ + vec3 fragPos = texture(positionTexture, inUV).rgb; + vec3 normal = texture(normalTexture, inUV).rgb; + vec4 albedo = texture(albedoTexture, inUV); + + // outColor = vec4(fragPos, 1.0); + // outColor = vec4(normal, 1.0); + outColor = albedo; +} diff --git a/assets/shaders/deferred.vert b/assets/shaders/deferred.vert new file mode 100644 index 0000000..b9a0e0f --- /dev/null +++ b/assets/shaders/deferred.vert @@ -0,0 +1,9 @@ +#version 450 + +layout (location = 0) out vec2 outUV; + +void main() +{ + outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f); +} diff --git a/assets/shaders/mrt.frag b/assets/shaders/mrt.frag new file mode 100755 index 0000000..55caee2 --- /dev/null +++ b/assets/shaders/mrt.frag @@ -0,0 +1,96 @@ +#version 450 +#extension GL_EXT_nonuniform_qualifier : require + +struct Material { + vec4 baseColor; + uint albedoTexture; + + uint normalTexture; + + float metallicFactor; + float roughnessFactor; + uint metallicRoughnessTexture; +}; + +struct Light { + vec3 color; + uint intensity; +}; + +struct Scene { + uint lightsCount; +}; + +vec3 directional_light = vec3(1.0); + +layout( push_constant ) uniform constants +{ + mat4 render_matrix; + uint indexBufferIndex; + uint vertexBufferIndex; + uint materialBufferIndex; +} PushConstants; + +layout(set = 0, binding = 0) uniform SceneData { + layout(align = 16) Scene mesh; +} SceneBuffers[]; + +layout(set = 0, binding = 1) uniform LightBuffer { + layout(align = 16) Light lights[]; +} LightsBuffers; + +layout(set = 2, binding = 3) buffer MaterialBuffer { + layout(align = 16) Material materials[]; +} MaterialBuffers; + +layout(set = 2, binding = 4) uniform sampler2D texSampler[]; + +layout(location = 0) in vec2 uv; +layout(location = 1) smooth in mat3 TBN; +layout(location = 4) in vec4 outpos; + +layout (location = 0) out vec4 gPosition; +layout (location = 1) out vec3 gNormal; +layout (location = 2) out vec3 gAlbedo; + +float near = 0.001; +float far = 50.0; + +float LinearizeDepth(float depth) +{ + float z = depth * 2.0 - 1.0; // back to NDC + return (2.0 * near * far) / (far + near - z * (far - near)); +} + +void main() { + Material material = MaterialBuffers.materials[ PushConstants.materialBufferIndex ]; + + vec3 out_normal; + if(material.normalTexture == 0) { + out_normal = TBN[2].xyz; + } else { + vec3 sampled_normal = texture(texSampler[material.normalTexture], uv).rgb; + sampled_normal = 2.0 * sampled_normal - vec3(1.0); + out_normal = normalize(TBN * sampled_normal); + } + + + float intensity = dot(out_normal, normalize(directional_light)) + 0.2; + + vec3 inColor; + if(material.albedoTexture == 0) { + inColor = material.baseColor.xyz; + } else { + inColor = texture(texSampler[material.albedoTexture], uv).xyz; + } + + float depth = LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration + + gPosition = outpos; + gNormal = out_normal; + gAlbedo = inColor; + // outColor = vec4(vec3(outpos.z) * 0.5 + 0.5 , 1.0); + // outColor = vec4(inColor, 1.0); + // outColor = vec4(vec3(intensity), 1.0); + // outColor = vec4(out_normal, 1.0); +} diff --git a/assets/shaders/mrt.vert b/assets/shaders/mrt.vert new file mode 100755 index 0000000..dc96acb --- /dev/null +++ b/assets/shaders/mrt.vert @@ -0,0 +1,86 @@ +#version 450 +#extension GL_EXT_nonuniform_qualifier : require + +struct Material { + vec4 baseColor; + uint albedoTexture; + + uint normalTexture; + + float metallicFactor; + float roughnessFactor; + uint metallicRoughnessTexture; +}; + +struct Vertex { + vec4 position; + vec4 normal; + vec2 uv[2]; + vec4 tangent; + vec4 bitangent; // No longer needed. // TODO remove +}; + +struct Light { + vec3 color; + uint intensity; +}; + +struct Scene { + uint lightsCount; +}; + +struct Mesh { + uint asd; +}; + +layout( push_constant ) uniform constants +{ + mat4 render_matrix; + uint indexBufferIndex; + uint vertexBufferIndex; + uint materialBufferIndex; +} PushConstants; + +layout(set = 1, binding = 0) uniform CameraParam { + mat4 projection; + mat4 view; +} Camera; + +layout(set = 2, binding = 0) buffer MeshBuffer { + layout(align = 16) Mesh mesh; +} MeshBuffers[]; + +layout(set = 2, binding = 1) buffer VertexBuffer { + layout(align = 16) Vertex vertices[]; +} VertexBuffers[]; + +layout(set = 2, binding = 2) buffer IndexBuffer { + uint indices[]; +} IndexBuffers[]; + +layout(set = 2, binding = 4) uniform sampler2D texSampler[]; + +layout(location = 0) out vec2 uv; +layout(location = 1) smooth out mat3 TBN; + +layout(location = 4) out vec4 outpos; + +void main() +{ + uint index = IndexBuffers[ PushConstants.indexBufferIndex ].indices[gl_VertexIndex]; + Vertex vertex = VertexBuffers[ PushConstants.vertexBufferIndex ].vertices[ index ]; + + uv = vertex.uv[0]; + + mat3 model = transpose(inverse(mat3(PushConstants.render_matrix))); + + vec3 T = normalize(model * vec3(vertex.tangent)); + vec3 N = normalize(model * vec3(vertex.normal)); + vec3 B = normalize(model * vec3(vertex.bitangent)); + /* T = normalize(T - dot(T, N) * N); // Re-orthoganize T with respect to N */ + /* vec3 B = normalize(cross(N, T)); */ + TBN = mat3(T, B, N); + + gl_Position = Camera.projection * Camera.view * PushConstants.render_matrix * vec4(vertex.position.xyz, 1.0); + outpos = PushConstants.render_matrix * vec4(vertex.position.xyz, 1.0); +}