futhark icon indicating copy to clipboard operation
futhark copied to clipboard

Futhark Does Not Wrap Defines / Constants

Open Niminem opened this issue 7 months ago • 4 comments

Reference: https://wiki.libsdl.org/SDL3/SDL_GPUShaderFormat

typedef Uint32 SDL_GPUShaderFormat;

#define SDL_GPU_SHADERFORMAT_INVALID  0
#define SDL_GPU_SHADERFORMAT_PRIVATE  (1u << 0) /**< Shaders for NDA'd platforms. */
#define SDL_GPU_SHADERFORMAT_SPIRV    (1u << 1) /**< SPIR-V shaders for Vulkan. */
#define SDL_GPU_SHADERFORMAT_DXBC     (1u << 2) /**< DXBC SM5_1 shaders for D3D12. */
#define SDL_GPU_SHADERFORMAT_DXIL     (1u << 3) /**< DXIL SM6_0 shaders for D3D12. */
#define SDL_GPU_SHADERFORMAT_MSL      (1u << 4) /**< MSL shaders for Metal. */
#define SDL_GPU_SHADERFORMAT_METALLIB (1u << 5) /**< Precompiled metallib shaders for Metal. */

Futhark will wrap SDL_GPUShaderFormat as a Uint32, however, none of the constants below. Why not? C2nim does this.

If this isn't some sort of bug- what do you recommend as a fix here? any ideas for this to be a more programmatic solution?

Niminem avatar May 18 '25 21:05 Niminem

In addition, here is something interesting:

when "SDL.gpu.device.create.verbose" is static:
  const
    SDL_PROP_GPU_DEVICE_CREATE_VERBOSE_BOOLEAN* = "SDL.gpu.device.create.verbose" ## Generated based on C:\Users\leonl\Desktop\Repositories\SDL3Bundle\vendored\SDL\include\SDL3/SDL_gpu.h:2292:9
else:
  let SDL_PROP_GPU_DEVICE_CREATE_VERBOSE_BOOLEAN* = "SDL.gpu.device.create.verbose"

The code above wraps this part of the C library:

extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_CreateGPUDeviceWithProperties(
    SDL_PropertiesID props);

#define SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN                 "SDL.gpu.device.create.debugmode"
#define SDL_PROP_GPU_DEVICE_CREATE_PREFERLOWPOWER_BOOLEAN            "SDL.gpu.device.create.preferlowpower"
#define SDL_PROP_GPU_DEVICE_CREATE_VERBOSE_BOOLEAN                   "SDL.gpu.device.create.verbose"
#define SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING                       "SDL.gpu.device.create.name"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_PRIVATE_BOOLEAN           "SDL.gpu.device.create.shaders.private"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN             "SDL.gpu.device.create.shaders.spirv"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOLEAN              "SDL.gpu.device.create.shaders.dxbc"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN              "SDL.gpu.device.create.shaders.dxil"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN               "SDL.gpu.device.create.shaders.msl"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN          "SDL.gpu.device.create.shaders.metallib"
#define SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING        "SDL.gpu.device.create.d3d12.semantic"
#define SDL_PROP_GPU_DEVICE_CREATE_VULKAN_SHADERCLIPDISTANCE_BOOLEAN "SDL.gpu.device.create.vulkan.shaderclipdistance"
#define SDL_PROP_GPU_DEVICE_CREATE_VULKAN_DEPTHCLAMP_BOOLEAN         "SDL.gpu.device.create.vulkan.depthclamp"
#define SDL_PROP_GPU_DEVICE_CREATE_VULKAN_DRAWINDIRECTFIRST_BOOLEAN  "SDL.gpu.device.create.vulkan.drawindirectfirstinstance"
#define SDL_PROP_GPU_DEVICE_CREATE_VULKAN_SAMPLERANISOTROPY_BOOLEAN  "SDL.gpu.device.create.vulkan.sampleranisotropy"

Idk what the difference is between this and the defines from my first comment. Thought it was worth mentioning

Niminem avatar May 18 '25 21:05 Niminem

Last comment PMunch sorry. Check this out:

when 0 is static:
  const
    SDL_GPU_SHADERFORMAT_INVALID* = 0 ## Generated based on C:\Users\leonl\Desktop\Repositories\SDL3Bundle\vendored\SDL\include\SDL3/SDL_gpu.h:1017:9
else:
  let SDL_GPU_SHADERFORMAT_INVALID* = 0 

SDL_GPU_SHADERFORMAT_INVALID is in the bindings as well. It's the first SDL_GPUShaderFormat value. But the others are not there.

Niminem avatar May 18 '25 21:05 Niminem

Short answer is that it's because Futhark and c2nim are implemented completely differently. Futhark relies on type information from clang, and defines don't have type information. Defines in C are preprocessor things that gets resolved before the C compiler starts trying to figure out the code.

Futhark does try to wrap some simple constructs though, which is what you're seeing with SDL_GPU_SHADERFORMAT_INVALID as it's just a literal 0. But more complex patterns like those bit shifts don't work. I am however planning on falling back to c2nim to parse defines if it is available on the system. But when I get the time to actually implement that I don't know.

PMunch avatar May 18 '25 21:05 PMunch

Interesting thanks PMunch. I think for now, I'll just need try to programmatically use c2nim on the header files separately then use a macro to find/copy/paste the wrapped defines from the produced Nim file into the Futhark generated wrapper. That should work

Niminem avatar May 18 '25 21:05 Niminem