Browse Source

shaderc: Added support for gl_VertexID/InstanceID. Issue #1062.

Branimir Karadžić 8 years ago
parent
commit
3d73d0904b

BIN
examples/runtime/shaders/dx11/vs_instancing.bin


BIN
examples/runtime/shaders/glsl/vs_instancing.bin


+ 14 - 1
src/renderer_gl.cpp

@@ -940,6 +940,13 @@ namespace bgfx { namespace gl
 		NULL
 		NULL
 	};
 	};
 
 
+	static const char* s_EXT_gpu_shader4[] =
+	{
+		"gl_VertexID",
+		"gl_InstanceID",
+		NULL
+	};
+
 	static const char* s_ARB_gpu_shader5[] =
 	static const char* s_ARB_gpu_shader5[] =
 	{
 	{
 		"bitfieldReverse",
 		"bitfieldReverse",
@@ -5602,6 +5609,7 @@ namespace bgfx { namespace gl
 						&& s_extension[Extension::ARB_shader_texture_lod].m_supported
 						&& s_extension[Extension::ARB_shader_texture_lod].m_supported
 						&& bx::findIdentifierMatch(code, s_ARB_shader_texture_lod)
 						&& bx::findIdentifierMatch(code, s_ARB_shader_texture_lod)
 						;
 						;
+					const bool usesGpuShader4   = !!bx::findIdentifierMatch(code, s_EXT_gpu_shader4);
 					const bool usesGpuShader5   = !!bx::findIdentifierMatch(code, s_ARB_gpu_shader5);
 					const bool usesGpuShader5   = !!bx::findIdentifierMatch(code, s_ARB_gpu_shader5);
 					const bool usesIUsamplers   = !!bx::findIdentifierMatch(code, s_uisamplers);
 					const bool usesIUsamplers   = !!bx::findIdentifierMatch(code, s_uisamplers);
 					const bool usesTexelFetch   = !!bx::findIdentifierMatch(code, s_texelFetch);
 					const bool usesTexelFetch   = !!bx::findIdentifierMatch(code, s_texelFetch);
@@ -5610,7 +5618,7 @@ namespace bgfx { namespace gl
 					const bool usesPacking      = !!bx::findIdentifierMatch(code, s_ARB_shading_language_packing);
 					const bool usesPacking      = !!bx::findIdentifierMatch(code, s_ARB_shading_language_packing);
 
 
 					uint32_t version =
 					uint32_t version =
-						  usesIUsamplers || usesTexelFetch || usesGpuShader5 ? 130
+						  usesIUsamplers|| usesTexelFetch || usesGpuShader5 ? 130
 						: usesTextureLod ? 120
 						: usesTextureLod ? 120
 						: 120
 						: 120
 						;
 						;
@@ -5633,6 +5641,11 @@ namespace bgfx { namespace gl
 						}
 						}
 					}
 					}
 
 
+					if (usesGpuShader4)
+					{
+						writeString(&writer, "#extension GL_EXT_gpu_shader4 : enable\n");
+					}
+
 					if (usesGpuShader5)
 					if (usesGpuShader5)
 					{
 					{
 						writeString(&writer, "#extension GL_ARB_gpu_shader5 : enable\n");
 						writeString(&writer, "#extension GL_ARB_gpu_shader5 : enable\n");

+ 68 - 4
tools/shaderc/shaderc.cpp

@@ -78,6 +78,13 @@ namespace bgfx
 		NULL
 		NULL
 	};
 	};
 
 
+	static const char* s_EXT_gpu_shader4[] =
+	{
+		"gl_VertexID",
+		"gl_InstanceID",
+		NULL
+	};
+
 	static const char* s_ARB_gpu_shader5[] =
 	static const char* s_ARB_gpu_shader5[] =
 	{
 	{
 		"bitfieldReverse",
 		"bitfieldReverse",
@@ -1671,7 +1678,7 @@ namespace bgfx
 								}
 								}
 								else
 								else
 								{
 								{
-									fprintf(stderr, "PrimitiveID builtin is not supported by this D3D9 HLSL.\n");
+									fprintf(stderr, "gl_PrimitiveID builtin is not supported by this D3D9 HLSL.\n");
 									return EXIT_FAILURE;
 									return EXIT_FAILURE;
 								}
 								}
 							}
 							}
