Compiler1 min read

Globals Module

Shared engine-facing constants, enums, structs, and layout helpers.

Reading Time
1 min
Word Count
124
Sections
19
Try It Live

Turn the guide into code

Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.

Open Playground

Shared engine-facing constants, enums, structs, and layout helpers.

Import

bwsl
import Globals

This module imports: Math, PostFX.

Usage Examples

Share Engine ABI Types

Use Globals when several shaders need the same host-facing structs, constants, light layouts, material payloads, or vertex attribute flags.

bwsl
import Globals
resources {
camera: cbuffer<Globals::CameraUniforms>
material: cbuffer<Globals::BWEMaterial>
}
float4 worldPos = resources.camera.viewMatrix * float4(attributes.position, 1.0);
output.position = resources.camera.projectionMatrix * worldPos;
float3 albedo = resources.material.albedo.rgb;
float alpha = resources.material.albedo.a * (1.0 - resources.material.transparency);
output.color = float4(albedo, alpha);

Branch on Reflected Mesh Capabilities

Use the mesh and attribute helpers when one shader handles multiple vertex layouts and needs to guard optional normals, tangents, colors, or skinning data.

bwsl
import Globals
Globals::MeshDescriptor desc = resources.meshes[resources.meshIndex];
bool hasNormals = Globals::hasAttribute(desc, Globals::VertexAttribute::Normal);
bool hasColor = Globals::hasAttribute(desc, Globals::VertexAttribute::Color);
float3 normal = hasNormals ? normalize(input.normal) : float3(0.0, 1.0, 0.0);
float3 vertexColor = hasColor ? input.color.rgb : float3(1.0);
float lit = max(dot(normal, normalize(resources.lightDir)), 0.0);
output.color = float4(vertexColor * (0.2 + lit * 0.8), 1.0);

Constants

const int MAX_LIGHTS = 16const int MAX_CASCADES = 4const int MAX_BONES = 128

Structs

CameraUniforms

bwsl
struct CameraUniforms {
mat4 viewMatrix;
mat4 projectionMatrix;
float4 cameraPosition;
};

MeshDescriptor

bwsl
struct MeshDescriptor {
VertexAttribute attributeFlags;
uint _padding[3];
uint numVertices;
uint numIndices;
uint indexOffset;
uint positionOffset;
uint normalOffset;
uint texCoordOffset;
uint colorOffset;
uint tangentOffset;
uint bitangentOffset;
uint boneIndicesOffset;
uint boneWeightsOffset;
};

AttributeOffsetInfo

bwsl
struct AttributeOffsetInfo {
uint baseOffset;
uint stride;
uint elementCount;
uint present;
};

DrawMetadata

bwsl
struct DrawMetadata {
uint _pipelineState[2];
uint sectionId;
uint transformIndex;
uint materialIndex;
uint _padding;
};

BWEMaterial

bwsl
struct BWEMaterial {
float4 albedo;
float roughness;
float metallic;
float specular;
float ambientOcclusion;
float3 emissiveColor;
float emissiveIntensity;
float transparency;
float refractionIndex;
float anisotropy;
float subsurfaceScattering;
float2 albedoTextureTiling;
float2 albedoTextureOffset;
float padding[2];
};

LightSourcesSoA

bwsl
struct LightSourcesSoA {
float3 positions[MAX_LIGHTS];
float3 directions[MAX_LIGHTS];
float3 colors[MAX_LIGHTS];
float intensities[MAX_LIGHTS];
float constantAttenuations[MAX_LIGHTS];
float linearAttenuations[MAX_LIGHTS];
float quadraticAttenuations[MAX_LIGHTS];
float innerCutoffs[MAX_LIGHTS];
float outerCutoffs[MAX_LIGHTS];
float lengths[MAX_LIGHTS];
float radii[MAX_LIGHTS];
int types[MAX_LIGHTS];
int version;
};

CascadeData

bwsl
struct CascadeData {
mat4 viewProjMatrix;
float splitDistance;
float padding[3];
};

ShadowData

bwsl
struct ShadowData {
CascadeData cascades[MAX_CASCADES];
float4 lightDirection;
uint numCascades;
uint shadowMapSize;
float cascadeSplitLambda;
float shadowBias;
float shadowSlopeBias;
float shadowFilterRadius;
float padding[2];
};

Enums

LightType

bwsl
enum LightType : int {
Point = 0,
Directional = 1,
Line = 2,
Spot = 3,
Probe = 4,
Area = 5
}

VertexAttribute

bwsl
enum VertexAttribute : uint {
Position = 0b00000001,
Normal = 0b00000010,
TexCoord = 0b00000100,
Color = 0b00001000,
Tangent = 0b00010000,
Bitangent = 0b00100000,
BoneIndices = 0b01000000,
BoneWeights = 0b10000000,
// Check if a specific attribute is present
eval has :: (self, VertexAttribute flag) -> bool {
return (self & flag) != 0;
}
// Count how many attributes are set
eval count :: (self) -> int {
int c = 0;
eval if (self & Position) { c++; }
eval if (self & Normal) { c++; }
eval if (self & TexCoord) { c++; }
eval if (self & Color) { c++; }
eval if (self & Tangent) { c++; }
eval if (self & Bitangent) { c++; }
eval if (self & BoneIndices) { c++; }
eval if (self & BoneWeights) { c++; }
return c;
}
// Check if skinning data is available
eval hasSkinning :: (self) -> bool {
return (self & BoneIndices) != 0 && (self & BoneWeights) != 0;
}
// Check if tangent space is complete
eval hasTangentSpace :: (self) -> bool {
return (self & Normal) != 0 && (self & Tangent) != 0 && (self & Bitangent) != 0;
}
}

Functions

hasAttribute

bwsl
hasAttribute :: (MeshDescriptor desc, VertexAttribute attr) -> bool

decompressPosition_10_10_10

bwsl
decompressPosition_10_10_10 :: (uint packed) -> float3

decompressOctahedral16

bwsl
decompressOctahedral16 :: (uint packed) -> float3

decompressUV16_16

bwsl
decompressUV16_16 :: (uint packed) -> float2

decompressColor8888

bwsl
decompressColor8888 :: (uint packed) -> float4

decompressBoneIndices8888

bwsl
decompressBoneIndices8888 :: (uint packed) -> int4

decompressBoneWeights8888

bwsl
decompressBoneWeights8888 :: (uint packed) -> float4

gammaCorrect

bwsl
gammaCorrect :: (float3 color) -> float3

inverseGammaCorrect

bwsl
inverseGammaCorrect :: (float3 color) -> float3

linearToSRGB

bwsl
linearToSRGB :: (float3 color) -> float3

sRGBToLinear

bwsl
sRGBToLinear :: (float3 color) -> float3

ACESFilm

bwsl
ACESFilm :: (float3 x) -> float3

reinhardToneMap

bwsl
reinhardToneMap :: (float3 color) -> float3

reinhardExtendedToneMap

bwsl
reinhardExtendedToneMap :: (float3 color, float maxWhite) -> float3

Source

Generated from compiler-reference/modules/globals.bwsl.