Skip to content

Instantly share code, notes, and snippets.

@domportera
Created May 3, 2023 07:56
Show Gist options
  • Save domportera/83fc494899a5431ab6a8c8fb81b33219 to your computer and use it in GitHub Desktop.
Save domportera/83fc494899a5431ab6a8c8fb81b33219 to your computer and use it in GitHub Desktop.
Include this to have useful macros for defining kernel-specific variables that can aid in your compute-shadering
// Licensed under the accredited MIT License, Copyright Dom Portera
// For full license text, see LICENSE file in the repo root or https://opensource.org/licenses/MIT
#ifndef UTILITY_MACROS
#define UTILITY_MACROS
/**
* \brief reference the resolution variable generated by resolution def macro
* \param texName
* \param kernelName
*/
#define RESOLUTION(texName,kernelName) CONCAT5(texName,_,kernelName,_,resolution)
/**
* \brief calculates aspect ratio for textures generated by resolution macro
* \param texName
* \param kernelName
*/
#define ASPECT_RATIO(texName,kernelName) RESOLUTION(texName,kernelName).x/RESOLUTION(texName,kernelName).y
#define GET_MACRO_1_ARG(_1, _2, _3, _4, _5, NAME, ...) NAME
#define GET_MACRO_2_ARG(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define GET_MACRO_3_ARG(_1, _2, _3, _4, _5, _6, _7, NAME, ...) NAME
#define VAR_DEF(type, name) type name;
#define CONCATENATE_IMPL(s1, s2) s1##s2
#define CONCAT(s1, s2) CONCATENATE_IMPL(s1, s2)
#define CONCAT3(s1, s2, s3) CONCAT(CONCAT(s1, s2), s3)
#define CONCAT4(s1, s2, s3, s4) CONCAT(CONCAT(CONCAT(s1, s2), s3), s4)
#define CONCAT5(s1, s2, s3, s4, s5) CONCAT(CONCAT(CONCAT(CONCAT(s1, s2), s3), s4), s5)
/**
* \brief Defines delta time variables for the given kernels
*/
#define DELTA_TIME_DEF(...) GET_MACRO_1_ARG(__VA_ARGS__, DELTA_TIME_DEF_IMPL5, DELTA_TIME_DEF_IMPL4, DELTA_TIME_DEF_IMPL3, DELTA_TIME_DEF_IMPL2, DELTA_TIME_DEF_IMPL1)(__VA_ARGS__)
/**
* \brief references delta time variable generated by delta time def macro
* \param kernelName
*/
#define DELTA_TIME(kernelName) DELTA_TIME_INTERNAL(kernelName)
/**
* \brief Defines resolution variables for the given item for the given kernel names
* \n i.e. RESOLUTION_DEF(textureName, kernel1, kernel2, kernel3)` up to 5 kernels
*/
#define RESOLUTION_DEF(...) GET_MACRO_2_ARG(__VA_ARGS__, RESOLUTION_DEF_IMPL5, RESOLUTION_DEF_IMPL4, RESOLUTION_DEF_IMPL3, RESOLUTION_DEF_IMPL2, RESOLUTION_DEF_IMPL1)(__VA_ARGS__)
/**
* \brief references resolution variable generated by resolution def macro
* \param texName
* \param kernelName
*/
#define TEXTURE_DEF(...) GET_MACRO_3_ARG(__VA_ARGS__, TEXTURE_DEF_IMPL5, TEXTURE_DEF_IMPL4, TEXTURE_DEF_IMPL3, TEXTURE_DEF_IMPL2, TEXTURE_DEF_IMPL1)(__VA_ARGS__)
/**
* \brief Defines buffer length variables for the given item for the given kernel names
* \n i.e. BUFFER_LENGTH_DEF(bufferName, kernel1, kernel2, kernel3)` up to 5 kernels
*/
#define BUFFER_DEF(...) GET_MACRO_3_ARG(__VA_ARGS__, BUFFER_DEF_IMPL5, BUFFER_DEF_IMPL4, BUFFER_DEF_IMPL3, BUFFER_DEF_IMPL2, BUFFER_DEF_IMPL1)(__VA_ARGS__)
#define RESOLUTION_DEF_INTERNAL(texName, kernelName) float4 RESOLUTION(texName, kernelName);
#define RESOLUTION_DEF_IMPL1(texName, kernelName1) RESOLUTION_DEF_INTERNAL(texName, kernelName1)
#define RESOLUTION_DEF_IMPL2(texName, kernelName1, kernelName2) RESOLUTION_DEF_INTERNAL(texName, kernelName1) RESOLUTION_DEF_INTERNAL(texName, kernelName2)
#define RESOLUTION_DEF_IMPL3(texName, kernelName1, kernelName2, kernelName3) RESOLUTION_DEF_INTERNAL(texName, kernelName1) RESOLUTION_DEF_INTERNAL(texName, kernelName2) RESOLUTION_DEF_INTERNAL(texName, kernelName3)
#define RESOLUTION_DEF_IMPL4(texName, kernelName1, kernelName2, kernelName3, kernelName4) RESOLUTION_DEF_INTERNAL(texName, kernelName1) RESOLUTION_DEF_INTERNAL(texName, kernelName2) RESOLUTION_DEF_INTERNAL(texName, kernelName3) RESOLUTION_DEF_INTERNAL(texName, kernelName4)
#define RESOLUTION_DEF_IMPL5(texName, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5) RESOLUTION_DEF_INTERNAL(texName, kernelName1) RESOLUTION_DEF_INTERNAL(texName, kernelName2) RESOLUTION_DEF_INTERNAL(texName, kernelName3) RESOLUTION_DEF_INTERNAL(texName, kernelName4) RESOLUTION_DEF_INTERNAL(texName, kernelName5)
#define TEXTURE_DEF_IMPL1(type, name, kernelName1) \
VAR_DEF(type, name) RESOLUTION_DEF(name, kernelName1)
#define TEXTURE_DEF_IMPL2(type, name, kernelName1, kernelName2) \
VAR_DEF(type, name) RESOLUTION_DEF(name, kernelName1, kernelName2)
#define TEXTURE_DEF_IMPL3(type, name, kernelName1, kernelName2, kernelName3) \
VAR_DEF(type, name) RESOLUTION_DEF(name, kernelName1, kernelName2, kernelName3)
#define TEXTURE_DEF_IMPL4(type, name, kernelName1, kernelName2, kernelName3, kernelName4) \
VAR_DEF(type, name) RESOLUTION_DEF(name, kernelName1, kernelName2, kernelName3, kernelName4)
#define TEXTURE_DEF_IMPL5(type, name, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5) \
VAR_DEF(type, name) RESOLUTION_DEF(name, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5)
#define BUFFER_DEF_IMPL1(type, name, kernelName1) \
VAR_DEF(type, name) BUFFER_LENGTH_DEF(name, kernelName1);
#define BUFFER_DEF_IMPL2(type, name, kernelName1, kernelName2) \
VAR_DEF(type, name) BUFFER_LENGTH_DEF(name, kernelName1, kernelName2);
#define BUFFER_DEF_IMPL3(type, name, kernelName1, kernelName2, kernelName3) \
VAR_DEF(type, name) BUFFER_LENGTH_DEF(name, kernelName1, kernelName2, kernelName3);
#define BUFFER_DEF_IMPL4(type, name, kernelName1, kernelName2, kernelName3, kernelName4) \
VAR_DEF(type, name) BUFFER_LENGTH_DEF(name, kernelName1, kernelName2, kernelName3, kernelName4);
#define BUFFER_DEF_IMPL5(type, name, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5) \
VAR_DEF(type, name) BUFFER_LENGTH_DEF(name, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5);
/**
* \brief Defines buffer length variables for the given item for the given kernel names
* \n i.e. BUFFER_LENGTH_DEF(bufferName, kernel1, kernel2, kernel3)` up to 5 kernels
*/
#define BUFFER_LENGTH_DEF(...) GET_MACRO_2_ARG(__VA_ARGS__, BUFFER_LENGTH_DEF_IMPL5, BUFFER_LENGTH_DEF_IMPL4, BUFFER_LENGTH_DEF_IMPL3, BUFFER_LENGTH_DEF_IMPL2, BUFFER_LENGTH_DEF_IMPL1)(__VA_ARGS__)
#define BUFFER_LENGTH_INTERNAL(buffName, kernelName) CONCAT5(buffName,_,kernelName,_,length)
#define BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName) int BUFFER_LENGTH_INTERNAL(buffName, kernelName);
#define BUFFER_LENGTH_DEF_IMPL1(buffName, kernelName1) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName1)
#define BUFFER_LENGTH_DEF_IMPL2(buffName, kernelName1, kernelName2) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName1) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName2)
#define BUFFER_LENGTH_DEF_IMPL3(buffName, kernelName1, kernelName2, kernelName3) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName1) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName2) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName3)
#define BUFFER_LENGTH_DEF_IMPL4(buffName, kernelName1, kernelName2, kernelName3, kernelName4) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName1) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName2) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName3) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName4)
#define BUFFER_LENGTH_DEF_IMPL5(buffName, kernelName1, kernelName2, kernelName3, kernelName4, kernelName5) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName1) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName2) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName3) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName4) BUFFER_LENGTH_DEF_INTERNAL(buffName, kernelName5)
#define DELTA_TIME_INTERNAL(kernelName) CONCAT3(deltaTime, _, kernelName)
#define DELTA_TIME_DEF_INTERNAL(kernelName) float DELTA_TIME_INTERNAL(kernelName);
#define DELTA_TIME_DEF_IMPL1(kernelName1) DELTA_TIME_DEF_INTERNAL(kernelName1)
#define DELTA_TIME_DEF_IMPL2(kernelName1, kernelName2) DELTA_TIME_DEF_INTERNAL(kernelName1) DELTA_TIME_DEF_INTERNAL(kernelName2)
#define DELTA_TIME_DEF_IMPL3(kernelName1, kernelName2, kernelName3) DELTA_TIME_DEF_INTERNAL(kernelName1) DELTA_TIME_DEF_INTERNAL(kernelName2) DELTA_TIME_DEF_INTERNAL(kernelName3)
#define DELTA_TIME_DEF_IMPL4(kernelName1, kernelName2, kernelName3, kernelName4) DELTA_TIME_DEF_INTERNAL(kernelName1) DELTA_TIME_DEF_INTERNAL(kernelName2) DELTA_TIME_DEF_INTERNAL(kernelName3) DELTA_TIME_DEF_INTERNAL(kernelName4)
#define DELTA_TIME_DEF_IMPL5(kernelName1, kernelName2, kernelName3, kernelName4, kernelName5) DELTA_TIME_DEF_INTERNAL(kernelName1) DELTA_TIME_DEF_INTERNAL(kernelName2) DELTA_TIME_DEF_INTERNAL(kernelName3) DELTA_TIME_DEF_INTERNAL(kernelName4) DELTA_TIME_DEF_INTERNAL(kernelName5)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment