lovr/src/resources/shaders.c

176 lines
5.4 KiB
C
Raw Normal View History

2017-12-10 20:31:50 +00:00
#include "resources/shaders.h"
2017-10-21 20:14:35 +00:00
const char* lovrShaderVertexPrefix = ""
"#define VERTEX VERTEX \n"
2017-11-25 20:29:40 +00:00
"#define MAX_BONES 48 \n"
2018-08-30 10:38:31 +00:00
"#define lovrView lovrViews[lovrViewportIndex] \n"
"#define lovrProjection lovrProjections[lovrViewportIndex] \n"
"#define lovrModel lovrDraws[lovrDrawID].model \n"
2018-12-08 02:19:03 +00:00
"#define lovrTransform (lovrView * lovrModel) \n"
"#define lovrNormalMatrix mat3(transpose(inverse(lovrTransform))) \n"
2018-09-01 09:52:02 +00:00
"#define lovrInstanceID (gl_InstanceID / lovrViewportCount) \n"
"#define lovrPoseMatrix ("
"lovrPose[lovrBones[0]] * lovrBoneWeights[0] +"
"lovrPose[lovrBones[1]] * lovrBoneWeights[1] +"
"lovrPose[lovrBones[2]] * lovrBoneWeights[2] +"
"lovrPose[lovrBones[3]] * lovrBoneWeights[3]"
") \n"
2017-10-21 20:14:35 +00:00
"in vec3 lovrPosition; \n"
"in vec3 lovrNormal; \n"
"in vec2 lovrTexCoord; \n"
2017-10-23 08:33:49 +00:00
"in vec4 lovrVertexColor; \n"
2018-02-26 10:54:35 +00:00
"in vec3 lovrTangent; \n"
"in ivec4 lovrBones; \n"
"in vec4 lovrBoneWeights; \n"
2019-02-08 18:32:05 +00:00
"in uint lovrDrawID; \n"
2017-10-21 20:14:35 +00:00
"out vec2 texCoord; \n"
2017-10-23 08:33:49 +00:00
"out vec4 vertexColor; \n"
2018-12-11 05:30:55 +00:00
"out vec4 lovrColor; \n"
"struct DrawData { \n"
" mat4 model; \n"
" vec4 color; \n"
"}; \n"
2018-12-08 02:19:03 +00:00
"uniform lovrDrawData { \n"
" DrawData lovrDraws[MAX_DRAWS]; \n"
2018-12-14 23:48:25 +00:00
"}; \n"
"uniform mat4 lovrViews[2]; \n"
"uniform mat4 lovrProjections[2]; \n"
"uniform mat3 lovrMaterialTransform; \n"
"uniform float lovrPointSize; \n"
2017-11-21 02:12:08 +00:00
"uniform mat4 lovrPose[MAX_BONES]; \n"
2018-08-31 23:45:05 +00:00
"uniform int lovrViewportCount; \n"
"#if SINGLEPASS \n"
2018-08-31 23:45:05 +00:00
"#define lovrViewportIndex gl_ViewportIndex \n"
"#else \n"
"uniform int lovrViewportIndex; \n"
"#endif \n"
"#line 0 \n";
2017-10-21 20:14:35 +00:00
const char* lovrShaderVertexSuffix = ""
"void main() { \n"
" texCoord = (lovrMaterialTransform * vec3(lovrTexCoord, 1.)).xy; \n"
" vertexColor = lovrVertexColor; \n"
" lovrColor = lovrDraws[lovrDrawID].color; \n"
"#if SINGLEPASS \n"
2018-09-01 09:52:02 +00:00
" gl_ViewportIndex = gl_InstanceID % lovrViewportCount; \n"
"#endif \n"
2018-09-01 09:52:02 +00:00
" gl_PointSize = lovrPointSize; \n"
" gl_Position = position(lovrProjection, lovrTransform, vec4(lovrPosition, 1.0)); \n"
"}";
const char* lovrShaderFragmentPrefix = ""
"#define PIXEL PIXEL \n"
"#define FRAGMENT FRAGMENT \n"
2017-10-21 20:14:35 +00:00
"in vec2 texCoord; \n"
2017-10-23 08:33:49 +00:00
"in vec4 vertexColor; \n"
2018-12-11 05:30:55 +00:00
"in vec4 lovrColor; \n"
"out vec4 lovrCanvas[gl_MaxDrawBuffers]; \n"
2018-02-12 03:16:40 +00:00
"uniform float lovrMetalness; \n"
"uniform float lovrRoughness; \n"
2017-10-21 21:32:41 +00:00
"uniform vec4 lovrDiffuseColor; \n"
2018-02-12 03:16:40 +00:00
"uniform vec4 lovrEmissiveColor; \n"
2017-10-21 21:59:34 +00:00
"uniform sampler2D lovrDiffuseTexture; \n"
2018-02-12 03:16:40 +00:00
"uniform sampler2D lovrEmissiveTexture; \n"
"uniform sampler2D lovrMetalnessTexture; \n"
"uniform sampler2D lovrRoughnessTexture; \n"
"uniform sampler2D lovrOcclusionTexture; \n"
"uniform sampler2D lovrNormalTexture; \n"
"uniform samplerCube lovrEnvironmentTexture; \n"
2018-08-31 23:45:05 +00:00
"uniform int lovrViewportCount; \n"
"#if SINGLEPASS \n"
2018-08-31 23:45:05 +00:00
"#define lovrViewportIndex gl_ViewportIndex \n"
"#else \n"
"uniform int lovrViewportIndex; \n"
"#endif \n"
"#line 0 \n";
2017-10-21 20:14:35 +00:00
const char* lovrShaderFragmentSuffix = ""
"void main() { \n"
"#ifdef MULTICANVAS \n"
" colors(lovrColor, lovrDiffuseTexture, texCoord); \n"
"#else \n"
" lovrCanvas[0] = color(lovrColor, lovrDiffuseTexture, texCoord); \n"
"#endif \n"
2017-10-21 20:14:35 +00:00
"}";
2018-08-09 01:05:29 +00:00
const char* lovrShaderComputePrefix = ""
"#version 430 \n"
2018-08-09 01:09:54 +00:00
"#line 0 \n";
2018-08-09 01:05:29 +00:00
2018-08-07 23:58:14 +00:00
const char* lovrShaderComputeSuffix = ""
"void main() { \n"
" compute(); \n"
"}";
2017-10-21 20:14:35 +00:00
const char* lovrDefaultVertexShader = ""
"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n"
" return projection * transform * vertex; \n"
"}";
const char* lovrDefaultFragmentShader = ""
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
2017-10-23 08:33:49 +00:00
" return graphicsColor * lovrDiffuseColor * vertexColor * texture(image, uv); \n"
2017-10-21 20:14:35 +00:00
"}";
const char* lovrCubeVertexShader = ""
"out vec3 texturePosition[2]; \n"
2017-10-21 20:14:35 +00:00
"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n"
2018-09-04 03:59:12 +00:00
" texturePosition[lovrViewportIndex] = inverse(mat3(transform)) * (inverse(projection) * vertex).xyz; \n"
2018-03-05 08:10:10 +00:00
" return vertex; \n"
2017-10-21 20:14:35 +00:00
"}";
const char* lovrCubeFragmentShader = ""
"in vec3 texturePosition[2]; \n"
2017-10-21 20:14:35 +00:00
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
2018-09-04 13:42:04 +00:00
" return graphicsColor * texture(lovrEnvironmentTexture, texturePosition[lovrViewportIndex] * vec3(-1, 1, 1)); \n"
2017-10-21 20:14:35 +00:00
"}";
2018-03-05 10:02:25 +00:00
const char* lovrPanoFragmentShader = ""
"in vec3 texturePosition[2]; \n"
2018-03-05 10:02:25 +00:00
"#define PI 3.141592653589 \n"
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
2018-08-30 10:38:31 +00:00
" vec3 direction = texturePosition[lovrViewportIndex]; \n"
2018-09-04 03:59:12 +00:00
" float theta = acos(-direction.y / length(direction)); \n"
" float phi = atan(direction.x, -direction.z); \n"
2018-06-10 06:17:46 +00:00
" uv = vec2(.5 + phi / (2. * PI), theta / PI); \n"
2018-03-05 10:02:25 +00:00
" return graphicsColor * texture(lovrDiffuseTexture, uv); \n"
"}";
2017-10-21 20:14:35 +00:00
const char* lovrFontFragmentShader = ""
"float median(float r, float g, float b) { \n"
" return max(min(r, g), min(max(r, g), b)); \n"
"} \n"
"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n"
" vec3 col = texture(image, uv).rgb; \n"
" float sdf = median(col.r, col.g, col.b); \n"
" float w = fwidth(sdf); \n"
" float alpha = smoothstep(.5 - w, .5 + w, sdf); \n"
" if (alpha <= 0.0) discard; \n"
2017-10-21 20:14:35 +00:00
" return vec4(graphicsColor.rgb, graphicsColor.a * alpha); \n"
"}";
2018-03-19 19:12:34 +00:00
const char* lovrFillVertexShader = ""
2017-10-21 20:14:35 +00:00
"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n"
" return vertex; \n"
"}";
2018-09-01 08:57:38 +00:00
const char* lovrShaderScalarUniforms[] = {
"lovrMetalness",
"lovrRoughness"
};
const char* lovrShaderColorUniforms[] = {
"lovrDiffuseColor",
"lovrEmissiveColor"
};
const char* lovrShaderTextureUniforms[] = {
"lovrDiffuseTexture",
"lovrEmissiveTexture",
"lovrMetalnessTexture",
"lovrRoughnessTexture",
"lovrOcclusionTexture",
"lovrNormalTexture",
"lovrEnvironmentTexture"
};