GPU: Cleanup type aliases

This define all aliases for supported types,
document which one to use in C++ shared code,
move relevant defines to their backend file.

Rename `bool1` to `bool32_t` and cleanup
its usage as mentioned in #118961.

Rel. #118961

Pull Request: https://projects.blender.org/blender/blender/pulls/119098
This commit is contained in:
Clément Foucault 2024-03-08 19:09:10 +01:00
parent fbaf6219eb
commit 4205718dce
12 changed files with 122 additions and 127 deletions

@ -34,9 +34,9 @@ struct OCIO_GPUParameters {
float dither;
float scale;
float exponent;
bool1 use_predivide;
bool1 use_overlay;
bool1 use_hdr;
bool32_t use_predivide;
bool32_t use_overlay;
bool32_t use_hdr;
int _pad0;
int _pad1;
};

@ -20,21 +20,21 @@ layout(std140) uniform common_block
vec4 volCoordScale; /* To convert volume uvs to screen uvs */
float volHistoryAlpha;
float volShadowSteps;
bool volUseLights;
bool volUseSoftShadows;
bool32_t volUseLights;
bool32_t volUseSoftShadows;
/* Screen Space Reflections */
vec4 ssrParameters;
float ssrBorderFac;
float ssrMaxRoughness;
float ssrFireflyFac;
float ssrBrdfBias;
bool ssrToggle;
bool ssrefractToggle;
bool32_t ssrToggle;
bool32_t ssrefractToggle;
/* SubSurface Scattering */
float sssJitterThreshold;
bool sssToggle;
bool32_t sssToggle;
/* Specular */
bool specToggle;
bool32_t specToggle;
/* Lights */
int laNumLight;
/* Probes */

