From bb9ff1ccb7c636416a8cf9ee0c5775b74297a2fc Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 17 Dec 2018 15:27:42 +0000 Subject: [PATCH] Adjust GL SPIR-V shader test demo to do opengl-unique things * E.g. not specify a binding and let that be done at runtime, or global variables not in constant buffers. --- util/test/demos/gl/gl_spirv_shader.cpp | 60 +++++++++++++++----------- util/test/demos/test_common.cpp | 7 ++- util/test/demos/test_common.h | 9 +++- util/test/demos/vk/vk_test.cpp | 3 +- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/util/test/demos/gl/gl_spirv_shader.cpp b/util/test/demos/gl/gl_spirv_shader.cpp index 076024c5f..5de4b32a4 100644 --- a/util/test/demos/gl/gl_spirv_shader.cpp +++ b/util/test/demos/gl/gl_spirv_shader.cpp @@ -29,7 +29,7 @@ struct SPIRV_Shader : OpenGLGraphicsTest static constexpr const char *Description = "Draws using a SPIR-V shader pipeline."; std::string vertex = R"EOSHADER( -#version 420 core +#version 430 core layout(location = 0) in vec3 Position; layout(location = 1) in vec4 Color; @@ -39,25 +39,21 @@ layout(location = 0) out vec4 oPos; layout(location = 1) out vec4 oCol; layout(location = 2) out vec2 oUV; -layout(binding = 0, std140) uniform vsconstsbuf -{ - vec4 offset; - vec4 scale; - - vec2 UVscroll; -} vsconsts; +layout(location = 2) uniform vec4 offset; +layout(location = 8) uniform vec4 scale; +layout(location = 13) uniform vec4 UVscroll; void main() { - gl_Position = oPos = vec4(Position.xyz * vsconsts.scale.xyz + vsconsts.offset.xyz, 1); + gl_Position = oPos = vec4(Position.xyz * scale.xyz + offset.xyz, 1); oCol = Color; - oUV = UV + vsconsts.UVscroll.xy; + oUV = UV + UVscroll.xy; } )EOSHADER"; std::string pixel = R"EOSHADER( -#version 420 core +#version 430 core layout(location = 0) in vec4 iPos; layout(location = 1) in vec4 iCol; @@ -65,16 +61,13 @@ layout(location = 2) in vec2 iUV; layout(location = 0) out vec4 Color; -layout(binding = 0) uniform sampler2D tex2D; +layout(location = 5) uniform sampler2D tex2D; -layout(binding = 1, std140) uniform fsconstsbuf -{ - vec4 tint; -} fsconsts; +layout(location = 7) uniform vec4 tint; void main() { - Color = (iCol + fsconsts.tint) * textureLod(tex2D, iUV, 0.0f); + Color = (iCol + tint) * textureLod(tex2D, iUV, 0.0f); } )EOSHADER"; @@ -127,6 +120,8 @@ void main() glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glBindTexture(GL_TEXTURE_2D, 0); + GLuint vsbuf = MakeBuffer(); glBindBuffer(GL_UNIFORM_BUFFER, vsbuf); glBufferStorage(GL_UNIFORM_BUFFER, sizeof(Vec4f) * 3, 0, GL_DYNAMIC_STORAGE_BIT); @@ -147,10 +142,10 @@ void main() GLuint vs = glCreateShader(GL_VERTEX_SHADER); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); - std::vector vsSPIRV = - CompileShaderToSpv(vertex, ShaderLang::glsl, ShaderStage::vert, "main"); - std::vector fsSPIRV = - CompileShaderToSpv(pixel, ShaderLang::glsl, ShaderStage::frag, "main"); + std::vector vsSPIRV = CompileShaderToSpv( + vertex, SPIRVTarget::opengl, ShaderLang::glsl, ShaderStage::vert, "main"); + std::vector fsSPIRV = CompileShaderToSpv( + pixel, SPIRVTarget::opengl, ShaderLang::glsl, ShaderStage::frag, "main"); glShaderBinary(1, &vs, GL_SHADER_BINARY_FORMAT_SPIR_V, vsSPIRV.data(), (GLsizei)vsSPIRV.size() * 4); @@ -219,6 +214,9 @@ void main() fsdata = Vec4f(0.1f, 0.2f, 0.3f, 1.0f); + glActiveTexture(GL_TEXTURE9); + glBindTexture(GL_TEXTURE_2D, tex); + while(Running()) { float col[] = {0.4f, 0.5f, 0.6f, 1.0f}; @@ -227,13 +225,27 @@ void main() vsdata.UVscroll.x += 0.01f; vsdata.UVscroll.y += 0.02f; + for(GLuint prog : {glslprogram, spirvprogram}) + { + glUseProgram(prog); + // tex2D location 5 + glUniform1i(5, 9); + + // offset location 2 + // scale location 8 + // UVscroll location 13 + glUniform4fv(2, 1, &vsdata.offset.x); + glUniform4fv(8, 1, &vsdata.scale.x); + glUniform4fv(13, 1, &vsdata.UVscroll.x); + + // tint location 7 + glUniform4fv(7, 1, &fsdata.x); + } + glBindVertexArray(vao); glViewport(0, 0, GLsizei(screenWidth) >> 1, GLsizei(screenHeight)); - glNamedBufferSubData(vsbuf, 0, sizeof(vsdata), &vsdata); - glNamedBufferSubData(fsbuf, 0, sizeof(fsdata), &fsdata); - glUseProgram(glslprogram); glDrawArrays(GL_TRIANGLES, 0, 3); diff --git a/util/test/demos/test_common.cpp b/util/test/demos/test_common.cpp index ff3dd746b..32e8f6e56 100644 --- a/util/test/demos/test_common.cpp +++ b/util/test/demos/test_common.cpp @@ -213,8 +213,8 @@ bool SpvCompilationSupported() return WEXITSTATUS(code) == 0; } -std::vector CompileShaderToSpv(const std::string &source_text, ShaderLang lang, - ShaderStage stage, const char *entry_point) +std::vector CompileShaderToSpv(const std::string &source_text, SPIRVTarget target, + ShaderLang lang, ShaderStage stage, const char *entry_point) { std::vector ret; @@ -243,6 +243,9 @@ std::vector CompileShaderToSpv(const std::string &source_text, ShaderL case ShaderStage::comp: shader_kind = shaderc_compute_shader; break; } + if(target == SPIRVTarget::opengl) + shaderc_compile_options_set_target_env(opts, shaderc_target_env_opengl, 0); + shaderc_compilation_result_t res = shaderc_compile_into_spv( shaderc, source_text.c_str(), source_text.size(), shader_kind, "inshader", entry_point, opts); diff --git a/util/test/demos/test_common.h b/util/test/demos/test_common.h index e964fa158..c400418d9 100644 --- a/util/test/demos/test_common.h +++ b/util/test/demos/test_common.h @@ -41,6 +41,11 @@ typedef uint8_t byte; +enum class SPIRVTarget +{ + opengl, + vulkan +}; enum class ShaderLang { glsl, @@ -57,8 +62,8 @@ enum class ShaderStage }; bool SpvCompilationSupported(); -std::vector CompileShaderToSpv(const std::string &source_text, ShaderLang lang, - ShaderStage stage, const char *entry_point); +std::vector CompileShaderToSpv(const std::string &source_text, SPIRVTarget target, + ShaderLang lang, ShaderStage stage, const char *entry_point); struct Vec2f { diff --git a/util/test/demos/vk/vk_test.cpp b/util/test/demos/vk/vk_test.cpp index e57f67a70..19ffb9f5a 100644 --- a/util/test/demos/vk/vk_test.cpp +++ b/util/test/demos/vk/vk_test.cpp @@ -578,7 +578,8 @@ VkPipelineShaderStageCreateInfo VulkanGraphicsTest::CompileShaderModule( { VkShaderModule ret = VK_NULL_HANDLE; - std::vector spirv = ::CompileShaderToSpv(source_text, lang, stage, entry_point); + std::vector spirv = + ::CompileShaderToSpv(source_text, SPIRVTarget::vulkan, lang, stage, entry_point); if(spirv.empty()) return {};