Shader Memory Layout
Choose an example to predict function values, inspect input storage or follow a complete raymarch calculation to a pixel.
Follow the function
This overload illustrates a buffer of input fields. The byte map includes those fields only; the result and compiler-managed temporaries are excluded. Selecting a function preserves your layout profile and precision settings. HLSL reference ↗
Change numerical inputs to predict this function's values. Storage layout below describes its input fields separately. A function result alone does not define a screen pixel; Raymarch includes the full drawing context.
Layout rules
Logical buffer contents, not physical VRAM allocation or register usage. The selected profile controls layout; a pasted cbuffer wrapper does not change it. Native 16-bit mode corresponds to DXC -enable-16bit-types.
Define data
DXC legacy cbuffer · 24 B data + 24 B padding = 48 B layout span. Changes update automatically.
Inspect memory
| Metric | One block / element | Repeated data budget |
|---|---|---|
| Useful data | 24 B | 24 B |
| Padding | 24 B | 24 B |
| Layout span / stride | 48 B | 48 B |
Color = data per row: green ≥75%, yellow 50–<75%, red <50%. Hatching = padding; numbers = field IDs. Density alone does not establish possible savings. Partial structured rows count only actual bytes. Hover or click a byte to inspect it.
50% data · 24 B of 48 B · 24 B padding in the displayed layout. Field order has not been checked. Use Suggest field order to test whether the span can shrink.
Byte 0 · gain · float · 4-byte component at offset 0. Field gain.
| Field | Type | Offset | Data / extent | Align / stride |
|---|---|---|---|---|
| float | 0 | 4 / 4 | 4 Fits at the next aligned offset | |
| float4 | 16 | 16 / 16 | 4 value would cross a 16-byte row · 12 B before field. | |
| float | 32 | 4 / 4 | 4 Fits at the next aligned offset |
Compare & export
Suggest field order searches top-level permutations for up to eight fields; larger records use a bounded heuristic. It preserves types, array lengths and nested structure order. A smaller layout does not guarantee a faster shader. Update every CPU/GPU consumer when changing layout.
Selected declarations
cbuffer LayoutData
{
float gain;
float4 tint;
float bias;
};
From function to pixel
Predict how one pixel is calculated in a simple SDF sphere shader. Change the inputs and follow the ray, intermediate values and resulting color.
Float32 prediction · an ideal, isolated-pixel model of this fixed shader. Image and trace use the same calculation. GPU intrinsics, fused operations and compiler optimizations can change the result, especially near hit thresholds.
Open this tab to render the study.
Choose a pixel
Click the image or use X/Y controls. On the image, arrows move one pixel; Home returns to center. The frame uses top-left pixel coordinates and samples at (x + 0.5, y + 0.5).
Trace the computation
Select a stage to inspect its values and HLSL function. On a miss, shading uses sky and skips normal estimation. Source, draw and rasterizer nodes describe the reference graphics contract; the browser evaluates pixels on the CPU.
Complete shader behind the example
// From Function to Pixel — fixed SDF sphere study.
// Compile separately: dxc -T vs_6_0 -E VSMain Raymarch_Study.hlsl
// dxc -T ps_6_0 -E PSMain Raymarch_Study.hlsl
// Draw(3,0); fullscreen viewport matching resolution; cull/depth/blend OFF;
// one sample; R8G8B8A8_UNORM target (NOT SRGB: encoding is explicit).
// Bind StudyParameters to b0. Buffer fields are float32/uint32, little endian.
// CPU prediction rounds scalar operations to float32, without fused operations.
// Compiler reassociation and GPU intrinsic approximations can change results.
cbuffer StudyParameters : register(b0)
{
float3 cameraPosition;
float tanHalfFov;
float2 resolution;
uint maxSteps;
float hitEpsilon;
float3 sphereCenter;
float sphereRadius;
float3 lightDirection;
float lightIntensity;
float3 baseColor;
float exposure;
float maxDistance;
};
float4 VSMain(uint vertexID : SV_VertexID) : SV_Position
{
float2 uv = float2((vertexID << 1) & 2, vertexID & 2);
return float4(uv * float2(2, -2) + float2(-1, 1), 0, 1);
}
float SceneSDF(float3 p)
{
return length(p - sphereCenter) - sphereRadius;
}
float3 RayDirection(float2 pixelCenter)
{
float2 uv = float2(2 * pixelCenter.x / resolution.x - 1,
1 - 2 * pixelCenter.y / resolution.y);
float aspect = resolution.x / resolution.y;
return normalize(float3(uv.x * aspect * tanHalfFov,
uv.y * tanHalfFov, -1));
}
struct MarchHit
{
bool hit;
float t;
float3 position;
uint samples;
uint reason; // 1 hit, 2 distance limit, 3 step limit
};
MarchHit Raymarch(float3 ro, float3 rd)
{
MarchHit h = (MarchHit)0;
float t = 0;
[loop] for (uint i = 0; i < maxSteps; ++i)
{
if (t > maxDistance) { h.t = t; h.reason = 2; return h; }
float3 p = ro + rd * t;
float d = SceneSDF(p);
h.samples = i + 1;
if (d < hitEpsilon)
{
h.hit = true; h.t = t; h.position = p; h.reason = 1;
return h;
}
t += d;
}
h.t = t; h.reason = 3;
return h;
}
float3 SurfaceNormal(float3 p)
{
float e = max(hitEpsilon, 0.0001);
float3 gradient = float3(
SceneSDF(p + float3(e,0,0)) - SceneSDF(p - float3(e,0,0)),
SceneSDF(p + float3(0,e,0)) - SceneSDF(p - float3(0,e,0)),
SceneSDF(p + float3(0,0,e)) - SceneSDF(p - float3(0,0,e)));
return normalize(gradient);
}
float3 Shade(MarchHit h, float3 rd)
{
if (!h.hit)
return lerp(float3(0.02,0.03,0.06), float3(0.12,0.18,0.28),
saturate(0.5 * (rd.y + 1)));
float3 n = SurfaceNormal(h.position);
float diffuse = saturate(dot(n, normalize(lightDirection)));
return baseColor * (0.08 + lightIntensity * diffuse);
}
float EncodeSRGB(float x)
{
return x <= 0.0031308 ? 12.92 * x : 1.055 * pow(x, 1.0/2.4) - 0.055;
}
float3 ToDisplay(float3 linearColor)
{
float3 exposed = linearColor * exposure;
float3 mapped = saturate(exposed / (1 + exposed));
return float3(EncodeSRGB(mapped.r), EncodeSRGB(mapped.g), EncodeSRGB(mapped.b));
}
float4 PSMain(float4 pixelPosition : SV_Position) : SV_Target
{
float3 rd = RayDirection(pixelPosition.xy);
MarchHit hit = Raymarch(cameraPosition, rd);
float3 linearColor = Shade(hit, rd);
return float4(ToDisplay(linearColor), 1);
}
Fixed DXC legacy cbuffer contract: 84 B of fields, 96 B rounded layout span. API allocation granularity is separate. These are parameter bytes, not the temporaries or the image storage. Float fields are float32; maxSteps is uint32; all bytes below are little endian.
The model predicts this pixel’s calculation under the displayed assumptions. Sample and SDF-call counts describe that calculation; hardware timings, register allocation and scheduling depend on the target GPU.