SO3Engine
OgreUnifiedShader.h
Go to the documentation of this file.
1// This file is part of the OGRE project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at https://www.ogre3d.org/licensing.
4
5// greatly inspired by
6// - shiny: https://ogrecave.github.io/shiny/defining-materials-shaders.html
7// - bgfx: https://github.com/bkaradzic/bgfx/blob/master/src/bgfx_shader.sh
8
10// MAIN_PARAMETERS
11// IN(vec4 vertex, POSITION)
12// MAIN_DECLARATION
13// {
14// GLSL code here
15// }
16
18// use macros that will be default with Ogre 14
19// #define USE_OGRE_FROM_FUTURE
20
21// @public-api
22
23#if defined(OGRE_HLSL) || defined(OGRE_CG)
24// HLSL
25#include "HLSL_SM4Support.hlsl"
26#define vec2 float2
27#define vec3 float3
28#define vec4 float4
29#define mat2 float2x2
30#define mat3 float3x3
31#define mat4 float4x4
32
33#define ivec2 int2
34#define ivec3 int3
35#define ivec4 int4
36
37#define texture1D tex1D
38#define texture2D tex2D
39#define texture3D tex3D
40#define texture2DArray tex2DARRAY
41#define textureCube texCUBE
42#define shadow2D tex2Dcmp
43#define texture2DProj tex2Dproj
44vec4 texture2DLod(sampler2D s, vec2 v, float lod) { return tex2Dlod(s, vec4(v.x, v.y, 0, lod)); }
45
46#define samplerCube samplerCUBE
47#define sampler2DShadow Sampler2DShadow
48
49#define mix lerp
50#define fract frac
51#define inversesqrt rsqrt
52#define mod(x, y) (x - y * floor(x / y))
53
54vec2 vec2_splat(float x) { return vec2(x, x); }
55vec3 vec3_splat(float x) { return vec3(x, x, x); }
56vec4 vec4_splat(float x) { return vec4(x, x, x, x); }
57
58mat4 mtxFromRows(vec4 a, vec4 b, vec4 c, vec4 d)
59{
60 return mat4(a, b, c, d);
61}
62
63mat3 mtxFromRows(vec3 a, vec3 b, vec3 c)
64{
65 return mat3(a, b, c);
66}
67
68mat3 mtxFromCols(vec3 a, vec3 b, vec3 c)
69{
70 return transpose(mat3(a, b, c));
71}
72
73mat2 transposeM(mat2 m)
74{
75 return transpose(m);
76}
77
78mat3 transposeM(mat3 m)
79{
80 return transpose(m);
81}
82
83mat4 transposeM(mat4 m)
84{
85 return transpose(m);
86}
87
88#define STATIC static
89
90#define OGRE_UNIFORMS_BEGIN
91#define OGRE_UNIFORMS_END
92
93#define MAIN_PARAMETERS void main(
94
95#ifdef OGRE_VERTEX_SHADER
96#define MAIN_DECLARATION out float4 gl_Position : POSITION)
97#else
98#define MAIN_DECLARATION in float4 gl_FragCoord : POSITION, out float4 gl_FragColor : COLOR)
99#endif
100
101#define IN(decl, sem) in decl : sem,
102#define OUT(decl, sem) out decl : sem,
103#elif defined(OGRE_METAL)
104#define vec2 float2
105#define vec3 float3
106#define vec4 float4
107#define mat2 metal::float2x2
108#define mat3 metal::float3x3
109#define mat4 metal::float4x4
110
111#define IN(decl, sem) decl [[ attribute(sem) ]];
112#else
113// GLSL
114#include "GLSL_GL3Support.glsl"
115
116#ifndef USE_OGRE_FROM_FUTURE
117#define _UNIFORM_BINDING(b)
118#elif defined(VULKAN)
119#define _UNIFORM_BINDING(b) layout(binding = b + 2) uniform
120#elif __VERSION__ >= 420
121#define _UNIFORM_BINDING(b) layout(binding = b) uniform
122#else
123#define _UNIFORM_BINDING(b) uniform
124#endif
125
126#define SAMPLER1D(name, reg) _UNIFORM_BINDING(reg) sampler1D name
127#define SAMPLER2D(name, reg) _UNIFORM_BINDING(reg) sampler2D name
128#define SAMPLER3D(name, reg) _UNIFORM_BINDING(reg) sampler3D name
129#define SAMPLER2DARRAY(name, reg) _UNIFORM_BINDING(reg) sampler2DArray name
130#define SAMPLERCUBE(name, reg) _UNIFORM_BINDING(reg) samplerCube name
131#define SAMPLER2DSHADOW(name, reg) _UNIFORM_BINDING(reg) sampler2DShadow name
132
133#define saturate(x) clamp(x, 0.0, 1.0)
134#define mul(a, b) ((a) * (b))
135
136#define vec2_splat vec2
137#define vec3_splat vec3
138#define vec4_splat vec4
139
140mat2 transposeM(mat2 m) {
141 return mat2(m[0][0], m[1][0],
142 m[0][1], m[1][1]);
143}
144
145mat3 transposeM(mat3 m) {
146 return mat3(m[0][0], m[1][0], m[2][0],
147 m[0][1], m[1][1], m[2][1],
148 m[0][2], m[1][2], m[2][2]);
149}
150
151mat4 transposeM(mat4 m) {
152 return mat4(m[0][0], m[1][0], m[2][0], m[3][0],
153 m[0][1], m[1][1], m[2][1], m[3][1],
154 m[0][2], m[1][2], m[2][2], m[3][2],
155 m[0][3], m[1][3], m[2][3], m[3][3]);
156}
157
158mat4 mtxFromRows(vec4 a, vec4 b, vec4 c, vec4 d)
159{
160 return transposeM(mat4(a, b, c, d));
161}
162
163mat3 mtxFromRows(vec3 a, vec3 b, vec3 c)
164{
165 return transposeM(mat3(a, b, c));
166}
167
168mat3 mtxFromCols(vec3 a, vec3 b, vec3 c)
169{
170 return mat3(a, b, c);
171}
172
173#define STATIC
174
175#define MAIN_PARAMETERS
176#define MAIN_DECLARATION void main()
177
178#endif
179
180#if !defined(OGRE_HLSL) && !defined(OGRE_CG)
181// semantics as aliases for attribute locations
182#define POSITION 0
183#define BLENDWEIGHT 1
184#define NORMAL 2
185#define COLOR0 3
186#define COLOR1 4
187#define COLOR COLOR0
188#define FOG 5
189#define BLENDINDICES 7
190#define TEXCOORD0 8
191#define TEXCOORD1 9
192#define TEXCOORD2 10
193#define TEXCOORD3 11
194#define TEXCOORD4 12
195#define TEXCOORD5 13
196#define TEXCOORD6 14
197#define TEXCOORD7 15
198#define TANGENT 14
199#endif
200
201#define OGRE_UNIFORMS(params) OGRE_UNIFORMS_BEGIN params OGRE_UNIFORMS_END
mat2 transposeM(mat2 m)
vec4 texture2DLod(sampler2D s, vec2 v, float lod)
vec4 vec4_splat(float x)
mat3 mtxFromCols(vec3 a, vec3 b, vec3 c)
vec3 vec3_splat(float x)
mat4 mtxFromRows(vec4 a, vec4 b, vec4 c, vec4 d)
vec2 vec2_splat(float x)