Hi there!
Is there any straightforward way of having a boolean in a header file? I’m sharing a struct on both sides (CPU and GPU) and I would like to put it inside a boolean value. I tried importing Foundation.h
and using bool
but the compiler complains unsupported architecture
Any ideas?
I know that I can use an int = 0/1 but…
My C isn’t great, but I don’t believe that bool
is supported.
You could create an enum, being careful not to conflict with other names.
eg
typedef enum {
False, True
} boolean;
If anyone is better with C than I am, please join in
Thks! enum
is a more elegant solution!!!
I was trying this out and was surprised to see true
was builtin to Metal. I tried to add #include <metal_stdlib>
to Common.h
to use Metal’s builtin bool
but it failed. I’m guessing it’s only on the GPU? I also tried Metal/Metal.h
but that was not supported on my architecture.
Is there really no way to use the Metal bool
in Swift or common headers? The Metal Shading Language specifies that the bool
just expands to 0 and 1, but still…
I did some exploring. I think this works too –
To Common.h
add –
typedef signed char BOOL;
#define TRUE ((BOOL)1)
#define FALSE ((BOOL)0)
then declare your value in the struct as –
BOOL value
This creates an Obj-C Bool value. You can use Swift’s true
and false
in Swift and Metal’s true
and false
in Metal.
Found in this project: GitHub - MetalPetal/MetalPetal: A GPU accelerated image and video processing framework built on Metal.
Cool! Thank you very much!!!
Just to add — the standard metal project has a few nice macros that you can use for inspiration. They give you this one and I think one or two others:
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
I always have read macros are evil but seems critical to integrating swift/metal like killer shows. Personally, I had a situation that was driving me insane because metal pointers require an address space qualifier. A macro did the trick to avoid duplicating the struct.
Revisiting this as this seems to work and keeps copyright.
stdbool.h:
https://opensource.apple.com/source/xnu/xnu-1228/EXTERNAL_HEADERS/stdbool.h.auto.html
You can include this file in your project and then in Common.h do for example:
#import "stdbool.h"
typedef struct {
uint width;
uint height;
bool alphaTesting;
bool scissorTesting;
} Params;
I’ve come upon the same problem. The MSL spec clearly states in table 2.1 that bool is supported and I can send a buffer of bools to the shader, so the bridge is clearly the problem. I imaging bridging is for Obj-C and not for C++, it just usually works, but not for bool.
This is surely a major issue for Metal. Is Apple not fixing it? Like the absence of a random number generator as I describe elsewhere.
The stdbool.h solution above works but the problem for me is that I don’t understand what it is doing. What are int and _Bool unto in this code?
Can anyone explain in simple language what the stdbool.h solution does?
Thanks
My C is not great, so when I wanted a bool
, I fumbled about a bit to see how Apple did it. I came across the stdbool.h solution, probably in an old Apple sample code somewhere that I can’t find now.
Here’s a bit of history and explanation behind this:
I probably should have poked around and played with the code a bit more, but I wasn’t sure whether I would break things in future versions. Or maybe that was just how things worked when I first wrote the code ages ago.
You can replace stdbool.h with this code at the top of Common.h.
#ifndef __cplusplus
#define bool _Bool
#endif
That SO link explains what _Bool
is. I don’t know what the compiler is doing in the bridging header (maybe there’s someone out there that can fill us in?), but if you don’t have the ifndef
to make sure that you’re only compiling C, then it won’t compile. I guess because bool
is now part of C++, but in C, _Bool
is the keyword.
Serendipitously, this article dropped in my feed today:
It’s over my head, I’m afraid.
Thank you. That’s so much simpler.