From 9e786f258ca13670943036ac4a2b36aee89dd5c4 Mon Sep 17 00:00:00 2001 From: baldurk Date: Thu, 21 Jul 2016 18:42:13 +0200 Subject: [PATCH] Make internal vulkan shaders compatible with GL with a few #ifdefs --- renderdoc/data/spv/blit.vert | 2 +- renderdoc/data/spv/debuguniforms.h | 61 ++++++++++++++++++++++----- renderdoc/data/spv/fixedcol.frag | 16 +++++-- renderdoc/data/spv/mesh.comp | 2 + renderdoc/data/spv/mesh.vert | 10 ++++- renderdoc/data/spv/quadwrite.frag | 4 ++ renderdoc/data/spv/texdisplay.frag | 27 +++++++++++- renderdoc/data/spv/texsample.h | 20 +++++++++ renderdoc/data/spv/text.frag | 4 ++ renderdoc/data/spv/text.vert | 4 +- renderdoc/driver/vulkan/vk_debug.cpp | 4 +- renderdoc/driver/vulkan/vk_replay.cpp | 4 +- 12 files changed, 137 insertions(+), 21 deletions(-) diff --git a/renderdoc/data/spv/blit.vert b/renderdoc/data/spv/blit.vert index 7b339e902..ac6e90b39 100644 --- a/renderdoc/data/spv/blit.vert +++ b/renderdoc/data/spv/blit.vert @@ -35,5 +35,5 @@ void main(void) vec4(-1.0, 1.0, 0.5, 1.0), vec4( 1.0, 1.0, 0.5, 1.0)); - gl_Position = verts[gl_VertexIndex]; + gl_Position = verts[VERTEX_ID]; } diff --git a/renderdoc/data/spv/debuguniforms.h b/renderdoc/data/spv/debuguniforms.h index 1513e7079..01f423c0f 100644 --- a/renderdoc/data/spv/debuguniforms.h +++ b/renderdoc/data/spv/debuguniforms.h @@ -37,7 +37,7 @@ #define mat4 Matrix4f #define uint uint32_t -#define BINDING(s, b) +#define BINDING(b) #define INST_NAME(name) struct Vec4u @@ -47,18 +47,41 @@ struct Vec4u #define uvec4 Vec4u +#if !defined(VULKAN) && !defined(OPENGL) +#error "Must define VULKAN or OPENGL before including debuguniforms.h" +#endif + +#if defined(VULKAN) && defined(OPENGL) +#error "Only one of VULKAN and OPENGL must be defined in debuguniforms.h" +#endif + #else // this has to happen above even any pre-processor definitions, // so it's added in code //#version 430 core -#define BINDING(s, b) layout(set = s, binding = b, std140) +#ifdef VULKAN + +#define BINDING(b) layout(set = 0, binding = b, std140) +#define VERTEX_ID gl_VertexIndex +#define INSTANCE_ID gl_InstanceIndex + +#else + +#define OPENGL 1 + +#define BINDING(b) layout(binding = b, std140) +#define VERTEX_ID gl_VertexID +#define INSTANCE_ID gl_InstanceID + +#endif + #define INST_NAME(name) name #endif -BINDING(0, 2) uniform HistogramUBOData +BINDING(2) uniform HistogramUBOData { uint HistogramChannels; float HistogramMin; @@ -75,7 +98,7 @@ BINDING(0, 2) uniform HistogramUBOData } INST_NAME(histogram_minmax); -BINDING(0, 0) uniform MeshUBOData +BINDING(0) uniform MeshUBOData { mat4 mvp; mat4 invProj; @@ -86,7 +109,7 @@ BINDING(0, 0) uniform MeshUBOData } INST_NAME(Mesh); -BINDING(0, 0) uniform OutlineUBOData +BINDING(0) uniform OutlineUBOData { vec4 Inner_Color; vec4 Border_Color; @@ -96,7 +119,7 @@ BINDING(0, 0) uniform OutlineUBOData } INST_NAME(outline); -BINDING(0, 0) uniform FontUBOData +BINDING(0) uniform FontUBOData { vec2 TextPosition; float txtpadding; @@ -107,7 +130,7 @@ BINDING(0, 0) uniform FontUBOData } INST_NAME(general); -BINDING(0, 0) uniform MeshPickUBOData +BINDING(0) uniform MeshPickUBOData { vec2 coords; vec2 viewport; @@ -130,7 +153,7 @@ struct FontGlyphData #define FONT_FIRST_CHAR 32 #define FONT_LAST_CHAR 126 -BINDING(0, 1) uniform GlyphUBOData +BINDING(1) uniform GlyphUBOData { FontGlyphData data[FONT_LAST_CHAR - FONT_FIRST_CHAR + 1]; } @@ -138,13 +161,13 @@ INST_NAME(glyphs); #define MAX_SINGLE_LINE_LENGTH 256 -BINDING(0, 2) uniform StringUBOData +BINDING(2) uniform StringUBOData { uvec4 chars[MAX_SINGLE_LINE_LENGTH]; } INST_NAME(str); -BINDING(0, 0) uniform TexDisplayUBOData +BINDING(0) uniform TexDisplayUBOData { vec2 Position; float Scale; @@ -178,6 +201,8 @@ INST_NAME(texdisplay); #define CUBEMAP_FACE_POS_Z 4 #define CUBEMAP_FACE_NEG_Z 5 +#ifdef VULKAN + // we always upload an array (but it might have only one layer), // so 2D and 2D arrays are the same. // Cube and cube array textures are treated as 2D arrays. @@ -187,6 +212,22 @@ INST_NAME(texdisplay); #define RESTYPE_TEX2DMS 0x4 #define RESTYPE_TEXTYPEMAX 0x5 +#else // OPENGL + +#define RESTYPE_TEX1D 0x1 +#define RESTYPE_TEX2D 0x2 +#define RESTYPE_TEX3D 0x3 +#define RESTYPE_TEXCUBE 0x4 +#define RESTYPE_TEX1DARRAY 0x5 +#define RESTYPE_TEX2DARRAY 0x6 +#define RESTYPE_TEXCUBEARRAY 0x7 +#define RESTYPE_TEXRECT 0x8 +#define RESTYPE_TEXBUFFER 0x9 +#define RESTYPE_TEX2DMS 0xA +#define RESTYPE_TEXTYPEMAX 0xA + +#endif + #define MESHDISPLAY_SOLID 0x1 #define MESHDISPLAY_FACELIT 0x2 #define MESHDISPLAY_SECONDARY 0x3 diff --git a/renderdoc/data/spv/fixedcol.frag b/renderdoc/data/spv/fixedcol.frag index 6f8a91489..3e51d98d2 100644 --- a/renderdoc/data/spv/fixedcol.frag +++ b/renderdoc/data/spv/fixedcol.frag @@ -27,11 +27,19 @@ layout (location = 0) out vec4 color_out; +#ifndef VULKAN // OpenGL can't use SPIR-V patching +uniform vec4 RENDERDOC_GenericFS_Color; +#endif + void main(void) { - // used to have a shader-replacement pixel shader - // that outputs a fixed colour, without needing a - // slot in a descriptor set. We re-write the SPIR-V - // on the fly to replace these constants +#ifdef VULKAN + // used to have a shader-replacement pixel shader + // that outputs a fixed colour, without needing a + // slot in a descriptor set. We re-write the SPIR-V + // on the fly to replace these constants color_out = vec4(1.1f, 2.2f, 3.3f, 4.4f); +#else + color_out = RENDERDOC_GenericFS_Color; +#endif } diff --git a/renderdoc/data/spv/mesh.comp b/renderdoc/data/spv/mesh.comp index 04e6feef3..d06ddfbab 100644 --- a/renderdoc/data/spv/mesh.comp +++ b/renderdoc/data/spv/mesh.comp @@ -57,8 +57,10 @@ void main() wpos.xyz /= wpos.www; +#ifdef VULKAN if(meshpick.unproject == 0u) wpos.xy *= vec2(1.0f, -1.0f); +#endif vec2 scr = (wpos.xy + 1.0f) * 0.5f * meshpick.viewport; diff --git a/renderdoc/data/spv/mesh.vert b/renderdoc/data/spv/mesh.vert index 35cf83cc3..88d7b45b1 100644 --- a/renderdoc/data/spv/mesh.vert +++ b/renderdoc/data/spv/mesh.vert @@ -50,18 +50,26 @@ void main(void) vec4 pos = position; if(Mesh.homogenousInput == 0) + { pos = vec4(position.xyz, 1); + } else + { +#ifdef VULKAN pos = vec4(position.x, -position.y, position.z, position.w); +#endif + } gl_Position = Mesh.mvp * pos; - gl_Position.xy += Mesh.pointSpriteSize.xy*0.01f*psprite[gl_VertexIndex%4]*gl_Position.w; + gl_Position.xy += Mesh.pointSpriteSize.xy*0.01f*psprite[VERTEX_ID%4]*gl_Position.w; OUT.secondary = secondary; OUT.norm = vec4(0, 0, 1, 1); +#ifdef VULKAN // GL->VK conventions gl_Position.y = -gl_Position.y; gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0; gl_PointSize = 4.0f; +#endif } diff --git a/renderdoc/data/spv/quadwrite.frag b/renderdoc/data/spv/quadwrite.frag index 4e99c8d26..a54f2fef4 100644 --- a/renderdoc/data/spv/quadwrite.frag +++ b/renderdoc/data/spv/quadwrite.frag @@ -32,8 +32,12 @@ // https://github.com/selfshadow/demos/blob/master/QuadShading/QuadShading.fx //////////////////////////////////////////////////////////////////////////////////////////// +#ifdef VULKAN // descriptor set will be patched from 0 to whichever descriptor set we're using in code layout(set = 0, binding = 0, r32ui) uniform coherent uimage2DArray overdrawImage; +#else // OPENGL +layout(binding = 0, r32ui) uniform coherent uimage2DArray overdrawImage; +#endif layout(early_fragment_tests) in; void main() diff --git a/renderdoc/data/spv/texdisplay.frag b/renderdoc/data/spv/texdisplay.frag index 853193438..98a88f377 100644 --- a/renderdoc/data/spv/texdisplay.frag +++ b/renderdoc/data/spv/texdisplay.frag @@ -36,8 +36,23 @@ float ConvertSRGBToLinear(float srgb) void main(void) { +#ifdef VULKAN // vulkan combines all three types bool uintTex = (texdisplay.OutputDisplayFormat & TEXDISPLAY_UINT_TEX) != 0; bool sintTex = (texdisplay.OutputDisplayFormat & TEXDISPLAY_SINT_TEX) != 0; +#else // OPENGL + +#if UINT_TEX + const bool uintTex = true; + const bool sintTex = false; +#elif SINT_TEX + const bool uintTex = false; + const bool sintTex = true; +#else + const bool uintTex = false; + const bool sintTex = false; +#endif + +#endif int texType = (texdisplay.OutputDisplayFormat & TEXDISPLAY_TYPEMASK); @@ -46,11 +61,21 @@ void main(void) ivec4 scol; // calc screen co-ords with origin top left, modified by Position - vec2 scr = gl_FragCoord.xy - texdisplay.Position.xy; + vec2 scr = gl_FragCoord.xy; + +#ifdef OPENGL + scr.y = OutputRes.y - scr.y; +#endif + + scr -= texdisplay.Position.xy; scr /= texdisplay.Scale; +#ifdef VULKAN if(texType == RESTYPE_TEX1D) +#else + if(texType == RESTYPE_TEX1D || texType == RESTYPE_TEXBUFFER || texType == RESTYPE_TEX1DARRAY) +#endif { // by convention display 1D textures as 100 high if(scr.x < 0.0f || scr.x > texdisplay.TextureResolutionPS.x || scr.y < 0.0f || scr.y > 100.0f) diff --git a/renderdoc/data/spv/texsample.h b/renderdoc/data/spv/texsample.h index 33b5169fa..022a8b28e 100644 --- a/renderdoc/data/spv/texsample.h +++ b/renderdoc/data/spv/texsample.h @@ -22,6 +22,26 @@ * THE SOFTWARE. ******************************************************************************/ +vec3 CalcCubeCoord(vec2 uv, int face) +{ + // From table 8.19 in GL4.5 spec + // Map UVs to [-0.5, 0.5] and rotate + uv -= vec2(0.5); + vec3 coord; + if(face == CUBEMAP_FACE_POS_X) + coord = vec3(0.5, -uv.y, -uv.x); + else if(face == CUBEMAP_FACE_NEG_X) + coord = vec3(-0.5, -uv.y, uv.x); + else if(face == CUBEMAP_FACE_POS_Y) + coord = vec3(uv.x, 0.5, uv.y); + else if(face == CUBEMAP_FACE_NEG_Y) + coord = vec3(uv.x, -0.5, -uv.y); + else if(face == CUBEMAP_FACE_POS_Z) + coord = vec3(uv.x, -uv.y, 0.5); + else // face == CUBEMAP_FACE_NEG_Z + coord = vec3(-uv.x, -uv.y, -0.5); + return coord; +} // these bindings are defined based on the RESTYPE_ defines in debuguniforms.h // binding = 5 + RESTYPE_x diff --git a/renderdoc/data/spv/text.frag b/renderdoc/data/spv/text.frag index 661481654..7e4e227bc 100644 --- a/renderdoc/data/spv/text.frag +++ b/renderdoc/data/spv/text.frag @@ -22,7 +22,11 @@ * THE SOFTWARE. ******************************************************************************/ +#ifdef VULKAN layout (binding = 3) uniform sampler2D tex0; +#else // OPENGL +layout (binding = 0) uniform sampler2D tex0; +#endif layout (location = 0) out vec4 color_out; layout (location = 0) in vec4 tex; diff --git a/renderdoc/data/spv/text.vert b/renderdoc/data/spv/text.vert index 1a29a9cb5..106c42e06 100644 --- a/renderdoc/data/spv/text.vert +++ b/renderdoc/data/spv/text.vert @@ -38,8 +38,8 @@ void main(void) vec3( 0.0, 1.0, 0.5), vec3( 1.0, 1.0, 0.5)); - vec3 pos = verts[gl_VertexIndex]; - uint strindex = gl_InstanceIndex; + vec3 pos = verts[VERTEX_ID]; + uint strindex = INSTANCE_ID; vec2 charPos = vec2(strindex + pos.x + general.TextPosition.x, pos.y + general.TextPosition.y); diff --git a/renderdoc/driver/vulkan/vk_debug.cpp b/renderdoc/driver/vulkan/vk_debug.cpp index 9f3a0ecc8..06c8088c3 100644 --- a/renderdoc/driver/vulkan/vk_debug.cpp +++ b/renderdoc/driver/vulkan/vk_debug.cpp @@ -27,7 +27,6 @@ #include "3rdparty/glslang/SPIRV/spirv.hpp" #include "3rdparty/stb/stb_truetype.h" #include "common/shader_cache.h" -#include "data/spv/debuguniforms.h" #include "driver/shaders/spirv/spirv_common.h" #include "maths/camera.h" #include "maths/formatpacking.h" @@ -35,6 +34,9 @@ #include "serialise/string_utils.h" #include "vk_core.h" +#define VULKAN 1 +#include "data/spv/debuguniforms.h" + const VkDeviceSize STAGE_BUFFER_BYTE_SIZE = 16 * 1024 * 1024ULL; void VulkanDebugManager::GPUBuffer::Create(WrappedVulkan *driver, VkDevice dev, VkDeviceSize size, diff --git a/renderdoc/driver/vulkan/vk_replay.cpp b/renderdoc/driver/vulkan/vk_replay.cpp index 0753bbaa0..26d4350fd 100644 --- a/renderdoc/driver/vulkan/vk_replay.cpp +++ b/renderdoc/driver/vulkan/vk_replay.cpp @@ -24,7 +24,6 @@ #include "vk_replay.h" #include -#include "data/spv/debuguniforms.h" #include "maths/camera.h" #include "maths/matrix.h" #include "serialise/string_utils.h" @@ -32,6 +31,9 @@ #include "vk_debug.h" #include "vk_resources.h" +#define VULKAN 1 +#include "data/spv/debuguniforms.h" + VulkanReplay::OutputWindow::OutputWindow() : wnd(NULL_WND_HANDLE), width(0), height(0) { surface = VK_NULL_HANDLE;