lovr/src/resources/shaders.c

182 lines
5.6 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
2018-02-12 03:16:40 +00:00
const char* lovrShaderScalarUniforms[] = {
"lovrMetalness",
"lovrRoughness"
};
2017-10-21 21:32:41 +00:00
const char* lovrShaderColorUniforms[] = {
2018-02-12 03:16:40 +00:00
"lovrDiffuseColor",
"lovrEmissiveColor"
2017-10-21 21:32:41 +00:00
};
const char* lovrShaderTextureUniforms[] = {
2017-10-21 21:59:34 +00:00
"lovrDiffuseTexture",
2018-02-12 03:16:40 +00:00
"lovrEmissiveTexture",
"lovrMetalnessTexture",
"lovrRoughnessTexture",
"lovrOcclusionTexture",
"lovrNormalTexture",
2017-10-21 21:59:34 +00:00
"lovrEnvironmentTexture"
2017-10-21 21:32:41 +00:00
};
2017-10-21 20:14:35 +00:00
const char* lovrShaderVertexPrefix = ""
#ifdef EMSCRIPTEN
"#version 300 es \n"
"precision mediump float; \n"
2018-08-31 04:37:45 +00:00
"precision mediump int; \n"
2017-10-21 20:14:35 +00:00
#else
"#version 150 \n"
2018-08-30 07:52:38 +00:00
"#extension GL_AMD_vertex_shader_viewport_index : enable \n"
2017-10-21 20:14:35 +00:00
#endif
"#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 lovrTransform lovrTransforms[lovrViewportIndex] \n"
"#define lovrNormalMatrix lovrNormalMatrices[lovrViewportIndex] \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"
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-08-30 10:38:31 +00:00
"#ifdef GL_AMD_vertex_shader_viewport_index \n"
"#define lovrViewportIndex gl_ViewportIndex \n"
"#else \n"
"uniform int lovrViewportIndex; \n"
"#endif \n"
"uniform int lovrViewportCount; \n"
2017-10-21 20:14:35 +00:00
"uniform mat4 lovrModel; \n"
"uniform mat4 lovrViews[2]; \n"
"uniform mat4 lovrProjections[2]; \n"
"uniform mat4 lovrTransforms[2]; \n"
"uniform mat3 lovrNormalMatrices[2]; \n"
"uniform mat3 lovrMaterialTransform; \n"
"uniform float lovrPointSize; \n"
2017-11-21 02:12:08 +00:00
"uniform mat4 lovrPose[MAX_BONES]; \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"
" mat4 pose = \n"
" lovrPose[lovrBones[0]] * lovrBoneWeights[0] + \n"
" lovrPose[lovrBones[1]] * lovrBoneWeights[1] + \n"
" lovrPose[lovrBones[2]] * lovrBoneWeights[2] + \n"
" lovrPose[lovrBones[3]] * lovrBoneWeights[3]; \n"
" gl_PointSize = lovrPointSize; \n"
2018-08-30 07:52:38 +00:00
"#ifdef GL_AMD_vertex_shader_viewport_index \n"
" lovrViewportIndex = gl_InstanceID % lovrViewportCount; \n"
"#endif \n"
" gl_Position = position(lovrProjection, lovrTransform, pose * vec4(lovrPosition, 1.0)); \n"
"}";
2017-10-21 20:14:35 +00:00
const char* lovrShaderFragmentPrefix = ""
#ifdef EMSCRIPTEN
"#version 300 es \n"
"precision mediump float; \n"
2018-08-31 04:37:45 +00:00
"precision mediump int; \n"
2017-10-21 20:14:35 +00:00
#else
"#version 150 \n"
2018-08-31 13:03:35 +00:00
"#extension GL_ARB_fragment_layer_viewport : enable \n"
2018-08-30 10:43:41 +00:00
"in vec4 gl_FragCoord; \n"
2017-10-21 20:14:35 +00:00
#endif
"#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"
"out vec4 lovrCanvas[gl_MaxDrawBuffers]; \n"
2018-08-31 13:03:35 +00:00
"#ifdef GL_ARB_fragment_layer_viewport \n"
2018-08-30 10:38:31 +00:00
"#define lovrViewportIndex gl_ViewportIndex \n"
"#else \n"
"uniform int lovrViewportIndex; \n"
"#endif \n"
"uniform int lovrViewportCount; \n"
2018-02-12 03:16:40 +00:00
"uniform float lovrMetalness; \n"
"uniform float lovrRoughness; \n"
2017-10-21 20:14:35 +00:00
"uniform vec4 lovrColor; \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"
"#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-08-30 10:38:31 +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-08-30 10:38:31 +00:00
" return graphicsColor * texture(lovrEnvironmentTexture, texturePosition[lovrViewportIndex]); \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"
" float theta = acos(direction.y / length(direction)); \n"
2018-08-30 07:52:38 +00:00
" 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"
" 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"
"}";