@ -39,21 +39,21 @@ struct CommonUniformBlock {
vec4 _volCoordScale; /* To convert volume uvs to screen uvs */
float _volHistoryAlpha;
float _volShadowSteps;
bool _volUseLights;
bool _volUseSoftShadows;
bool32_t _volUseLights;
bool32_t _volUseSoftShadows;
/* Screen Space Reflections */
vec4 _ssrParameters;
float _ssrBorderFac;
float _ssrMaxRoughness;
float _ssrFireflyFac;
float _ssrBrdfBias;
bool _ssrToggle;
bool _ssrefractToggle;
bool32_t _ssrToggle;
bool32_t _ssrefractToggle;
/* SubSurface Scattering */
float _sssJitterThreshold;
bool _sssToggle;
bool32_t _sssToggle;
/* Specular */
bool _specToggle;
bool32_t _specToggle;
/* Lights */
int _laNumLight;
/* Probes */
@ -166,14 +166,14 @@ struct LightBlock {
BLI_STATIC_ASSERT_ALIGN(LightBlock, 16)
struct RenderpassBlock {
bool _renderPassDiffuse;
bool _renderPassDiffuseLight;
bool _renderPassGlossy;
bool _renderPassGlossyLight;
bool _renderPassEmit;
bool _renderPassSSSColor;
bool _renderPassEnvironment;
bool _renderPassAOV;
bool32_t _renderPassDiffuse;
bool32_t _renderPassDiffuseLight;
bool32_t _renderPassGlossy;
bool32_t _renderPassGlossyLight;
bool32_t _renderPassEmit;
bool32_t _renderPassSSSColor;
bool32_t _renderPassEnvironment;
bool32_t _renderPassAOV;
uint _renderPassAOVActive;
};
BLI_STATIC_ASSERT_ALIGN(RenderpassBlock, 16)

@ -67,7 +67,7 @@ class DepthOfField {
/** Stabilization (flicker attenuation) of Color and CoC output of the setup pass. */
TextureFromPool stabilize_output_tx_ = {"dof_taa"};
GPUTexture *stabilize_input_ = nullptr;
bool1 stabilize_valid_history_ = false;
bool32_t stabilize_valid_history_ = false;
int3 dispatch_stabilize_size_ = int3(-1);
PassSimple stabilize_ps_ = {"Stabilize"};

@ -211,7 +211,7 @@ struct CameraData {
float _pad1;
float _pad2;
bool1 initialized;
bool32_t initialized;
#ifdef __cplusplus
/* Small constructor to allow detecting new buffers. */
@ -277,7 +277,7 @@ struct FilmData {
/** Scaling factor to convert texel to uvs. */
float2 extent_inv;
/** Is true if history is valid and can be sampled. Bypass history to resets accumulation. */
bool1 use_history;
bool32_t use_history;
/** Controlled by user in lookdev mode or by render settings. */
float background_opacity;
/** Output counts per type. */
@ -305,7 +305,7 @@ struct FilmData {
/** Storage type of the render-pass to be displayed. */
ePassStorageType display_storage_type;
/** True if we bypass the accumulation and directly output the accumulation buffer. */
bool1 display_only;
bool32_t display_only;
/** Start of AOVs and number of aov. */
int aov_color_id, aov_color_len;
int aov_value_id, aov_value_len;
@ -370,7 +370,7 @@ struct AOVsInfoData {
/** Id of the AOV to be displayed (from the start of the AOV array). -1 for combined. */
int display_id;
/** True if the AOV to be displayed is from the value accumulation buffer. */
bool1 display_is_value;
bool32_t display_is_value;
};
BLI_STATIC_ASSERT_ALIGN(AOVsInfoData, 16)
@ -426,7 +426,7 @@ struct VelocityGeometryIndex {
/** Offset inside #VelocityGeometryBuf for each time-step. Indexed using eVelocityStep. */
packed_int3 ofs;
/** If true, compute deformation motion blur. */
bool1 do_deform;
bool32_t do_deform;
/**
* Length of data inside #VelocityGeometryBuf for each time-step.
* Indexed using eVelocityStep.
@ -1137,7 +1137,7 @@ struct Surfel {
/** Cluster this surfel is assigned to. */
int cluster_id;
/** True if the light can bounce or be emitted by the surfel back face. */
bool1 double_sided;
bool32_t double_sided;
int _pad0;
int _pad1;
int _pad2;
@ -1152,9 +1152,9 @@ struct CaptureInfoData {
/** Number of surfels inside the surfel buffer or the needed len. */
packed_int3 irradiance_grid_size;
/** True if the surface shader needs to write the surfel data. */
bool1 do_surfel_output;
bool32_t do_surfel_output;
/** True if the surface shader needs to increment the surfel_len. */
bool1 do_surfel_count;
bool32_t do_surfel_count;
/** Number of surfels inside the surfel buffer or the needed len. */
uint surfel_len;
/** Total number of a ray for light transportation. */
@ -1187,12 +1187,12 @@ struct CaptureInfoData {
/** Radius of surfels. */
float surfel_radius;
/** Capture options. */
bool1 capture_world_direct;
bool1 capture_world_indirect;
bool1 capture_visibility_direct;
bool1 capture_visibility_indirect;
bool1 capture_indirect;
bool1 capture_emission;
bool32_t capture_world_direct;
bool32_t capture_world_indirect;
bool32_t capture_visibility_direct;
bool32_t capture_visibility_indirect;
bool32_t capture_indirect;
bool32_t capture_emission;
int _pad0;
/* World light probe atlas coordinate. */
SphereProbeUvArea world_atlas_coord;
@ -1327,9 +1327,9 @@ struct RayTraceData {
float roughness_mask_scale;
float roughness_mask_bias;
/** If set to true will bypass spatial denoising. */
bool1 skip_denoise;
bool32_t skip_denoise;
/** If set to false will bypass tracing for refractive closures. */
bool1 trace_refraction;
bool32_t trace_refraction;
/** Closure being ray-traced. */
int closure_index;
int _pad0;
@ -1456,8 +1456,8 @@ BLI_STATIC_ASSERT_ALIGN(PlanarProbeDisplayData, 16)
struct PipelineInfoData {
float alpha_hash_scale;
bool1 is_probe_reflection;
bool1 use_combined_lightprobe_eval;
bool32_t is_probe_reflection;
bool32_t use_combined_lightprobe_eval;
float _pad2;
};
BLI_STATIC_ASSERT_ALIGN(PipelineInfoData, 16)

@ -134,9 +134,9 @@ BLI_STATIC_ASSERT_ALIGN(gpLight, 16)
struct gpObject {
/** Weather or not to apply lighting to the GPencil object. */
bool1 is_shadeless;
bool32_t is_shadeless;
/** Switch between 2d and 3D stroke order. */
bool1 stroke_order3d;
bool32_t stroke_order3d;
/** Offset inside the layer buffer to the first layer data of this object. */
uint layer_offset;
/** Offset inside the material buffer to the first material data of this object. */

@ -340,7 +340,7 @@ class PassBase {
* IMPORTANT: Will keep a reference to the data and dereference it upon drawing. Make sure data
* still alive until pass submission.
*
* \note bool reference version is expected to take bool1 reference which is aliased to int.
* \note bool reference version is expected to take bool32_t reference which is aliased to int.
*/
void push_constant(const char *name, const float &data);
void push_constant(const char *name, const float2 &data);

@ -37,8 +37,8 @@ struct NodeLinkData {
/* bezierPts Is actually a float2, but due to std140 each element needs to be aligned to 16
* bytes. */
float4 bezierPts[4];
bool1 doArrow;
bool1 doMuted;
bool32_t doArrow;
bool32_t doMuted;
float dim_factor;
float thickness;
float4 dash_params;
@ -64,8 +64,8 @@ struct GPencilStrokeData {
int xraymode;
int caps_start;
int caps_end;
bool1 keep_size;
bool1 fill_stroke;
bool32_t keep_size;
bool32_t fill_stroke;
float2 _pad;
};
BLI_STATIC_ASSERT_ALIGN(struct GPencilStrokeData, 16)

@ -25,16 +25,19 @@
*
* IMPORTANT: Do not forget to align mat4, vec3 and vec4 to 16 bytes, and vec2 to 8 bytes.
*
* NOTE: You can use bool type using bool1 a int boolean type matching the GLSL type.
* NOTE: You can use bool type using bool32_t a int boolean type matching the GLSL type.
*/
#ifdef GPU_SHADER
/* Silence macros when compiling for shaders. */
# define BLI_STATIC_ASSERT(cond, msg)
# define BLI_STATIC_ASSERT_ALIGN(type_, align_)
# define BLI_STATIC_ASSERT_SIZE(type_, size_)
# define ENUM_OPERATORS(a, b)
/* Incompatible keywords. */
# define static
# define inline
/* Math function renaming. */
# define cosf cos
# define sinf sin
# define tanf tan
@ -46,71 +49,43 @@
# define sqrtf sqrt
# define expf exp
# define bool1 bool
/* Type name collision with Metal shading language - These type-names are already defined. */
# ifndef GPU_METAL
# define float2 vec2
# define float3 vec3
# define float3x4 mat3x4
# define float4 vec4
# define float4x4 mat4
# define int2 ivec2
# define int3 ivec3
# define int4 ivec4
# define uint2 uvec2
# define uint3 uvec3
# define uint4 uvec4
# define bool2 bvec2
# define bool3 bvec3
# define bool4 bvec4
# define packed_float3 vec3
# define packed_int3 int3
# endif
#else /* C / C++ */
# pragma once
# include "BLI_assert.h"
# ifdef __cplusplus
# include "BLI_math_matrix_types.hh"
# include "BLI_math_vector_types.hh"
# include "BLI_math_matrix_types.hh"
# include "BLI_math_vector_types.hh"
using bool32_t = int32_t;
// using bool2 = blender::int2; /* Size is not consistent across backend. */
// using bool3 = blender::int3; /* Size is not consistent across backend. */
// using bool4 = blender::int4; /* Size is not consistent across backend. */
using blender::float2;
using blender::float3;
using blender::float3x4;
using blender::float4;
using blender::float4x4;
using blender::int2;
using blender::int3;
using blender::int4;
using blender::uint2;
using blender::uint3;
using blender::uint4;
using bool1 = int;
using bool2 = blender::int2;
using bool3 = blender::int3;
using bool4 = blender::int4;
/** IMPORTANT: Do not use in shared struct. Use packed_(float/int/uint)3 instead.
* Here for static functions usage only. */
using blender::float3;
using blender::int3;
using blender::uint3;
/** Packed types are needed for MSL which have different alignment rules for float3. */
using packed_float3 = blender::float3;
using packed_int3 = blender::int3;
using packed_uint3 = blender::uint3;
# else /* C */
typedef float float2[2];
typedef float float3[3];
typedef float float4[4];
typedef float float4x4[4][4];
typedef float float3x4[3][4];
typedef int int2[2];
typedef int int3[2];
typedef int int4[4];
typedef uint uint2[2];
typedef uint uint3[3];
typedef uint uint4[4];
typedef int bool1;
typedef int bool2[2];
typedef int bool3[2];
typedef int bool4[4];
typedef float3 packed_float3;
typedef int3 packed_int3;
# endif
using blender::float2x2;
// using blender::float3x2; /* Does not follow alignment rules of GPU. */
using blender::float4x2;
// using blender::float2x3; /* Does not follow alignment rules of GPU. */
// using blender::float3x3; /* Does not follow alignment rules of GPU. */
// using blender::float4x3; /* Does not follow alignment rules of GPU. */
using blender::float2x4;
using blender::float3x4;
using blender::float4x4;
#endif

@ -1355,7 +1355,7 @@ bool MTLShader::generate_msl_from_glsl(const shader::ShaderCreateInfo *info)
ss_fragment << "float2 gl_PointCoord;" << std::endl;
}
if (msl_iface.uses_gl_FrontFacing) {
ss_fragment << "MTLBOOL gl_FrontFacing;" << std::endl;
ss_fragment << "bool gl_FrontFacing;" << std::endl;
}
if (msl_iface.uses_gl_PrimitiveID) {
ss_fragment << "uint gl_PrimitiveID;" << std::endl;
@ -2616,7 +2616,7 @@ std::string MSLGeneratorInterface::generate_msl_fragment_inputs_string()
}
if (this->uses_gl_FrontFacing) {
out << parameter_delimiter(is_first_parameter)
<< "\n\tconst MTLBOOL gl_FrontFacing [[front_facing]]";
<< "\n\tconst bool gl_FrontFacing [[front_facing]]";
}
if (this->uses_gl_PrimitiveID) {
out << parameter_delimiter(is_first_parameter)

@ -22,6 +22,11 @@
#define DFDY_SIGN 1.0
/* Type definitions. */
/* int implicitly cast to bool in MSL. */
using bool32_t = int32_t;
using vec3_1010102_Unorm = uint32_t;
using vec3_1010102_Inorm = int32_t;
/* GLSL types aliases. */
using vec2 = float2;
using vec3 = float3;
using vec4 = float4;
@ -36,7 +41,6 @@ using mat4x3 = float4x3;
using mat4x4 = float4x4;
using mat2 = float2x2;
using mat3 = float3x3;
using mat3x4 = float3x4;
using mat4 = float4x4;
using ivec2 = int2;
using ivec3 = int3;
@ -44,27 +48,9 @@ using ivec4 = int4;
using uvec2 = uint2;
using uvec3 = uint3;
using uvec4 = uint4;
/* MTLBOOL is used for native boolean's generated by the Metal backend, to avoid type-emulation
* for GLSL booleans, which are treated as integers. */
#define MTLBOOL bool
#define bool int
#define bvec2 bool2
#define bvec3 bool3
#define bvec4 bool4
#define vec3_1010102_Unorm uint
#define vec3_1010102_Inorm int
/* Strip GLSL Decorators. */
/* NOTE: For debugging, keep decorators in to ensure we have not missed any
* shader resources which are not guarded behind Macro's. */
#if 0
# define in
# define flat
# define smooth
# define noperspective
# define layout(std140) struct
# define uniform
#endif
using bvec2 = bool2;
using bvec3 = bool3;
using bvec4 = bool4;
/* Compute decorators. */
#define TG threadgroup
@ -2323,7 +2309,7 @@ template<typename T> vec<T, 2> mix(vec<T, 2> a, vec<T, 2> b, bvec2 mask)
}
/* Overload for mix(A, B, bvec<N>). */
template<typename T> T mix(T a, T b, MTLBOOL mask)
template<typename T> T mix(T a, T b, bool mask)
{
return (mask) ? b : a;
}

@ -2,6 +2,40 @@
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** Type aliases. */
/** IMPORTANT: Be warry of size and alignment matching for types that are present
* in C++ shared code. */
/* Boolean in GLSL are 32bit in interface structs. */
#define bool32_t bool
#define bool2 bvec2
#define bool3 bvec3
#define bool4 bvec4
#define float2 vec2
#define float3 vec3
#define float4 vec4
#define int2 ivec2
#define int3 ivec3
#define int4 ivec4
#define uint2 uvec2
#define uint3 uvec3
#define uint4 uvec4
/* GLSL already follows the packed alignement / size rules for vec3. */
#define packed_float3 float3
#define packed_int3 int3
#define packed_uint3 uint3
#define float2x2 mat2x2
#define float3x2 mat3x2
#define float4x2 mat4x2
#define float2x3 mat2x3
#define float3x3 mat3x3
#define float4x3 mat4x3
#define float2x4 mat2x4
#define float3x4 mat3x4
#define float4x4 mat4x4
/* Fast store variant macro. In GLSL this is the same as imageStore, but assumes no bounds
* checking. */
#define imageStoreFast imageStore