@@ -1698,6 +1705,9 @@ namespace bgfx
 						}
 						}
 						else if ('v' == shaderType)
 						else if ('v' == shaderType)
 						{
 						{
+							const bool hasVertexId    = NULL != bx::strFind(input, "gl_VertexID");
+							const bool hasInstanceId  = NULL != bx::strFind(input, "gl_InstanceID");
+
 							const char* brace = bx::strFind(entry, "{");
 							const char* brace = bx::strFind(entry, "{");
 							if (NULL != brace)
 							if (NULL != brace)
 							{
 							{
@@ -1730,17 +1740,55 @@ namespace bgfx
 
 
 							preprocessor.writef("#define void_main() \\\n");
 							preprocessor.writef("#define void_main() \\\n");
 							preprocessor.writef("Output main(");
 							preprocessor.writef("Output main(");
-							bool first = true;
+							uint32_t arg = 0;
 							for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
 							for (InOut::const_iterator it = shaderInputs.begin(), itEnd = shaderInputs.end(); it != itEnd; ++it)
 							{
 							{
 								VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
 								VaryingMap::const_iterator varyingIt = varyingMap.find(*it);
 								if (varyingIt != varyingMap.end() )
 								if (varyingIt != varyingMap.end() )
 								{
 								{
 									const Varying& var = varyingIt->second;
 									const Varying& var = varyingIt->second;
-									preprocessor.writef("%s%s %s : %s\\\n", first ? "" : "\t, ", var.m_type.c_str(), var.m_name.c_str(), var.m_semantics.c_str() );
-									first = false;
+									preprocessor.writef(
+										" \\\n\t%s%s %s : %s"
+										, arg++ > 0 ? ", " : ""
+										, var.m_type.c_str()
+										, var.m_name.c_str()
+										, var.m_semantics.c_str()
+										);
+								}
+							}
+
+							if (hasVertexId)
+							{
+								if (d3d > 9)
+								{
+									preprocessor.writef(
+										" \\\n\t%suint gl_VertexID : SV_VertexID"
+										, arg++ > 0 ? ", " : "  "
+										);
+								}
+								else
+								{
+									fprintf(stderr, "gl_VertexID builtin is not supported by this D3D9 HLSL.\n");
+									return EXIT_FAILURE;
 								}
 								}
 							}
 							}
+
+							if (hasInstanceId)
+							{
+								if (d3d > 9)
+								{
+									preprocessor.writef(
+										" \\\n\t%suint gl_InstanceID : SV_InstanceID"
+										, arg++ > 0 ? ", " : "  "
+										);
+								}
+								else
+								{
+									fprintf(stderr, "gl_InstanceID builtin is not supported by this D3D9 HLSL.\n");
+									return EXIT_FAILURE;
+								}
+							}
+
 							preprocessor.writef(
 							preprocessor.writef(
 								") \\\n"
 								") \\\n"
 								"{ \\\n"
 								"{ \\\n"
@@ -1857,6 +1905,8 @@ namespace bgfx
 									|| !!bx::findIdentifierMatch(input, s_ARB_shader_texture_lod)
 									|| !!bx::findIdentifierMatch(input, s_ARB_shader_texture_lod)
 									|| !!bx::findIdentifierMatch(input, s_EXT_shader_texture_lod)
 									|| !!bx::findIdentifierMatch(input, s_EXT_shader_texture_lod)
 									;
 									;
+								const bool usesInstanceID   = !!bx::strFind(input, "gl_InstanceID");
+								const bool usesGpuShader4   = !!bx::findIdentifierMatch(input, s_EXT_gpu_shader4);
 								const bool usesGpuShader5   = !!bx::findIdentifierMatch(input, s_ARB_gpu_shader5);
 								const bool usesGpuShader5   = !!bx::findIdentifierMatch(input, s_ARB_gpu_shader5);
 								const bool usesTexelFetch   = !!bx::findIdentifierMatch(input, s_texelFetch);
 								const bool usesTexelFetch   = !!bx::findIdentifierMatch(input, s_texelFetch);
 								const bool usesTextureMS    = !!bx::findIdentifierMatch(input, s_ARB_texture_multisample);
 								const bool usesTextureMS    = !!bx::findIdentifierMatch(input, s_ARB_texture_multisample);
@@ -1880,6 +1930,20 @@ namespace bgfx
 										glsl = 130;
 										glsl = 130;
 									}
 									}
 
 
+									if (usesInstanceID)
+									{
+										bx::stringPrintf(code
+											, "#extension GL_ARB_draw_instanced : enable\n"
+											);
+									}
+
+									if (usesGpuShader4)
+									{
+										bx::stringPrintf(code
+											, "#extension GL_EXT_gpu_shader4 : enable\n"
+											);
+									}
+
 									if (usesGpuShader5)
 									if (usesGpuShader5)
 									{
 									{
 										bx::stringPrintf(code
 										bx::stringPrintf(code

+ 7 - 4
tools/shaderc/shaderc_spirv.cpp

@@ -475,6 +475,8 @@ namespace bgfx { namespace spirv
 		return true;
 		return true;
 	}
 	}
 
 
+#define DBG(...)
+
 	void disassemble(bx::WriterI* _writer, bx::ReaderSeekerI* _reader, bx::Error* _err)
 	void disassemble(bx::WriterI* _writer, bx::ReaderSeekerI* _reader, bx::Error* _err)
 	{
 	{
 		BX_UNUSED(_writer);
 		BX_UNUSED(_writer);
@@ -497,24 +499,25 @@ namespace bgfx { namespace spirv
 				if (0 < num
 				if (0 < num
 				&&  0 != bx::strCmp(id.var.name.c_str(), "gl_PerVertex") )
 				&&  0 != bx::strCmp(id.var.name.c_str(), "gl_PerVertex") )
 				{
 				{
-					printf("%3d: %s %d %s\n"
+					DBG("%3d: %s %d %s\n"
 						, it->first
 						, it->first
 						, id.var.name.c_str()
 						, id.var.name.c_str()
 						, id.var.location
 						, id.var.location
 						, getName(id.var.storageClass)
 						, getName(id.var.storageClass)
 						);
 						);
-					printf("{\n");
+					DBG("{\n");
 					for (uint32_t ii = 0; ii < num; ++ii)
 					for (uint32_t ii = 0; ii < num; ++ii)
 					{
 					{
 						const SpvReflection::Id::Variable& var = id.members[ii];
 						const SpvReflection::Id::Variable& var = id.members[ii];
-						printf("\t\t%s %s %d %s\n"
+						DBG("\t\t%s %s %d %s\n"
 							, spvx.getTypeName(var.type).c_str()
 							, spvx.getTypeName(var.type).c_str()
 							, var.name.c_str()
 							, var.name.c_str()
 							, var.offset
 							, var.offset
 							, getName(var.storageClass)
 							, getName(var.storageClass)
 							);
 							);
+						BX_UNUSED(var);
 					}
 					}
-					printf("}\n");
+					DBG("}\n");
 				}
 				}
 			}
 			}