Change GLSL shaders to compile at #version 150 and add #extensions

* For some #extensions we can fallback to a lesser path (or just drop
  that functionality - e.g. displaying cubemap arrays if there's no
  extension for it).
This commit is contained in:
baldurk
2017-01-17 16:55:26 +00:00
parent 031ea659be
commit 21d6f41c97
12 changed files with 117 additions and 42 deletions
+6 -1
View File
@@ -61,6 +61,11 @@ struct Vec4u
// so it's added in code
//#version 430 core
// we require these extensions to be able to set explicit layout bindings, etc
//#extension GL_ARB_shading_language_420pack : require
//#extension GL_ARB_separate_shader_objects : require
//#extension GL_ARB_explicit_attrib_location : require
#ifdef VULKAN
#define BINDING(b) layout(set = 0, binding = b, std140)
@@ -103,7 +108,7 @@ BINDING(0) uniform MeshUBOData
mat4 mvp;
mat4 invProj;
vec4 color;
uint displayFormat;
int displayFormat;
uint homogenousInput;
vec2 pointSpriteSize;
uint rawoutput;
+28
View File
@@ -43,6 +43,10 @@ vec3 CalcCubeCoord(vec2 uv, int face)
return coord;
}
// enable these extensions if possible
//#extension GL_ARB_texture_cube_map_array : enable
//#extension GL_ARB_texture_multisample : enable
#if UINT_TEX
// these bindings are defined based on the RESTYPE_ defines in debuguniforms.h
@@ -56,7 +60,9 @@ layout(binding = 6) uniform usampler2DArray texUInt2DArray;
// cube array = 7
layout(binding = 8) uniform usampler2DRect texUInt2DRect;
layout(binding = 9) uniform usamplerBuffer texUIntBuffer;
#ifdef GL_ARB_texture_multisample
layout(binding = 10) uniform usampler2DMS texUInt2DMS;
#endif
vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int sampleIdx, vec3 texRes)
{
@@ -88,10 +94,14 @@ uvec4 SampleTextureUInt4(int type, vec2 pos, float slice, int mipLevel, int samp
}
else if(type == RESTYPE_TEX2DMS)
{
#ifdef GL_ARB_texture_multisample
if(sampleIdx < 0)
sampleIdx = 0;
col = texelFetch(texUInt2DMS, ivec2(pos * texRes.xy), sampleIdx);
#else
col = uvec4(0u, 0u, 0u, 0u);
#endif
}
else if(type == RESTYPE_TEX2DARRAY)
{
@@ -123,7 +133,9 @@ layout(binding = 6) uniform isampler2DArray texSInt2DArray;
// cube array = 7
layout(binding = 8) uniform isampler2DRect texSInt2DRect;
layout(binding = 9) uniform isamplerBuffer texSIntBuffer;
#ifdef GL_ARB_texture_multisample
layout(binding = 10) uniform isampler2DMS texSInt2DMS;
#endif
vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int sampleIdx, vec3 texRes)
{
@@ -160,10 +172,14 @@ ivec4 SampleTextureSInt4(int type, vec2 pos, float slice, int mipLevel, int samp
}
else if(type == RESTYPE_TEX2DMS)
{
#ifdef GL_ARB_texture_multisample
if(sampleIdx < 0)
sampleIdx = 0;
col = texelFetch(texSInt2DMS, ivec2(pos * texRes.xy), sampleIdx);
#else
col = ivec4(0, 0, 0, 0);
#endif
}
else if(type == RESTYPE_TEX2DARRAY)
{
@@ -187,10 +203,14 @@ layout(binding = 3) uniform sampler3D tex3D;
layout(binding = 4) uniform samplerCube texCube;
layout(binding = 5) uniform sampler1DArray tex1DArray;
layout(binding = 6) uniform sampler2DArray tex2DArray;
#ifdef GL_ARB_texture_cube_map_array
layout(binding = 7) uniform samplerCubeArray texCubeArray;
#endif
layout(binding = 8) uniform sampler2DRect tex2DRect;
layout(binding = 9) uniform samplerBuffer texBuffer;
#ifdef GL_ARB_texture_multisample
layout(binding = 10) uniform sampler2DMS tex2DMS;
#endif
vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int sampleIdx, vec3 texRes)
{
@@ -217,6 +237,7 @@ vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int samp
}
else if(type == RESTYPE_TEX2DMS)
{
#ifdef GL_ARB_texture_multisample
if(sampleIdx < 0)
{
int sampleCount = -sampleIdx;
@@ -271,6 +292,9 @@ vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int samp
{
col = texelFetch(tex2DMS, ivec2(pos * texRes.xy), sampleIdx);
}
#else
col = vec4(0.0f, 0.0f, 0.0f, 0.0f);
#endif
}
else if(type == RESTYPE_TEX2DARRAY)
{
@@ -288,10 +312,14 @@ vec4 SampleTextureFloat4(int type, vec2 pos, float slice, int mipLevel, int samp
}
else // type == RESTYPE_TEXCUBEARRAY
{
#ifdef GL_ARB_texture_cube_map_array
vec3 cubeCoord = CalcCubeCoord(pos, int(slice) % 6);
vec4 arrayCoord = vec4(cubeCoord, int(slice) / 6);
col = textureLod(texCubeArray, arrayCoord, float(mipLevel));
#else
col = vec4(0.0f, 0.0f, 0.0f, 0.0f);
#endif
}
return col;
+1 -1
View File
@@ -29,7 +29,7 @@ layout (location = 0) out vec4 color_out;
void main(void)
{
uint type = Mesh.displayFormat;
int type = Mesh.displayFormat;
if(type == MESHDISPLAY_SECONDARY)
{
+1 -1
View File
@@ -22,7 +22,7 @@
* THE SOFTWARE.
******************************************************************************/
layout(triangles, invocations = 1) in;
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
layout (location = 0) in vec4 IN_secondary[3];
+1 -1
View File
@@ -45,7 +45,7 @@ void main(void)
};
vec4 pos = position;
if(Mesh.homogenousInput == 0)
if(Mesh.homogenousInput == 0u)
{
pos = vec4(position.xyz, 1);
}
+1 -1
View File
@@ -38,7 +38,7 @@ void main(void)
(ab.x > 16 && ab.y > 16)
);
if(outline.Scissor == 0)
if(outline.Scissor == 0u)
{
if(rectPos.x < 3.0f || rectPos.x > rectSize.x - 3.0f ||
rectPos.y < 3.0f || rectPos.y > rectSize.y - 3.0f)
+2 -1
View File
@@ -23,7 +23,8 @@
******************************************************************************/
// outer code will hoist this up to just after the #version
//#extension GL_ARB_derivative_control : require
//#extension GL_ARB_derivative_control : enable
//#extension GL_ARB_shader_image_load_store : require
////////////////////////////////////////////////////////////////////////////////////////////
// Below shaders courtesy of Stephen Hill (@self_shadow), converted to glsl trivially
+13
View File
@@ -24,6 +24,8 @@
layout (location = 0) out vec4 color_out;
//#extension GL_ARB_gpu_shader5 : enable
//#include "texsample.h" // while includes aren't supported in glslang, this will be added in code
float ConvertSRGBToLinear(float srgb)
@@ -122,12 +124,23 @@ void main(void)
if(texdisplay.RawOutput != 0)
{
#ifdef GL_ARB_gpu_shader5
if (uintTex)
color_out = uintBitsToFloat(ucol);
else if (sintTex)
color_out = intBitsToFloat(scol);
else
color_out = col;
#else
// without being able to alias bits, we won't get accurate results.
// a cast is better than nothing though
if (uintTex)
color_out = vec4(ucol);
else if (sintTex)
color_out = vec4(scol);
else
color_out = col;
#endif
return;
}
+1 -1
View File
@@ -22,7 +22,7 @@
* THE SOFTWARE.
******************************************************************************/
layout(triangles, invocations = 1) in;
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
layout (location = 0) in vec4 IN_secondary[3];
+20 -17
View File
@@ -32,23 +32,6 @@ void GenerateGLSLShader(std::vector<std::string> &sources, ShaderType type,
sources.resize(4);
sources[0] = StringFormat::Fmt("#version %d core\n", version);
// hoist up any #extension directives
size_t extsearch = 0;
do
{
extsearch = shader.find("#extension", extsearch);
if(extsearch == string::npos)
break;
size_t begin = extsearch;
extsearch = shader.find('\n', extsearch);
sources[0] += shader.substr(begin, extsearch - begin + 1);
} while(extsearch != string::npos);
sources[0] += "\n" + defines + "\n";
if(uniforms)
sources[1] = GetEmbeddedResource(glsl_debuguniforms_h);
else
@@ -69,4 +52,24 @@ void GenerateGLSLShader(std::vector<std::string> &sources, ShaderType type,
}
sources[3] = shader;
for(int i = 0; i < 4; i++)
{
// hoist up any #extension directives
size_t extsearch = 0;
do
{
extsearch = sources[i].find("#extension", extsearch);
if(extsearch == string::npos)
break;
size_t begin = extsearch;
extsearch = sources[i].find('\n', extsearch);
sources[0] += sources[i].substr(begin, extsearch - begin + 1);
} while(extsearch != string::npos);
}
sources[0] += "\n" + defines + "\n";
}
+42 -13
View File
@@ -262,7 +262,7 @@ void GLReplay::InitDebugData()
vector<string> gs;
vector<string> cs;
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 420);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 150);
DebugData.texDisplayVSProg = CreateShaderProgram(vs, empty);
@@ -271,7 +271,7 @@ void GLReplay::InitDebugData()
string defines = string("#define UINT_TEX ") + (i == 1 ? "1" : "0") + "\n";
defines += string("#define SINT_TEX ") + (i == 2 ? "1" : "0") + "\n";
GenerateGLSLShader(fs, eShaderGLSL, defines, GetEmbeddedResource(glsl_texdisplay_frag), 420);
GenerateGLSLShader(fs, eShaderGLSL, defines, GetEmbeddedResource(glsl_texdisplay_frag), 150);
DebugData.texDisplayProg[i] = CreateShaderProgram(empty, fs);
}
@@ -302,7 +302,8 @@ void GLReplay::InitDebugData()
DebugData.glslVersion = glslVersion;
RDCLOG("GLSL version %d", glslVersion);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 420);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 150);
if(HasExt[ARB_shader_image_load_store])
{
@@ -326,7 +327,7 @@ void GLReplay::InitDebugData()
DebugData.quadoverdrawFSProg = CreateShaderProgram(empty, fs);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_quadresolve_frag), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_quadresolve_frag), 150);
DebugData.quadoverdrawResolveProg = CreateShaderProgram(vs, fs);
}
@@ -340,22 +341,22 @@ void GLReplay::InitDebugData()
DebugData.quadoverdrawResolveProg = 0;
}
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_checkerboard_frag), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_checkerboard_frag), 150);
DebugData.checkerProg = CreateShaderProgram(vs, fs);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_fixedcol_frag), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_fixedcol_frag), 150);
DebugData.fixedcolFSProg = CreateShaderProgram(empty, fs);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_vert), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_frag), 420);
GenerateGLSLShader(gs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_geom), 420);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_vert), 150);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_frag), 150);
GenerateGLSLShader(gs, eShaderGLSL, "", GetEmbeddedResource(glsl_mesh_geom), 150);
DebugData.meshProg = CreateShaderProgram(vs, fs);
DebugData.meshgsProg = CreateShaderProgram(vs, fs, gs);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_trisize_frag), 420);
GenerateGLSLShader(gs, eShaderGLSL, "", GetEmbeddedResource(glsl_trisize_geom), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_trisize_frag), 150);
GenerateGLSLShader(gs, eShaderGLSL, "", GetEmbeddedResource(glsl_trisize_geom), 150);
DebugData.trisizeProg = CreateShaderProgram(vs, fs, gs);
@@ -624,8 +625,8 @@ void GLReplay::InitDebugData()
gl.glVertexAttribPointer(0, 4, eGL_FLOAT, GL_FALSE, sizeof(Vec4f), NULL);
gl.glEnableVertexAttribArray(0);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 420);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_outline_frag), 420);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_blit_vert), 150);
GenerateGLSLShader(fs, eShaderGLSL, "", GetEmbeddedResource(glsl_outline_frag), 150);
DebugData.outlineQuadProg = CreateShaderProgram(vs, fs);
@@ -1496,6 +1497,24 @@ void GLReplay::PickPixel(ResourceId texture, uint32_t x, uint32_t y, uint32_t sl
gl.glReadPixels(0, 0, 1, 1, eGL_RGBA, eGL_FLOAT, (void *)pixel);
if(!HasExt[ARB_gpu_shader5])
{
for(int i = 0; i < 4; i++)
{
// if negative, cast via int
if(pixel[i] < 0.0f)
{
int casted = (int)pixel[i];
memcpy(&pixel[i], &casted, sizeof(casted));
}
else
{
uint32_t casted = (uint32_t)pixel[i];
memcpy(&pixel[i], &casted, sizeof(casted));
}
}
}
{
auto &texDetails = m_pDriver->m_Textures[texture];
@@ -1512,6 +1531,16 @@ void GLReplay::PickPixel(ResourceId texture, uint32_t x, uint32_t y, uint32_t sl
uint32_t stencilpixel[4];
gl.glReadPixels(0, 0, 1, 1, eGL_RGBA, eGL_FLOAT, (void *)stencilpixel);
if(!HasExt[ARB_gpu_shader5])
{
// bits weren't aliased, so re-cast back to uint.
float fpix[4];
memcpy(fpix, stencilpixel, sizeof(fpix));
stencilpixel[0] = (uint32_t)fpix[0];
stencilpixel[1] = (uint32_t)fpix[1];
}
// not sure whether [0] or [1] will return stencil values, so use
// max of two because other channel should be 0
pixel[1] = float(RDCMAX(stencilpixel[0], stencilpixel[1])) / 255.0f;
+1 -5
View File
@@ -1205,11 +1205,7 @@ void WrappedOpenGL::ContextData::CreateDebugData(const GLHookSet &gl)
vector<string> vs;
vector<string> fs;
GenerateGLSLShader(vs, eShaderGLSL,
"#extension GL_ARB_shading_language_420pack : require\n"
"#extension GL_ARB_separate_shader_objects : require\n"
"#extension GL_ARB_explicit_attrib_location : require\n",
GetEmbeddedResource(glsl_text_vert), 150);
GenerateGLSLShader(vs, eShaderGLSL, "", GetEmbeddedResource(glsl_text_vert), 150);
GenerateGLSLShader(fs, eShaderGLSL,
"#extension GL_ARB_shading_language_420pack : require\n"