From 6ed208747005774f3fbc90256b9da0c6890a0814 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 27 Apr 2023 18:36:45 +0100 Subject: Lookup material details once in the vertex shader ... instead of per texel in the fragment shader --- gfx/gl/shaders/commonPoint.glsl | 17 ++++++++++++++++- gfx/gl/shaders/dynamicPoint.vs | 1 + gfx/gl/shaders/dynamicPointInst.vs | 1 + gfx/gl/shaders/fixedPoint.vs | 1 + gfx/gl/shaders/material.fs | 15 +++++---------- gfx/gl/shaders/materialInterface.glsl | 8 +++++++- 6 files changed, 31 insertions(+), 12 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/shaders/commonPoint.glsl b/gfx/gl/shaders/commonPoint.glsl index ecf5fc9..35510e1 100644 --- a/gfx/gl/shaders/commonPoint.glsl +++ b/gfx/gl/shaders/commonPoint.glsl @@ -1,3 +1,18 @@ +layout(binding = 1) uniform usampler2DRect materialData; + +MaterialDetail +getMaterialDetail(uint midx) +{ + if (midx > 0u) { + const vec4 sPosSize = texture(materialData, uvec2(0, midx - 1u)); + const uvec4 sMode = texture(materialData, uvec2(1, midx - 1u)); + const uint mapmodeU = sMode.x & 0xFu; + const uint mapmodeV = (sMode.x & 0xF0u) >> 1; + return MaterialDetail(sPosSize.xy, sPosSize.zw, uvec2(mapmodeU, mapmodeV)); + } + return MaterialDetail(vec2(0, 0), vec2(0, 0), uvec2(0, 0)); +} + void main() { @@ -7,7 +22,7 @@ main() TexCoords = texCoord; Normal = (model * vec4(normal, 0.0)).xyz; Colour = colour; - Material = material; + Material = getMaterialDetail(material); gl_Position = viewProjection * worldPos; } diff --git a/gfx/gl/shaders/dynamicPoint.vs b/gfx/gl/shaders/dynamicPoint.vs index ffa8275..961535c 100644 --- a/gfx/gl/shaders/dynamicPoint.vs +++ b/gfx/gl/shaders/dynamicPoint.vs @@ -1,4 +1,5 @@ #version 330 core +#extension GL_ARB_shading_language_420pack : enable include(`meshIn.glsl') include(`materialInterface.glsl') diff --git a/gfx/gl/shaders/dynamicPointInst.vs b/gfx/gl/shaders/dynamicPointInst.vs index e4c8526..2d6cee5 100644 --- a/gfx/gl/shaders/dynamicPointInst.vs +++ b/gfx/gl/shaders/dynamicPointInst.vs @@ -1,4 +1,5 @@ #version 330 core +#extension GL_ARB_shading_language_420pack : enable include(`meshIn.glsl') include(`materialInterface.glsl') diff --git a/gfx/gl/shaders/fixedPoint.vs b/gfx/gl/shaders/fixedPoint.vs index 6f517ca..ed78c96 100644 --- a/gfx/gl/shaders/fixedPoint.vs +++ b/gfx/gl/shaders/fixedPoint.vs @@ -1,4 +1,5 @@ #version 330 core +#extension GL_ARB_shading_language_420pack : enable include(`meshIn.glsl') include(`materialInterface.glsl') diff --git a/gfx/gl/shaders/material.fs b/gfx/gl/shaders/material.fs index cafb35d..20fa8ab 100644 --- a/gfx/gl/shaders/material.fs +++ b/gfx/gl/shaders/material.fs @@ -5,7 +5,6 @@ include(`materialInterface.glsl') include(`materialOut.glsl') layout(binding = 0) uniform sampler2D texture0; -layout(binding = 1) uniform usampler2DRect material; float map(uint mapmode, float value) { @@ -24,20 +23,16 @@ float map(uint mapmode, float value) return 0; } -vec2 map(uint mapmodeU, uint mapmodeV, vec2 value) +vec2 map(uvec2 mapmode, vec2 value) { - return vec2(map(mapmodeU, value.x), map(mapmodeV, value.y)); + return vec2(map(mapmode.x, value.x), map(mapmode.y, value.y)); } -vec4 getTextureColour(uint midx, vec2 uv) +vec4 getTextureColour(MaterialDetail mat, vec2 uv) { - if (midx > 0u) { + if (mat.textureSize.x > 0) { const vec2 tSize = textureSize(texture0, 0); - const vec4 sPosSize = texture(material, uvec2(0, midx - 1u)); - const uvec4 sMode = texture(material, uvec2(1, midx - 1u)); - const uint mapmodeU = sMode.x & 0xFu; - const uint mapmodeV = (sMode.x & 0xF0u) >> 1; - uv = (sPosSize.xy + sPosSize.zw * map(mapmodeU, mapmodeV, uv)) / tSize; + uv = (mat.textureOrigin + mat.textureSize * map(mat.mapmode, uv)) / tSize; } return texture(texture0, uv); } diff --git a/gfx/gl/shaders/materialInterface.glsl b/gfx/gl/shaders/materialInterface.glsl index 5735cf9..3a4796b 100644 --- a/gfx/gl/shaders/materialInterface.glsl +++ b/gfx/gl/shaders/materialInterface.glsl @@ -1,7 +1,13 @@ +struct MaterialDetail { + vec2 textureOrigin; + vec2 textureSize; + uvec2 mapmode; +}; + ifelse(TYPE, .fs, in, out) vec3 FragPos; ifelse(TYPE, .fs, in, out) vec2 TexCoords; ifelse(TYPE, .fs, in, out) vec3 Normal; ifelse(TYPE, .fs, in, out) vec4 Colour; flat ifelse(TYPE, .fs, in, out) -uint Material; +MaterialDetail Material; -- cgit v1.2.3