Shader improvements;

Mostly renaming things and adding helper functions.
This commit is contained in:
bjorn 2022-07-06 22:54:56 -07:00
parent f9c07f852b
commit 3bf5ec5744
9 changed files with 76 additions and 58 deletions

View File

@ -5,9 +5,8 @@
#include "lovr.glsl"
layout(set = 1, binding = 1) uniform textureCube SkyboxTexture;
layout(location = 2) in vec3 FragDirection;
layout(location = 0) in vec3 Direction;
void main() {
PixelColors[0] = FragColor * texture(samplerCube(SkyboxTexture, Sampler), FragDirection);
PixelColors[0] = Color * getPixel(SkyboxTexture, Direction);
}

View File

@ -4,10 +4,10 @@
#include "lovr.glsl"
layout(location = 2) out vec3 FragDirection;
layout(location = 0) out vec3 Direction;
void main() {
FragColor = VertexColor * Color;
Color = PassColor * VertexColor;
const vec2 uvs[6] = vec2[6](
vec2(-1, -1),
@ -21,6 +21,6 @@ void main() {
vec2 uv = uvs[VertexIndex % 6];
vec3 ray = vec3(uv, -1.);
mat3 inverseViewOrientation = transpose(mat3(View));
FragDirection = normalize(inverseViewOrientation * (InverseProjection * vec4(ray, 1.)).xyz);
Direction = normalize(inverseViewOrientation * (InverseProjection * vec4(ray, 1.)).xyz);
Position = vec4(uv, 1, 1);
}

View File

@ -6,12 +6,12 @@
#include "lovr.glsl"
layout(location = 2) in vec3 FragDirection;
layout(location = 0) in vec3 Direction;
void main() {
vec3 dir = normalize(FragDirection);
vec3 dir = normalize(Direction);
float phi = acos(dir.y);
float theta = atan(dir.x, -dir.z);
vec2 uv = vec2(.5 + theta / (2 * PI), phi / PI);
PixelColors[0] = FragColor * texture(sampler2D(Texture, Sampler), uv);
PixelColors[0] = Color * getPixel(ColorTexture, uv);
}

View File

@ -5,10 +5,10 @@
#include "lovr.glsl"
void main() {
FragColor = VertexColor * Color;
Color = PassColor * VertexColor;
float x = -1 + float((VertexIndex & 1) << 2);
float y = -1 + float((VertexIndex & 2) << 1);
FragUV = vec2(x, y) * .5 + .5;
UV = vec2(x, y) * .5 + .5;
Position = vec4(x, y, 0., 1.);
PointSize = 1.f;
}

View File

@ -6,8 +6,8 @@
#include "lovr.glsl"
float screenPxRange() {
vec2 screenTexSize = vec2(1.) / fwidth(FragUV);
return max(.5 * dot(Material.sdfRange, screenTexSize), 1.);
vec2 screenTexSize = vec2(1.) / fwidth(UV);
return max(.5 * dot(SDFRange, screenTexSize), 1.);
}
float median(float r, float g, float b) {
@ -15,10 +15,10 @@ float median(float r, float g, float b) {
}
void main() {
vec3 msdf = texture(sampler2D(Texture, Sampler), FragUV).rgb;
vec3 msdf = getPixel(ColorTexture).rgb;
float sdf = median(msdf.r, msdf.g, msdf.b);
float screenPxDistance = screenPxRange() * (sdf - .5);
float alpha = clamp(screenPxDistance + .5, 0., 1.);
if (alpha <= 0.) discard;
PixelColors[0] = vec4(FragColor.rgb, FragColor.a * alpha);
PixelColors[0] = vec4(Color.rgb, Color.a * alpha);
}

View File

@ -13,29 +13,28 @@ struct Draw {
vec4 color;
};
layout(set = 0, binding = 0) uniform Cameras { Camera cameras[6]; };
layout(set = 0, binding = 1) uniform Draws { Draw draws[256]; };
layout(set = 0, binding = 0) uniform CameraBuffer { Camera Cameras[6]; };
layout(set = 0, binding = 1) uniform DrawBuffer { Draw Draws[256]; };
layout(set = 0, binding = 2) uniform sampler Sampler;
struct MaterialData {
vec4 color;
vec4 glow;
vec2 uvShift;
vec2 uvScale;
vec2 sdfRange;
float metalness;
float roughness;
float clearcoat;
float clearcoatRoughness;
float occlusionStrength;
float glowStrength;
float normalScale;
float alphaCutoff;
float pointSize;
layout(set = 1, binding = 0) uniform MaterialBuffer {
vec4 MaterialColor;
vec4 GlowColor;
vec2 UVShift;
vec2 UVScale;
vec2 SDFRange;
float Metalness;
float Roughness;
float Clearcoat;
float ClearcoatRoughness;
float OcclusionStrength;
float GlowStrength;
float NormalScale;
float AlphaCutoff;
float PointSize;
};
layout(set = 1, binding = 0) uniform MaterialBlock { MaterialData Material; };
layout(set = 1, binding = 1) uniform texture2D Texture;
layout(set = 1, binding = 1) uniform texture2D ColorTexture;
layout(set = 1, binding = 2) uniform texture2D GlowTexture;
layout(set = 1, binding = 3) uniform texture2D OcclusionTexture;
layout(set = 1, binding = 4) uniform texture2D MetalnessTexture;
@ -60,13 +59,13 @@ layout(location = 0) out vec4 PixelColors[1];
// Varyings
#ifdef GL_VERTEX_SHADER
layout(location = 10) out vec3 FragNormal;
layout(location = 11) out vec4 FragColor;
layout(location = 12) out vec2 FragUV;
layout(location = 10) out vec3 Normal;
layout(location = 11) out vec4 Color;
layout(location = 12) out vec2 UV;
#else
layout(location = 10) in vec3 FragNormal;
layout(location = 11) in vec4 FragColor;
layout(location = 12) in vec2 FragUV;
layout(location = 10) in vec3 Normal;
layout(location = 11) in vec4 Color;
layout(location = 12) in vec2 UV;
#endif
// Macros
@ -94,13 +93,13 @@ layout(location = 12) in vec2 FragUV;
#define ViewIndex gl_ViewIndex
#define DrawId gl_BaseInstance
#define Projection cameras[ViewIndex].projection
#define View cameras[ViewIndex].view
#define ViewProjection cameras[ViewIndex].viewProjection
#define InverseProjection cameras[ViewIndex].inverseProjection
#define Transform draws[DrawId].transform
#define NormalMatrix mat3(draws[DrawId].normalMatrix)
#define Color draws[DrawId].color
#define Projection Cameras[ViewIndex].projection
#define View Cameras[ViewIndex].view
#define ViewProjection Cameras[ViewIndex].viewProjection
#define InverseProjection Cameras[ViewIndex].inverseProjection
#define Transform Draws[DrawId].transform
#define NormalMatrix mat3(Draws[DrawId].normalMatrix)
#define PassColor Draws[DrawId].color
#define ClipFromLocal (ViewProjection * Transform)
#define ClipFromWorld (ViewProjection)
@ -110,5 +109,13 @@ layout(location = 12) in vec2 FragUV;
#define WorldFromLocal (Transform)
#define DefaultPosition (ClipFromLocal * VertexPosition)
#define DefaultColor (FragColor * texture(sampler2D(Texture, Sampler), FragUV) * Material.color)
#define DefaultColor (Color * MaterialColor * getPixel(ColorTexture))
#endif
#ifdef GL_FRAGMENT_SHADER
vec4 getPixel(texture2D t) { return texture(sampler2D(t, Sampler), UV); }
vec4 getPixel(texture2D t, vec2 uv) { return texture(sampler2D(t, Sampler), uv); }
vec4 getPixel(texture3D t, vec3 uvw) { return texture(sampler3D(t, Sampler), uvw); }
vec4 getPixel(textureCube t, vec3 dir) { return texture(samplerCube(t, Sampler), dir); }
vec4 getPixel(texture2DArray t, vec3 uvw) { return texture(sampler2DArray(t, Sampler), uvw); }
#endif

View File

@ -5,5 +5,5 @@
#include "lovr.glsl"
void main() {
PixelColors[0] = FragColor * texture(sampler2D(Texture, Sampler), FragUV) * Material.color;
PixelColors[0] = Color * MaterialColor * getPixel(ColorTexture, UV);
}

View File

@ -5,9 +5,9 @@
#include "lovr.glsl"
void main() {
FragColor = VertexColor * Color;
FragNormal = normalize(NormalMatrix * VertexNormal);
FragUV = VertexUV;
Color = PassColor * VertexColor;
Normal = normalize(NormalMatrix * VertexNormal);
UV = VertexUV;
Position = DefaultPosition;
PointSize = 1.f;
}

View File

@ -1295,15 +1295,27 @@ Blob* lovrGraphicsCompileShader(ShaderStage stage, Blob* source) {
[STAGE_COMPUTE] = GLSLANG_STAGE_COMPUTE
};
const char* stageNames[] = {
[STAGE_VERTEX] = "vertex",
[STAGE_FRAGMENT] = "fragment",
[STAGE_COMPUTE] = "compute"
};
const char* prefix = ""
"#version 460\n"
"#extension GL_EXT_multiview : require\n"
"#extension GL_GOOGLE_include_directive : require\n";
const char* suffixes[] = {
[STAGE_VERTEX] = "void main() { FragColor = Color * VertexColor, FragUV = VertexUV, Position = lovrmain(); }",
[STAGE_FRAGMENT] = "void main() { PixelColors[0] = lovrmain(); }",
[STAGE_COMPUTE] = "void main() { lovrmain(); }"
[STAGE_VERTEX] = ""
"void main() {"
"Color = PassColor * VertexColor;"
"Normal = normalize(NormalMatrix * VertexNormal);"
"UV = VertexUV;"
"Position = lovrmain();"
"}\n#line 0\n",
[STAGE_FRAGMENT] = "void main() { PixelColors[0] = lovrmain(); }\n#line 0\n",
[STAGE_COMPUTE] = "void main() { lovrmain(); }\n#line 0\n"
};
const char* strings[] = {
@ -1340,12 +1352,12 @@ Blob* lovrGraphicsCompileShader(ShaderStage stage, Blob* source) {
glslang_shader_t* shader = glslang_shader_create(&input);
if (!glslang_shader_preprocess(shader, &input)) {
lovrLog(LOG_INFO, "Could not preprocess shader: %s", glslang_shader_get_info_log(shader));
lovrLog(LOG_INFO, "GFX", "Could not preprocess %s shader:\n%s", stageNames[stage], glslang_shader_get_info_log(shader));
return NULL;
}
if (!glslang_shader_parse(shader, &input)) {
lovrLog(LOG_INFO, "Could not parse shader: %s", glslang_shader_get_info_log(shader));
lovrLog(LOG_INFO, "GFX", "Could not parse %s shader:\n%s", stageNames[stage], glslang_shader_get_info_log(shader));
return NULL;
}
@ -1353,7 +1365,7 @@ Blob* lovrGraphicsCompileShader(ShaderStage stage, Blob* source) {
glslang_program_add_shader(program, shader);
if (!glslang_program_link(program, 0)) {
lovrLog(LOG_INFO, "Could not link shader: %s", glslang_program_get_info_log(program));
lovrLog(LOG_INFO, "GFX", "Could not link shader:\n%s", glslang_program_get_info_log(program));
return NULL;
}