forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opengl] Do not use macros in GLSL codegen (taichi-dev#3369)
* a * fix * hmm * hmm
- Loading branch information
Showing
4 changed files
with
94 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,38 @@ | ||
// vim: ft=glsl | ||
// clang-format off | ||
#include "taichi/util/macros.h" | ||
|
||
#ifdef TI_INSIDE_OPENGL_CODEGEN | ||
#define OPENGL_BEGIN_ATOMIC_F32_DEF constexpr auto kOpenGLAtomicF32SourceCode = | ||
#define OPENGL_END_ATOMIC_F32_DEF ; | ||
#else | ||
#ifndef TI_INSIDE_OPENGL_CODEGEN | ||
static_assert(false, "Do not include"); | ||
#define OPENGL_BEGIN_ATOMIC_F32_DEF | ||
#define OPENGL_END_ATOMIC_F32_DEF | ||
#endif | ||
|
||
OPENGL_BEGIN_ATOMIC_F32_DEF | ||
"#define DEFINE_ATOMIC_F32_FUNCTIONS(NAME) " | ||
STR( | ||
float atomicAdd_##NAME##_f32(int addr, float rhs) { | ||
int old, new, ret; | ||
do { | ||
old = _##NAME##_i32_[addr]; | ||
new = floatBitsToInt((intBitsToFloat(old) + rhs)); | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); | ||
return intBitsToFloat(old); | ||
} | ||
float atomicSub_##NAME##_f32(int addr, float rhs) { | ||
int old, new, ret; | ||
do { | ||
old = _##NAME##_i32_[addr]; | ||
new = floatBitsToInt((intBitsToFloat(old) - rhs)); | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); | ||
return intBitsToFloat(old); | ||
} | ||
float atomicMax_##NAME##_f32(int addr, float rhs) { | ||
int old, new, ret; | ||
do { | ||
old = _##NAME##_i32_[addr]; | ||
new = floatBitsToInt(max(intBitsToFloat(old), rhs)); | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); | ||
return intBitsToFloat(old); | ||
} | ||
float atomicMin_##NAME##_f32(int addr, float rhs) { | ||
int old, new, ret; | ||
do { | ||
old = _##NAME##_i32_[addr]; | ||
new = floatBitsToInt(min(intBitsToFloat(old), rhs)); | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); | ||
return intBitsToFloat(old); | ||
} | ||
\n | ||
) | ||
OPENGL_END_ATOMIC_F32_DEF | ||
|
||
#undef OPENGL_BEGIN_ATOMIC_F32_DEF | ||
#undef OPENGL_END_ATOMIC_F32_DEF | ||
#define GENERATE_OPENGL_ATOMIC_F32(NAME) \ | ||
constexpr auto kOpenGlAtomicF32Source_##NAME = STR( \ | ||
float atomicAdd_##NAME##_f32(int addr, float rhs) { \ | ||
int old, new, ret; \ | ||
do { \ | ||
old = _##NAME##_i32_[addr]; \ | ||
new = floatBitsToInt((intBitsToFloat(old) + rhs)); \ | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); \ | ||
return intBitsToFloat(old); \ | ||
} float atomicSub_##NAME##_f32(int addr, float rhs) { \ | ||
int old, new, ret; \ | ||
do { \ | ||
old = _##NAME##_i32_[addr]; \ | ||
new = floatBitsToInt((intBitsToFloat(old) - rhs)); \ | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); \ | ||
return intBitsToFloat(old); \ | ||
} float atomicMax_##NAME##_f32(int addr, float rhs) { \ | ||
int old, new, ret; \ | ||
do { \ | ||
old = _##NAME##_i32_[addr]; \ | ||
new = floatBitsToInt(max(intBitsToFloat(old), rhs)); \ | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); \ | ||
return intBitsToFloat(old); \ | ||
} float atomicMin_##NAME##_f32(int addr, float rhs) { \ | ||
int old, new, ret; \ | ||
do { \ | ||
old = _##NAME##_i32_[addr]; \ | ||
new = floatBitsToInt(min(intBitsToFloat(old), rhs)); \ | ||
} while (old != atomicCompSwap(_##NAME##_i32_[addr], old, new)); \ | ||
return intBitsToFloat(old); \ | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,45 @@ | ||
// vim: ft=glsl | ||
// clang-format off | ||
#include "taichi/util/macros.h" | ||
|
||
constexpr auto kOpenGLReductionCommon = STR( | ||
shared float _reduction_temp_float[gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z]; | ||
shared int _reduction_temp_int[gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z]; | ||
shared uint _reduction_temp_uint[gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z]; | ||
float add(float a, float b) { return a + b; } | ||
int add(int a, int b) { return a + b; } | ||
uint add(uint a, uint b) { return a + b; } | ||
\n | ||
); | ||
shared float _reduction_temp_float[gl_WorkGroupSize.x * gl_WorkGroupSize.y * | ||
gl_WorkGroupSize.z]; | ||
shared int _reduction_temp_int[gl_WorkGroupSize.x * gl_WorkGroupSize.y * | ||
gl_WorkGroupSize.z]; | ||
shared uint _reduction_temp_uint[gl_WorkGroupSize.x * gl_WorkGroupSize.y * | ||
gl_WorkGroupSize.z]; | ||
float add(float a, float b) { return a + b; } int add(int a, int b) { | ||
return a + b; | ||
} uint add(uint a, uint b) { return a + b; } | ||
\n); | ||
|
||
#ifdef TI_INSIDE_OPENGL_CODEGEN | ||
#define OPENGL_BEGIN_REDUCTION_DEF constexpr auto kOpenGLReductionSourceCode = | ||
#define OPENGL_END_REDUCTION_DEF ; | ||
#else | ||
#ifndef TI_INSIDE_OPENGL_CODEGEN | ||
static_assert(false, "Do not include"); | ||
#define OPENGL_BEGIN_REDUCTION_DEF | ||
#define OPENGL_END_REDUCTION_DEF | ||
#endif | ||
|
||
OPENGL_BEGIN_REDUCTION_DEF | ||
"#define DEFINE_REDUCTION_FUNCTIONS(OP, TYPE) " | ||
STR( | ||
TYPE reduction_workgroup_##OP##_##TYPE##(in TYPE r) { | ||
_reduction_temp_##TYPE##[gl_LocalInvocationIndex] = r; | ||
barrier(); | ||
memoryBarrierShared(); | ||
const int group_size = int(gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z); | ||
const int depth = int(ceil(log2(float(group_size)))); | ||
for (int i = 0; i < depth; ++i) { | ||
const int radix = 1 << (i + 1); | ||
const int stride = 1 << i; | ||
const int cmp_index = int(gl_LocalInvocationIndex) + stride; | ||
if (gl_LocalInvocationIndex % radix == 0 && cmp_index < group_size) { | ||
_reduction_temp_##TYPE##[gl_LocalInvocationIndex] = ##OP##( | ||
_reduction_temp_##TYPE##[gl_LocalInvocationIndex], | ||
_reduction_temp_##TYPE##[cmp_index] | ||
); | ||
} | ||
barrier(); | ||
memoryBarrierShared(); | ||
} | ||
const TYPE result = _reduction_temp_##TYPE##[0]; | ||
barrier(); | ||
return result; | ||
} | ||
\n | ||
) | ||
OPENGL_END_REDUCTION_DEF | ||
|
||
#undef OPENGL_BEGIN_REDUCTION_DEF | ||
#undef OPENGL_END_REDUCTION_DEF | ||
#define GENERATE_OPENGL_REDUCTION_FUNCTIONS(OP, TYPE) \ | ||
constexpr auto kOpenGlReductionSource_##OP##_##TYPE = \ | ||
STR(TYPE reduction_workgroup_##OP##_##TYPE(in TYPE r) { \ | ||
_reduction_temp_##TYPE[gl_LocalInvocationIndex] = r; \ | ||
barrier(); \ | ||
memoryBarrierShared(); \ | ||
const int group_size = int(gl_WorkGroupSize.x * \ | ||
gl_WorkGroupSize.y * \gl_WorkGroupSize.z); \ | ||
const int depth = int(ceil(log2(float(group_size)))); \ | ||
for (int i = 0; i < depth; ++i) { \ | ||
const int radix = 1 << (i + 1); \ | ||
const int stride = 1 << i; \ | ||
const int cmp_index = int(gl_LocalInvocationIndex) + stride; \ | ||
if (gl_LocalInvocationIndex % radix == 0 && \ | ||
cmp_index < group_size) { \ | ||
_reduction_temp_##TYPE[gl_LocalInvocationIndex] = \ | ||
OP(_reduction_temp_##TYPE[gl_LocalInvocationIndex], \ | ||
_reduction_temp_##TYPE[cmp_index]); \ | ||
} \ | ||
barrier(); \ | ||
memoryBarrierShared(); \ | ||
} \ | ||
const TYPE result = _reduction_temp_##TYPE[0]; \ | ||
barrier(); \ | ||
return result; \ | ||
}); |