/****************************************************************************** * The MIT License (MIT) * * Copyright (c) 2020-2023 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; int dispatchOffset; } mscopy; #define textureWidth (mscopy.textureWidth) #define baseSlice (mscopy.baseSlice) #define baseSample (mscopy.baseSample) #define byteSize (mscopy.byteSize) #define maxInvocationID (mscopy.maxInvocationID) #define dispatchOffset (mscopy.dispatchOffset) 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[dispatchOffset + 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[dispatchOffset + 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[dispatchOffset + 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[dispatchOffset + (idx * 2)] = data.x; result[dispatchOffset + (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[dispatchOffset + (idx * 4)] = data.x; result[dispatchOffset + (idx * 4 + 1)] = data.y; result[dispatchOffset + (idx * 4 + 2)] = data.z; result[dispatchOffset + (idx * 4 + 3)] = data.w; } }