Skip to content

Commit

Permalink
Merge pull request KhronosGroup#2271 from KhronosGroup/fix-2264
Browse files Browse the repository at this point in the history
MSL: Handle volatile properly for emulated image atomics.
  • Loading branch information
HansKristian-Work authored Jan 23, 2024
2 parents bad97c8 + 9d87a01 commit 03b485d
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 6 deletions.
22 changes: 22 additions & 0 deletions reference/opt/shaders-msl/comp/coherent-image-atomic.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

kernel void main0(texture2d<uint> rw_spd_global_atomic [[texture(0)]], volatile device atomic_uint* rw_spd_global_atomic_atomic [[buffer(0)]])
{
uint _43 = atomic_fetch_add_explicit((volatile device atomic_uint*)&rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), rw_spd_global_atomic)], 1u, memory_order_relaxed);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

struct spvDescriptorSetBuffer1
{
texture2d<uint> rw_spd_global_atomic [[id(0)]];
volatile device atomic_uint* rw_spd_global_atomic_atomic [[id(1)]];
};

kernel void main0(constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
uint _43 = atomic_fetch_add_explicit((volatile device atomic_uint*)&spvDescriptorSet1.rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), spvDescriptorSet1.rw_spd_global_atomic)], 1u, memory_order_relaxed);
}

22 changes: 22 additions & 0 deletions reference/opt/shaders-msl/comp/coherent-image-atomic.msl2.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

kernel void main0(texture2d<uint> rw_spd_global_atomic [[texture(0)]], volatile device atomic_uint* rw_spd_global_atomic_atomic [[buffer(0)]])
{
uint _43 = atomic_fetch_add_explicit((volatile device atomic_uint*)&rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), rw_spd_global_atomic)], 1u, memory_order_relaxed);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

struct spvDescriptorSetBuffer1
{
texture2d<uint, access::read_write> rw_spd_global_atomic [[id(0)]];
};

kernel void main0(constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
uint _43 = spvDescriptorSet1.rw_spd_global_atomic.atomic_fetch_add(uint2(int2(0)), 1u).x;
}

15 changes: 15 additions & 0 deletions reference/opt/shaders-msl/comp/coherent-image-atomic.msl31.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

kernel void main0(texture2d<uint, access::read_write> rw_spd_global_atomic [[texture(0)]])
{
uint _43 = rw_spd_global_atomic.atomic_fetch_add(uint2(int2(0)), 1u).x;
}

38 changes: 38 additions & 0 deletions reference/shaders-msl/comp/coherent-image-atomic.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

static inline __attribute__((always_inline))
void SPD_IncreaseAtomicCounter(thread uint& spdCounter, texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint _25 = atomic_fetch_add_explicit((volatile device atomic_uint*)&rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), rw_spd_global_atomic)], 1u, memory_order_relaxed);
spdCounter = _25;
}

static inline __attribute__((always_inline))
void ComputeAutoExposure(texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint v = 0u;
uint param = v;
SPD_IncreaseAtomicCounter(param, rw_spd_global_atomic, rw_spd_global_atomic_atomic);
v = param;
}

kernel void main0(texture2d<uint> rw_spd_global_atomic [[texture(0)]], volatile device atomic_uint* rw_spd_global_atomic_atomic [[buffer(0)]])
{
ComputeAutoExposure(rw_spd_global_atomic, rw_spd_global_atomic_atomic);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

struct spvDescriptorSetBuffer1
{
texture2d<uint> rw_spd_global_atomic [[id(0)]];
volatile device atomic_uint* rw_spd_global_atomic_atomic [[id(1)]];
};

static inline __attribute__((always_inline))
void SPD_IncreaseAtomicCounter(thread uint& spdCounter, texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint _25 = atomic_fetch_add_explicit((volatile device atomic_uint*)&rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), rw_spd_global_atomic)], 1u, memory_order_relaxed);
spdCounter = _25;
}

static inline __attribute__((always_inline))
void ComputeAutoExposure(texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint v = 0u;
uint param = v;
SPD_IncreaseAtomicCounter(param, rw_spd_global_atomic, rw_spd_global_atomic_atomic);
v = param;
}

kernel void main0(constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
ComputeAutoExposure(spvDescriptorSet1.rw_spd_global_atomic, spvDescriptorSet1.rw_spd_global_atomic_atomic);
}

38 changes: 38 additions & 0 deletions reference/shaders-msl/comp/coherent-image-atomic.msl2.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

// The required alignment of a linear texture of R32Uint format.
constant uint spvLinearTextureAlignmentOverride [[function_constant(65535)]];
constant uint spvLinearTextureAlignment = is_function_constant_defined(spvLinearTextureAlignmentOverride) ? spvLinearTextureAlignmentOverride : 4;
// Returns buffer coords corresponding to 2D texture coords for emulating 2D texture atomics
#define spvImage2DAtomicCoord(tc, tex) (((((tex).get_width() + spvLinearTextureAlignment / 4 - 1) & ~( spvLinearTextureAlignment / 4 - 1)) * (tc).y) + (tc).x)

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

