full implementation of compute msaa-to-buffer

This commit is contained in:
Remi Palandri
2022-03-09 12:10:47 +00:00
committed by Baldur Karlsson
parent b72bdb2b9a
commit 79aee1df43
24 changed files with 1576 additions and 1389 deletions
+4
View File
@@ -69,5 +69,9 @@ DECLARE_EMBED(glsl_pixelhistory_primid_frag);
DECLARE_EMBED(glsl_shaderdebug_sample_vert);
DECLARE_EMBED(glsl_texremap_frag);
DECLARE_EMBED(glsl_discard_frag);
DECLARE_EMBED(glsl_vk_ms2buffer_comp);
DECLARE_EMBED(glsl_vk_depthms2buffer_comp);
DECLARE_EMBED(glsl_vk_buffer2ms_comp);
DECLARE_EMBED(glsl_vk_depthbuf2ms_frag);
#undef DECLARE_EMBED
+11
View File
@@ -84,6 +84,17 @@ precision highp int;
#define CUBEMAP_FACE_POS_Z 4
#define CUBEMAP_FACE_NEG_Z 5
// used in depthms2buffer.comp to define enum-format mapping
#define SHADER_D16_UNORM 0
#define SHADER_D16_UNORM_S8_UINT 1
#define SHADER_X8_D24_UNORM_PACK32 2
#define SHADER_D24_UNORM_S8_UINT 3
#define SHADER_D32_SFLOAT 4
#define SHADER_D32_SFLOAT_S8_UINT 5
// divide MS<->buffer workgroups by 64
#define MS_DISPATCH_LOCAL_SIZE 64
#if !defined(__cplusplus)
vec3 CalcCubeCoord(vec2 uv, int face)
+120
View File
@@ -0,0 +1,120 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "glsl_globals.h"
layout(local_size_x = MS_DISPATCH_LOCAL_SIZE, local_size_y = 1, local_size_z = 1) in;
layout(binding = 2, std430) readonly buffer srcBuf
{
uint srcData[];
};
layout(binding = 3) writeonly uniform uimage2DMSArray dstMS;
layout(push_constant) uniform multisamplePush
{
int texWidth;
int sliceOffset;
int sampleOffset;
int byteSize;
int maxInvocationID;
}
mscopy;
#define texWidth (mscopy.texWidth)
#define sliceOffset (mscopy.sliceOffset)
#define sampleOffset (mscopy.sampleOffset)
#define byteSize (mscopy.byteSize)
#define maxInvocationID (mscopy.maxInvocationID)
void main()
{
uint idx = gl_GlobalInvocationID.x;
int slice = sliceOffset;
int sampleIdx = sampleOffset;
if(int(idx) >= maxInvocationID)
{
return;
}
uvec4 data;
if(byteSize == 1)
{
data.x = srcData[idx];
int pxIdx = int(idx * 4);
int x0 = (pxIdx + 0) % texWidth;
int y0 = (pxIdx + 0) / texWidth;
int x1 = (pxIdx + 1) % texWidth;
int y1 = (pxIdx + 1) / texWidth;
int x2 = (pxIdx + 2) % texWidth;
int y2 = (pxIdx + 2) / texWidth;
int x3 = (pxIdx + 3) % texWidth;
int y3 = (pxIdx + 3) / texWidth;
imageStore(dstMS, ivec3(x0, y0, slice), sampleIdx, uvec4((data.x >> 0) & 0xFF, 0, 0, 0));
imageStore(dstMS, ivec3(x1, y1, slice), sampleIdx, uvec4((data.x >> 8) & 0xFF, 0, 0, 0));
imageStore(dstMS, ivec3(x2, y2, slice), sampleIdx, uvec4((data.x >> 16) & 0xFF, 0, 0, 0));
imageStore(dstMS, ivec3(x3, y3, slice), sampleIdx, uvec4((data.x >> 24) & 0xFF, 0, 0, 0));
}
else if(byteSize == 2)
{
data.x = srcData[idx];
int pxIdx = int(idx * 2);
int x0 = (pxIdx + 0) % texWidth;
int y0 = (pxIdx + 0) / texWidth;
int x1 = (pxIdx + 1) % texWidth;
int y1 = (pxIdx + 1) / texWidth;
imageStore(dstMS, ivec3(x0, y0, slice), sampleIdx, uvec4((data.x >> 0) & 0xFFFF, 0, 0, 0));
imageStore(dstMS, ivec3(x1, y1, slice), sampleIdx, uvec4((data.x >> 16) & 0xFFFF, 0, 0, 0));
}
else if(byteSize == 4)
{
int x0 = int(idx) % texWidth;
int y0 = int(idx) / texWidth;
data.x = srcData[idx];
imageStore(dstMS, ivec3(x0, y0, slice), sampleIdx, data);
}
else if(byteSize == 8)
{
int x0 = int(idx) % texWidth;
int y0 = int(idx) / texWidth;
data.x = srcData[idx * 2];
data.y = srcData[idx * 2 + 1];
imageStore(dstMS, ivec3(x0, y0, slice), sampleIdx, data);
}
else if(byteSize == 16)
{
int x0 = int(idx) % texWidth;
int y0 = int(idx) / texWidth;
data.x = srcData[idx * 4];
data.y = srcData[idx * 4 + 1];
data.z = srcData[idx * 4 + 2];
data.w = srcData[idx * 4 + 3];
imageStore(dstMS, ivec3(x0, y0, slice), sampleIdx, data);
}
}
+113
View File
@@ -0,0 +1,113 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "glsl_globals.h"
layout(binding = 2, std430) readonly buffer srcBuf
{
uint srcData[];
};
layout(push_constant) uniform multisamplePush
{
int numMultiSamples;
int format;
int currentSlice;
uint currentStencil;
int textureWidth;
int textureHeight;
}
mscopy;
#define MAX_D16 ((1 << 16) - 1)
#define MAX_D24 ((1 << 24) - 1)
#define D16ToFloat(depth) (float(depth) / float(MAX_D16))
#define D24ToFloat(depth) (float(depth) / float(MAX_D24))
#define numMultiSamples (mscopy.numMultiSamples)
#define format (mscopy.format)
#define currentSlice (mscopy.currentSlice)
#define currentStencil (mscopy.currentStencil)
#define textureWidth (mscopy.textureWidth)
#define textureHeight (mscopy.textureHeight)
void main()
{
ivec3 srcCoord =
ivec3(int(gl_FragCoord.x), int(gl_FragCoord.y), currentSlice *numMultiSamples + gl_SampleID);
uint idx = srcCoord.x + textureWidth * (srcCoord.y + (textureHeight * srcCoord.z));
float depth = 0;
uint stencil = 0;
if(format == SHADER_D16_UNORM)
{
uint data = srcData[idx / 2];
if(idx % 2 == 0)
{
depth = D16ToFloat(data & 0xFFFF);
}
else
{
depth = D16ToFloat((data >> 16) & 0xFFFF);
}
}
else if(format == SHADER_D16_UNORM_S8_UINT)
{
uint data = srcData[idx];
depth = D16ToFloat(data & 0xFFFF);
stencil = (data >> 16) & 0xFF;
}
else if(format == SHADER_X8_D24_UNORM_PACK32)
{
uint data = srcData[idx];
depth = D24ToFloat(data & 0xFFFFFF);
}
else if(format == SHADER_D24_UNORM_S8_UINT)
{
uint data = srcData[idx];
depth = D24ToFloat(data & 0xFFFFFF);
stencil = (data >> 24) & 0xFF;
}
else if(format == SHADER_D32_SFLOAT)
{
uint data = srcData[idx];
depth = uintBitsToFloat(data);
}
else if(format == SHADER_D32_SFLOAT_S8_UINT)
{
uint data = srcData[idx * 2];
depth = uintBitsToFloat(data);
uint stencilData = srcData[idx * 2 + 1];
stencil = stencilData & 0xFF;
}
if(currentStencil < 256u)
{
if(stencil != currentStencil)
discard;
}
gl_FragDepth = depth;
}
+116
View File
@@ -0,0 +1,116 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "glsl_globals.h"
#extension GL_EXT_samplerless_texture_functions : require
layout(local_size_x = MS_DISPATCH_LOCAL_SIZE, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0) uniform texture2DMSArray srcDepthMS;
layout(binding = 1) uniform utexture2DMSArray srcStencilMS;
layout(binding = 2, std430) writeonly buffer dstBuf
{
uint result[];
};
#define MAX_D16 ((1 << 16) - 1)
#define MAX_D24 ((1 << 24) - 1)
#define floatToD16(depth) uint(MAX_D16 *depth)
#define floatToD24(depth) uint(MAX_D24 *depth)
layout(push_constant) uniform multisamplePush
{
int textureWidth;
int baseSlice;
int baseSample;
int format;
int maxInvocationID;
}
mscopy;
#define textureWidth (mscopy.textureWidth)
#define baseSlice (mscopy.baseSlice)
#define baseSample (mscopy.baseSample)
#define format (mscopy.format)
#define maxInvocationID (mscopy.maxInvocationID)
void main()
{
int slice = baseSlice;
int sampleIdx = baseSample;
uint idx = gl_GlobalInvocationID.x;
if(int(idx) >= maxInvocationID)
{
return;
}
int x0 = int(idx) % textureWidth;
int y0 = int(idx) / textureWidth;
ivec3 coord = ivec3(x0, y0, slice);
// For D16, we need to sample 2 pixels at a time
if(format == SHADER_D16_UNORM)
{
int pxIdx = int(idx * 2);
int x0 = (pxIdx + 0) % textureWidth;
int y0 = (pxIdx + 0) / textureWidth;
int x1 = (pxIdx + 1) % textureWidth;
int y1 = (pxIdx + 1) / textureWidth;
vec2 depth = vec2(texelFetch(srcDepthMS, ivec3(x0, y0, slice), sampleIdx).x,
texelFetch(srcDepthMS, ivec3(x1, y1, slice), sampleIdx).x);
result[idx] = (floatToD16(depth.x) << 0) | (floatToD16(depth.y) << 16);
}
else if(format == SHADER_D16_UNORM_S8_UINT)
{
float depth = texelFetch(srcDepthMS, coord, sampleIdx).x;
uint stencil = texelFetch(srcStencilMS, coord, sampleIdx).x;
result[idx] = (floatToD16(depth) << 0) | (stencil << 16);
}
else if(format == SHADER_X8_D24_UNORM_PACK32)
{
float depth = texelFetch(srcDepthMS, coord, sampleIdx).x;
result[idx] = (floatToD24(depth) << 0);
}
else if(format == SHADER_D24_UNORM_S8_UINT)
{
float depth = texelFetch(srcDepthMS, coord, sampleIdx).x;
uint stencil = texelFetch(srcStencilMS, coord, sampleIdx).x;
result[idx] = (floatToD24(depth) << 0) | (stencil << 24);
}
else if(format == SHADER_D32_SFLOAT)
{
float depth = texelFetch(srcDepthMS, coord, sampleIdx).x;
result[idx] = floatBitsToUint(depth);
}
else if(format == SHADER_D32_SFLOAT_S8_UINT)
{
float depth = texelFetch(srcDepthMS, coord, sampleIdx).x;
uint stencil = texelFetch(srcStencilMS, coord, sampleIdx).x;
result[idx * 2 + 0] = floatBitsToUint(depth);
result[idx * 2 + 1] = stencil;
}
}
+120
View File
@@ -0,0 +1,120 @@
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "glsl_globals.h"
#extension GL_EXT_samplerless_texture_functions : require
layout(local_size_x = MS_DISPATCH_LOCAL_SIZE, local_size_y = 1, local_size_z = 1) in;
layout(binding = 0) uniform utexture2DMSArray srcMS;
// binding = 1 used as stencil read in the depth-stencil copy fragment shaders
layout(binding = 2, std430) writeonly buffer dstBuf
{
uint result[];
};
layout(push_constant) uniform multisamplePush
{
int textureWidth;
int baseSlice;
int baseSample;
int byteSize;
int maxInvocationID;
}
mscopy;
#define textureWidth (mscopy.textureWidth)
#define baseSlice (mscopy.baseSlice)
#define baseSample (mscopy.baseSample)
#define byteSize (mscopy.byteSize)
#define maxInvocationID (mscopy.maxInvocationID)
void main()
{
int slice = baseSlice;
int sampleIdx = baseSample;
uint idx = gl_GlobalInvocationID.x;
if(int(idx) >= maxInvocationID)
{
return;
}
// for byteSize < 4, we sample multiple pixels to fill a single output buffer element
if(byteSize == 1)
{
int pxIdx = int(idx * 4);
int x0 = (pxIdx + 0) % textureWidth;
int y0 = (pxIdx + 0) / textureWidth;
int x1 = (pxIdx + 1) % textureWidth;
int y1 = (pxIdx + 1) / textureWidth;
int x2 = (pxIdx + 2) % textureWidth;
int y2 = (pxIdx + 2) / textureWidth;
int x3 = (pxIdx + 3) % textureWidth;
int y3 = (pxIdx + 3) / textureWidth;
uvec4 data = uvec4(texelFetch(srcMS, ivec3(x0, y0, slice), sampleIdx).x,
texelFetch(srcMS, ivec3(x1, y1, slice), sampleIdx).x,
texelFetch(srcMS, ivec3(x2, y2, slice), sampleIdx).x,
texelFetch(srcMS, ivec3(x3, y3, slice), sampleIdx).x);
result[idx] = (data.x << 0 | data.y << 8 | data.z << 16 | data.w << 24);
}
else if(byteSize == 2)
{
int pxIdx = int(idx * 2);
int x0 = (pxIdx + 0) % textureWidth;
int y0 = (pxIdx + 0) / textureWidth;
int x1 = (pxIdx + 1) % textureWidth;
int y1 = (pxIdx + 1) / textureWidth;
uvec2 data = uvec2(texelFetch(srcMS, ivec3(x0, y0, slice), sampleIdx).x,
texelFetch(srcMS, ivec3(x1, y1, slice), sampleIdx).x);
result[idx] = (data.x << 0 | data.y << 16);
}
else if(byteSize == 4)
{
int x0 = int(idx) % textureWidth;
int y0 = int(idx) / textureWidth;
uint data = texelFetch(srcMS, ivec3(x0, y0, slice), sampleIdx).x;
result[idx] = data;
}
else if(byteSize == 8)
{
int x0 = int(idx) % textureWidth;
int y0 = int(idx) / textureWidth;
uvec2 data = texelFetch(srcMS, ivec3(x0, y0, slice), sampleIdx).xy;
result[idx * 2] = data.x;
result[idx * 2 + 1] = data.y;
}
else if(byteSize == 16)
{
int x0 = int(idx) % textureWidth;
int y0 = int(idx) / textureWidth;
uvec4 data = texelFetch(srcMS, ivec3(x0, y0, slice), sampleIdx);
result[idx * 4] = data.x;
result[idx * 4 + 1] = data.y;
result[idx * 4 + 2] = data.z;
result[idx * 4 + 3] = data.w;
}
}
+4
View File
@@ -174,6 +174,10 @@ RESOURCE_glsl_glsl_globals_h TYPE_EMBED "glsl/glsl_globals.h"
RESOURCE_glsl_texremap_frag TYPE_EMBED "glsl/texremap.frag"
RESOURCE_glsl_shaderdebug_sample_vert TYPE_EMBED "glsl/shaderdebug_sample.vert"
RESOURCE_glsl_discard_frag TYPE_EMBED "glsl/discard.frag"
RESOURCE_glsl_vk_ms2buffer_comp TYPE_EMBED "glsl/vk_ms2buffer.comp"
RESOURCE_glsl_vk_depthms2buffer_comp TYPE_EMBED "glsl/vk_depthms2buffer.comp"
RESOURCE_glsl_vk_buffer2ms_comp TYPE_EMBED "glsl/vk_buffer2ms.comp"
RESOURCE_glsl_vk_depthbuf2ms_frag TYPE_EMBED "glsl/vk_depthbuf2ms.frag"
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
+4
View File
@@ -60,6 +60,10 @@
#define RESOURCE_glsl_pixelhistory_primid_frag 444
#define RESOURCE_glsl_shaderdebug_sample_vert 445
#define RESOURCE_glsl_discard_frag 446
#define RESOURCE_glsl_vk_ms2buffer_comp 447
#define RESOURCE_glsl_vk_depthms2buffer_comp 448
#define RESOURCE_glsl_vk_buffer2ms_comp 449
#define RESOURCE_glsl_vk_depthbuf2ms_frag 450
// Next default values for new objects
//