static inline __attribute__((always_inline))
void SPD_IncreaseAtomicCounter(thread uint& spdCounter, texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint _25 = atomic_fetch_add_explicit((volatile device atomic_uint*)&rw_spd_global_atomic_atomic[spvImage2DAtomicCoord(int2(0), rw_spd_global_atomic)], 1u, memory_order_relaxed);
spdCounter = _25;
}

static inline __attribute__((always_inline))
void ComputeAutoExposure(texture2d<uint> rw_spd_global_atomic, volatile device atomic_uint* rw_spd_global_atomic_atomic)
{
uint v = 0u;
uint param = v;
SPD_IncreaseAtomicCounter(param, rw_spd_global_atomic, rw_spd_global_atomic_atomic);
v = param;
}

kernel void main0(texture2d<uint> rw_spd_global_atomic [[texture(0)]], volatile device atomic_uint* rw_spd_global_atomic_atomic [[buffer(0)]])
{
ComputeAutoExposure(rw_spd_global_atomic, rw_spd_global_atomic_atomic);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

struct spvDescriptorSetBuffer1
{
texture2d<uint, access::read_write> rw_spd_global_atomic [[id(0)]];
};

static inline __attribute__((always_inline))
void SPD_IncreaseAtomicCounter(thread uint& spdCounter, texture2d<uint, access::read_write> rw_spd_global_atomic)
{
uint _25 = rw_spd_global_atomic.atomic_fetch_add(uint2(int2(0)), 1u).x;
spdCounter = _25;
}

static inline __attribute__((always_inline))
void ComputeAutoExposure(texture2d<uint, access::read_write> rw_spd_global_atomic)
{
uint v = 0u;
uint param = v;
SPD_IncreaseAtomicCounter(param, rw_spd_global_atomic);
v = param;
}

kernel void main0(constant spvDescriptorSetBuffer1& spvDescriptorSet1 [[buffer(1)]])
{
ComputeAutoExposure(spvDescriptorSet1.rw_spd_global_atomic);
}

32 changes: 32 additions & 0 deletions reference/shaders-msl/comp/coherent-image-atomic.msl31.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wunused-variable"

#include <metal_stdlib>
#include <simd/simd.h>
#include <metal_atomic>

using namespace metal;

constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(256u, 1u, 1u);

static inline __attribute__((always_inline))
void SPD_IncreaseAtomicCounter(thread uint& spdCounter, texture2d<uint, access::read_write> rw_spd_global_atomic)
{
uint _25 = rw_spd_global_atomic.atomic_fetch_add(uint2(int2(0)), 1u).x;
spdCounter = _25;
}

static inline __attribute__((always_inline))
void ComputeAutoExposure(texture2d<uint, access::read_write> rw_spd_global_atomic)
{
uint v = 0u;
uint param = v;
SPD_IncreaseAtomicCounter(param, rw_spd_global_atomic);
v = param;
}

kernel void main0(texture2d<uint, access::read_write> rw_spd_global_atomic [[texture(0)]])
{
ComputeAutoExposure(rw_spd_global_atomic);
}

20 changes: 20 additions & 0 deletions shaders-msl/comp/coherent-image-atomic.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450

layout (set = 1, binding = 0, r32ui) coherent uniform uimage2D rw_spd_global_atomic;

void SPD_IncreaseAtomicCounter(inout uint spdCounter)
{
spdCounter = imageAtomicAdd(rw_spd_global_atomic, ivec2(0,0), 1);
}

void ComputeAutoExposure() {
uint v = 0;
SPD_IncreaseAtomicCounter(v);
}

layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;

void main()
{
ComputeAutoExposure();
}
20 changes: 20 additions & 0 deletions shaders-msl/comp/coherent-image-atomic.msl2.argument.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450

layout (set = 1, binding = 0, r32ui) coherent uniform uimage2D rw_spd_global_atomic;

void SPD_IncreaseAtomicCounter(inout uint spdCounter)
{
spdCounter = imageAtomicAdd(rw_spd_global_atomic, ivec2(0,0), 1);
}

void ComputeAutoExposure() {
uint v = 0;
SPD_IncreaseAtomicCounter(v);
}

layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;

void main()
{
ComputeAutoExposure();
}
20 changes: 20 additions & 0 deletions shaders-msl/comp/coherent-image-atomic.msl2.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450

layout (set = 1, binding = 0, r32ui) coherent uniform uimage2D rw_spd_global_atomic;

void SPD_IncreaseAtomicCounter(inout uint spdCounter)
{
spdCounter = imageAtomicAdd(rw_spd_global_atomic, ivec2(0,0), 1);
}

void ComputeAutoExposure() {
uint v = 0;
SPD_IncreaseAtomicCounter(v);
}

layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;

void main()
{
ComputeAutoExposure();
}
20 changes: 20 additions & 0 deletions shaders-msl/comp/coherent-image-atomic.msl31.argument.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 450

layout (set = 1, binding = 0, r32ui) coherent uniform uimage2D rw_spd_global_atomic;

void SPD_IncreaseAtomicCounter(inout uint spdCounter)
{
spdCounter = imageAtomicAdd(rw_spd_global_atomic, ivec2(0,0), 1);
}

void ComputeAutoExposure() {
uint v = 0;
SPD_IncreaseAtomicCounter(v);
}

layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;

void main()
{
ComputeAutoExposure();
}
Loading

0 comments on commit 03b485d

Please sign in to comment.