Shaders
In Hello Triangle we met shaders briefly: small programs that run on the GPU, one stage of the pipeline each, once per vertex or once per pixel. A shader is a function from inputs to outputs and nothing more. It can't call another shader or keep anything between one invocation and the next; everything it knows arrives through its inputs, and everything it does leaves through its outputs. This chapter looks at the language we write them in, HLSL, and at the two ways data gets into them from our program.
HLSL
The
A GLSL shader is a file that starts with a #version line, declares global in, out and uniform variables, and has a main function. An HLSL file is organised differently:
- There is no version line. The shader model is chosen when the shader is compiled, by the
target we pass:vs_5_0for a vertex shader,ps_5_0for a pixel shader. - There is no
main. Each shader is an ordinary function with a name we choose, itsentry point , and one file can hold several of them. Ours always hold a vertex shader calledvs_mainand a pixel shader calledps_main. - Inputs and outputs are not global variables. They are the function's parameters and return value, and each is tagged with a
semantic that says what it is. - Values that are the same for a whole draw, GLSL's uniforms, are grouped into
constant buffers , declared withcbuffer.
In outline, an HLSL file looks like this:
cbuffer name : register(b0)
{
type constant_name;
};
struct vs_out
{
float4 position : SV_Position;
type out_name : SOME_SEMANTIC;
};
vs_out vs_main(type in_name : INPUT_SEMANTIC)
{
vs_out o;
// compute o.position and the other outputs from the inputs
return o;
}
float4 ps_main(vs_out p) : SV_Target
{
// compute the pixel's colour
}
Inputs to a vertex shader are also called
Types
HLSL has C's basic scalar types and a few of its own: bool, int, uint, float, double, and half, nominally a lower-precision float, which shader model 5 simply treats as a float. On top of those it has vectors and matrices, which we use all the time. Matrices wait for the Transformations chapter.
Vectors
A vector type is a scalar type followed by its number of components, from 1 to 4:
float2,float3,float4: vectors of floats, the counterpart of GLSL'svec2tovec4, and what we use almost always.int3,uint4,bool2and so on for the other scalar types, GLSL'sivec,uvecandbvec.
The components are named .x, .y, .z and .w, or, when the vector holds a colour, .r, .g, .b and .a. (GLSL's third set, stpq, does not exist in HLSL.) Both sets support
float2 a = float2(0.5, 0.7);
float4 b = a.xyxx; // (0.5, 0.7, 0.5, 0.5)
float3 c = b.zyw; // (0.5, 0.7, 0.5)
float4 d = a.xxxx + c.yxzy;
float4 e = float4(a, 0.0, 1.0);
float4 f = float4(e.rgb, 1.0);
The two sets can't be mixed in one swizzle, and we can't name a component the vector doesn't have, such as .z of a float2. Constructors take any mixture of scalars and vectors as long as the component count comes out right, as the last two lines show. That is exactly how our vertex shaders extend a position to four components: float4(position, 1.0).
float4 to a float3 silently drops the last component (the compiler only warns, "implicit truncation"), and a scalar assigned to a vector fills every component. Both are handy on purpose and confusing by accident, so read the compiler's warnings.
Ins and outs
Shaders are only useful as part of the pipeline, so each needs inputs and outputs that connect to the stages around it. GLSL connects them by name: an out in the vertex shader feeds the in of the same name and type in the fragment shader. HLSL connects them by
At the start, the vertex shader's inputs are filled from vertex buffers, and the semantics are the link: the input layout we create on the CPU names a semantic for each attribute, and the attribute goes to the vertex shader input with that semantic. We saw that in Hello Triangle, where POSITION joined the two. Semantics like POSITION and COLOR are names we pick; only the ones starting with SV_, the
At the other end, the pixel shader must produce a colour. It returns a float4 marked SV_Target, which goes to the bound render target.
In between, the vertex shader's outputs travel through the rasterizer to the pixel shader's inputs, matched by semantic. A vertex shader always outputs SV_Position, the clip-space position the rasterizer needs, and can output more alongside it. The tidiest way to write this is one struct for the vertex shader's output, used again as the pixel shader's input, so the two sides cannot disagree. Here is this lesson's shader file, shaders.hlsl, apart from its constant buffer:
struct vs_in
{
float3 position : POSITION;
float3 color : COLOR;
};
// What the vertex shader hands on. Anything beyond SV_Position is
// interpolated across the triangle on its way to the pixel shader.
struct vs_out
{
float4 position : SV_Position;
float3 color : COLOR;
};
vs_out vs_main(vs_in v)
{
vs_out o;
o.position = float4(v.position, 1.0);
o.color = v.color;
return o;
}
The inputs are a struct too this time, vs_in, with two attributes: a position and a colour. The vertex shader copies the colour through unchanged into its output, under the semantic COLOR, and the pixel shader receives it:
float4 ps_main(vs_out p) : SV_Target
{
return float4(p.color * tint.rgb, 1.0);
}
Ignore tint for a moment; it is the constant buffer we come to next. What matters here is p.color: a value computed per vertex, handed to the pixel shader. The pixel shader also receives p.position. By the time it gets there it is no longer in clip space: SV_Position in a pixel shader holds the pixel's own coordinates in the render target, which can be handy and which we use in later chapters.
Constant buffers
Vertex attributes give each vertex its own values. Often we want the opposite: one value that every vertex and pixel in a draw sees, such as the time, a colour for the whole object, or later a transformation matrix. GLSL calls these cbuffer, whose values come from a buffer object we fill on the CPU. This lesson's has a single member, a colour to tint everything with:
cbuffer frame : register(b0)
{
float4 tint;
};
register(b0) places it in constant buffer slot 0. That number is how our program and the shader find each other. OpenGL looks uniforms up by name with
; Mirrors `cbuffer frame` in the shader, register for register.
frame_constants: struct = {
tint: vec4
}
This is the first type our own program declares. A rasm struct is written as rin writes one, a field per line with its type, and is laid out as C would lay it out, since the data file declares it in C. vec4 is a structure of four f32 floats, x to w, from includes/math.rasm, so frame_constants is 16 bytes, one register. With one member the layouts trivially agree, but they won't always be so simple. HLSL packs a constant buffer into 16-byte float3 followed by a float share one register, but a float3 followed by another float3 leaves a gap of 4 bytes between them, and our structure has to leave the same gap. Later lessons add explicit padding fields for this.
Creating and filling the buffer
The buffer itself is created like the vertex buffers, with different settings. The lesson asks for it in one call:
; A dynamic constant buffer: the CPU may rewrite it. Its size must be a
; multiple of 16 bytes, the size of one HLSL register.
invoke(create_constant_buffer, sizeof(frame_constants))
mov constants, rax
and the procedure, with learn/gpu.rasm, where the later lessons find it:
; create_constant_buffer's
create_constant_buffer_desc: D3D11_BUFFER_DESC = {
.Usage = D3D11_USAGE.D3D11_USAGE_DYNAMIC,
.BindFlags = D3D11_BIND_FLAG.D3D11_BIND_CONSTANT_BUFFER,
.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG.D3D11_CPU_ACCESS_WRITE,
}
create_constant_buffer_out: *ID3D11Buffer = {}
; ecx: size -> rax: ID3D11Buffer
;
; A constant buffer the CPU rewrites, usually every frame. Its size must be a
; multiple of 16 bytes, the size of one HLSL register.
create_constant_buffer: proc(size: u32) -> *ID3D11Buffer = {
sub rsp, 40 ; 32 shadow, and rsp back on 16
mov create_constant_buffer_desc.ByteWidth, ecx
vcall(device, ID3D11DeviceVtbl.CreateBuffer, create_constant_buffer_desc&, 0, create_constant_buffer_out&)
assert_hr()
mov rax, create_constant_buffer_out
add rsp, 40
ret
}
D3D11_BIND_CONSTANT_BUFFER says what it is for. Our vertex buffers were IMMUTABLE, fixed at creation; this one is DYNAMIC, with CPU write access, because we will rewrite it every frame. Everything but the size is known before the program runs, so it is all in the initialiser, and the procedure stores only ByteWidth. In an include, a procedure's globals carry its name, create_constant_buffer_desc, since every global of a program shares one namespace with every other. There is no initial data (the 0 in place of a D3D11_SUBRESOURCE_DATA), since we fill it before every draw. macros.rasm, is test eax, eax, jns past an int 3: it stops in the debugger if the call failed.
Each frame, the lesson computes a tint that pulses between black and green over time, the same sin trick LearnOpenGL uses, and copies it into the buffer:
; tint = (0, sin(t) / 2 + 0.5, 0, 1)
movss xmm0, current_frame
call sinf
mulss xmm0, 0.5
addss xmm0, 0.5
mov c.tint.x, 0
movss c.tint.y, xmm0
mov c.tint.z, 0
mov c.tint.w, 0x3F800000 ; 1.0, as bits: a float immediate is written as its bits
invoke(update_buffer, constants, c&, sizeof(frame_constants))
\(\sin\) swings between -1 and 1, so halving it and adding a half gives a green channel that moves smoothly between 0 and 1. c is a frame_constants in the data section, c: frame_constants = {}; each field is written separately, the computed one from xmm0. These are the first floating-point constants in our code, and rasm treats them in two ways:
- A
float literal is memory . SSE has no instruction that takes a float immediate, somulss xmm0, 0.5reads a 0.5 that rasm places in read-only data, once for the whole program however often it is written;movss xmm0, 1.0loads a 1.0 the same way. - A float stored as an
immediate is written as its bits .mov c.tint.w, 0x3F800000stores the 32 bits of 1.0, andmov c.tint.x, 0stores 0.0, which is all zero bits. The field is a float, butmovonly moves four bytes.
math.rasm's: with no C library to supply one, it hands the angle to the x87 unit, which still has a sine instruction, fsin. Its argument and its result are both in xmm0, as the Win64 convention has it for a float. current_frame is the time in seconds since the program started, from a small learn/window.rasm that plays the part of
; -> xmm0: seconds since the window was created: what glfwGetTime is to
; LearnOpenGL. Stopped at --time while taking a --shot.
get_time: proc() -> f32 = {
sub rsp, 40 ; 32 shadow, and rsp back on 16
cmp window_shot_path[0], 0
je running
movss xmm0, window_shot_time
jmp done
running:
invoke(QueryPerformanceCounter, window_time_now&)
mov rax, window_time_now
sub rax, window_perf_start
cvtsi2sd xmm0, rax
cvtsi2sd xmm1, window_perf_frequency
divsd xmm0, xmm1
cvtsd2ss xmm0, xmm0
done:
add rsp, 40
ret
}
cvtsi2sd, divsd), since a count of a few billion doesn't fit a float's 24 bits of precision, and only then rounded to the float the lessons use. The count is written to window_time_now, a 64-bit global that is --shot option, which the lessons use to take their screenshots at a fixed moment.
Back to filling the buffer. That is learn/gpu.rasm:
update_buffer_mapped: D3D11_MAPPED_SUBRESOURCE = {}
; rcx: buffer, rdx: data, r8: size
;
; WRITE_DISCARD hands back fresh memory each time, so the GPU never waits on
; the CPU, and a draw already recorded keeps the values it saw.
update_buffer: proc(buffer: *ID3D11Buffer, data: *const void, size: u64) = {
push rbx
push rsi
push rdi
sub rsp, 48 ; Map takes 6: 32 + 16; entry 8 + three pushes 24 + 48 lands on 16
mov rbx, rcx ; buffer
mov rsi, rdx ; data
mov rdi, r8 ; size
vcall(context, ID3D11DeviceContextVtbl.Map, rbx, 0, D3D11_MAP.D3D11_MAP_WRITE_DISCARD, 0, update_buffer_mapped&)
assert_hr()
invoke(mem_copy, update_buffer_mapped.pData, rsi, rdi)
vcall(context, ID3D11DeviceContextVtbl.Unmap, rbx, 0)
add rsp, 48
pop rdi
pop rsi
pop rbx
ret
}
cstr.rasm, our rbx, rsi and rdi, which calls preserve; the procedure pushes them first, since its own caller expects them back. Three pushes and a sub rsp, 48 (32 of shadow space, 16 for rsp back onto 16, as the comment works out.
The D3D11_MAP_WRITE_DISCARD flag matters. The GPU runs behind the CPU, often by a frame or more, so when we map the buffer the GPU may not yet have run the draw that read its previous contents. Overwriting that memory in place would mean either waiting for the GPU or changing values under a draw that has not happened yet. "Discard" says we don't need the old contents at all, so the driver hands us a fresh piece of memory, and the earlier draw keeps the values it was given. This is how a buffer that changes every frame should be updated.
Binding it
The last step is to bind the buffer to slot 0, and here Direct3D differs from OpenGL in a way worth noticing. An OpenGL uniform belongs to a program object. A Direct3D constant buffer slot belongs to a stage: the vertex shader has its own slots, the pixel shader its own. Only our pixel shader reads tint, so we bind it there only:
vcall(context, ID3D11DeviceContextVtbl.PSSetConstantBuffers, 0, 1, constants&)
If the vertex shader needed it too,
The lesson draws its left triangle with the pulsing green tint, then writes plain white into the buffer, and draws the right triangle:
vcall(context, ID3D11DeviceContextVtbl.IASetVertexBuffers, 0, 1, left_vb&, stride&, offset&)
vcall(context, ID3D11DeviceContextVtbl.Draw, 3, 0)
movss xmm0, 1.0
movss c.tint.x, xmm0
movss c.tint.y, xmm0
movss c.tint.z, xmm0
invoke(update_buffer, constants, c&, sizeof(frame_constants))
vcall(context, ID3D11DeviceContextVtbl.IASetVertexBuffers, 0, 1, right_vb&, stride&, offset&)
vcall(context, ID3D11DeviceContextVtbl.Draw, 3, 0)
Mapping with discard between the two draws is safe for the same reason as before: the first draw keeps the green it was given, and the second sees white. The pixel shader multiplies each vertex colour by the tint. The left triangle's vertices are all white, so it shows the tint alone and fades between black and green; the right one is multiplied by white, so it shows its vertex colours unchanged.
More attributes!
The right triangle is the interesting one. Each of its vertices carries a colour of its own, as three more floats after the position:
; x, y, z, then r, g, b
; ...
right_triangle: [18]const f32 = {
0.1, -0.5, 0.0, 1.0, 0.0, 0.0,
0.9, -0.5, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 0.0, 0.0, 1.0,
}
Red at the bottom left, green at the bottom right, blue at the top. (The left triangle has the same layout, with white for every colour.) Each vertex is now six floats, 24 bytes, laid out as in LearnOpenGL's diagram:
The input layout gets a second element to match:
vertex_layout: [2]const D3D11_INPUT_ELEMENT_DESC = {
{ .SemanticName = "POSITION", .Format = DXGI_FORMAT.DXGI_FORMAT_R32G32B32_FLOAT, .AlignedByteOffset = 0 },
{ .SemanticName = "COLOR", .Format = DXGI_FORMAT.DXGI_FORMAT_R32G32B32_FLOAT, .AlignedByteOffset = 12 },
}
Both attributes are three floats. AlignedByteOffset says where each starts within a vertex: the position at byte 0, the colour 12 bytes in, right after the position's three floats. The semantic names match the ones in vs_in. The array is two elements long, and each element is initialised by its fields' names, the rest left zero as before. The distance from one vertex to the next, the
stride: u32 = 24
offset: u32 = 0
With the layout, the buffers and the shaders in place, this is what the program shows:
The left triangle's green rises and falls over time. The right one deserves a closer look: we gave it three colours, one per corner, and it shows a smooth blend of them across its whole surface. That is SV_Position is interpolated this way before the pixel shader sees it, which is why the comment on vs_out says so.
The triangle has three vertices, but tens of thousands of pixels, and every one of them got its own colour without us doing anything per pixel. This is the basic economy of the GPU: compute what you can per vertex and let interpolation spread it.
nointerpolation gives every pixel the value of one vertex, with no blending, which is what integer outputs such as an index need. noperspective blends linearly in screen space instead of correcting for perspective. The default, which we always use, is perspective-correct blending.
Our own shader helper
Hello Triangle compiled its two shaders and created its input layout by hand: read the file, call includes/learn/shader.rasm, just as LearnOpenGL writes its Shader class at this point. (The same goes for the window: everything Hello Window and Hello Triangle wrote out, from the window class to the swap chain, the rasterizer state and the viewport, is learn/window.rasm from this lesson on, and the frame loop is .hlsl file turns into:
shader: struct = {
vertex: *ID3D11VertexShader
geometry: *ID3D11GeometryShader ; 0 unless the file has a gs_main
pixel: *ID3D11PixelShader
layout: *ID3D11InputLayout ; 0 for shaders that make their own vertices
}
A vertex shader, a pixel shader, an optional geometry shader for a later chapter, and the input layout. Since Direct3D has no program object, this structure is the nearest thing: four objects that belong together, four pointers of 8 bytes each, each typed with the interface it points to, so the debugger shows which is which.
Reading and compiling
learn/filesystem.rasm, then compiles each entry point and creates its object. It fills in a shader that the caller owns, passed by address:
; rcx: shader, rdx: path relative to the repository root, r8: input layout
; (D3D11_INPUT_ELEMENT_DESC array, or 0), r9: how many elements it has
shader_create: proc(out: *shader, relative: *const c8, layout: *const D3D11_INPUT_ELEMENT_DESC, layout_count: u64) = {
push rbx
push rsi
push rdi
push r12
push r13
sub rsp, 48 ; CreateInputLayout takes 6: 32 + 16; entry 8 + five pushes 40 + 48 lands on 16
mov rbx, rcx ; the shader
mov r12, r8 ; the layout
mov r13, r9 ; its count
invoke(filesystem_path, rdx)
mov shader_create_path, rax
invoke(filesystem_read, rax, shader_create_size&)
test rax, rax
jnz have_source
invoke(print, "shader: cannot read %s\n", shader_create_path)
invoke(ExitProcess, 1)
have_source:
mov shader_create_source, rax
invoke(mem_zero, rbx, sizeof(shader))
invoke(shader_compile, shader_create_path, shader_create_source, shader_create_size, "vs_main", "vs_5_0")
mov shader_create_blob, rax
vcall(shader_create_blob, ID3D10BlobVtbl.GetBufferPointer)
mov rsi, rax
vcall(shader_create_blob, ID3D10BlobVtbl.GetBufferSize)
mov rdi, rax
lea rax, [rbx].shader.vertex
vcall(device, ID3D11DeviceVtbl.CreateVertexShader, rsi, rdi, 0, rax)
test r13, r13
jz no_layout
lea rax, [rbx].shader.layout
vcall(device, ID3D11DeviceVtbl.CreateInputLayout, r12, r13, rsi, rdi, rax)
test eax, eax
jns no_layout
invoke(print, "shader: the input layout does not match vs_main in %s\n", shader_create_path)
invoke(ExitProcess, 1)
no_layout:
release(shader_create_blob)
; ...
invoke(shader_compile, shader_create_path, shader_create_source, shader_create_size, "ps_main", "ps_5_0")
mov shader_create_blob, rax
vcall(shader_create_blob, ID3D10BlobVtbl.GetBufferPointer)
mov rsi, rax
vcall(shader_create_blob, ID3D10BlobVtbl.GetBufferSize)
mov rdi, rax
lea rax, [rbx].shader.pixel
vcall(device, ID3D11DeviceVtbl.CreatePixelShader, rsi, rdi, 0, rax)
release(shader_create_blob)
add rsp, 48
pop r13
pop r12
pop rdi
pop rsi
pop rbx
ret
}
The layout is passed as two arguments, the address of the array and its number of elements. A count of 0 means the shader reads no vertex buffers (some later shaders make their vertices up from SV_VertexID), and then no layout is created. Creating the layout is where Direct3D checks it against the vertex shader's inputs, as Hello Triangle explained, so a mismatch stops the program here with a message naming the file. The bytecode blobs are released once their shader objects exist, since nothing needs them after that; macros.rasm, vcall(object, IUnknownVtbl.Release). The part cut out in the middle does the same for gs_main if the file contains one, found by searching the source text for the name.
Each object is written straight into the caller's structure: lea rax, [rbx].shader.vertex computes the address of the vertex field of the structure rbx points to, which is how a field is named through a register, and that address is the method's last argument. It goes through rax rather than an argument register because rcx to r9 in order and would overwrite it on the way; rax is safe in any position. Everything the procedure must keep across its calls lives in a register that calls preserve: the shader in rbx, the layout and its count in r12 and r13, the bytecode's address and size in rsi and rdi. That is five pushes, so the frame is sub rsp, 48, as its comment works out. The rest, the path, the source and the blob, are globals named after the procedure:
; shader_create's
shader_create_path: *c8 = {}
shader_create_source: *u8 = {}
shader_create_size: u64 = 0
shader_create_blob: *ID3DBlob = {}
The compile step itself is Hello Triangle's, with three additions:
; rcx: path, rdx: source, r8: size, r9: entry point, [rsp + 40]: target -> rax: the compiled code
shader_compile: proc(path: *const c8, source: *const u8, size: u64, entry: *const c8, target: *const c8) -> *ID3DBlob = {
push rbx
sub rsp, 96 ; D3DCompile takes 11: 32 shadow + 7 * 8 = 88; entry 8 + push 8 + 96 lands on 16
mov rbx, rcx ; the path, for the messages
mov shader_compile_code, 0
mov shader_compile_errors, 0
; D3DCompile wants (source, size, path): every argument moves, and rcx is
; read last, after it has been overwritten. rbx holds the path instead.
; The target, the fifth argument, is at [rsp + 40] on entry: + 8 + 96.
invoke(D3DCompile, rdx, r8, rbx, 0, D3D_COMPILE_STANDARD_FILE_INCLUDE, r9, qword ptr [rsp + 144],
D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG, 0, shader_compile_code&, shader_compile_errors&)
test eax, eax
jns compiled
cmp shader_compile_errors, 0
je no_message
vcall(shader_compile_errors, ID3D10BlobVtbl.GetBufferPointer)
invoke(print, "shader: %s\n", rax)
invoke(ExitProcess, 1)
no_message:
invoke(print, "shader: %s\n", rbx)
invoke(ExitProcess, 1)
compiled:
cmp shader_compile_errors, 0
je done
release(shader_compile_errors) ; warnings
done:
mov rax, shader_compile_code
add rsp, 96
pop rbx
ret
}
- D3D_COMPILE_STANDARD_FILE_INCLUDE lets a shader use
#include, with paths relative to the file being compiled. It is the header's own constant, a special pointer value, 1. - D3DCOMPILE_ENABLE_STRICTNESS rejects old, deprecated syntax, and D3DCOMPILE_DEBUG keeps debug information in the bytecode so that graphics debuggers such as PIX or RenderDoc can show the HLSL source. The
|between them is folded when the program is built, into a single immediate. - The errors blob can be filled even when compilation succeeds, with warnings, so it is released either way.
The path is kept in rbx this time, which also serves the error message, so the procedure pushes rbx and the frame grows to 96: with the push, the 88 that rsp 8 off. The target is then 8 + 96 bytes further from rsp than at entry, at [rsp + 144]. On failure, the compiler's message already starts with the file name and line, so it is printed as it is and the program exits.
Using it
Binding a shader is four calls, wrapped in one:
; rcx: shader. Binds its stages and input layout.
shader_use: proc(s: *const shader) = {
push rbx
sub rsp, 32 ; 32 shadow; entry 8 + push 8 + 32 lands on 16
mov rbx, rcx
vcall(context, ID3D11DeviceContextVtbl.IASetInputLayout, [rbx].shader.layout)
vcall(context, ID3D11DeviceContextVtbl.VSSetShader, [rbx].shader.vertex, 0, 0)
vcall(context, ID3D11DeviceContextVtbl.GSSetShader, [rbx].shader.geometry, 0, 0)
vcall(context, ID3D11DeviceContextVtbl.PSSetShader, [rbx].shader.pixel, 0, 0)
add rsp, 32
pop rbx
ret
}
Binding a null geometry shader is not a mistake: it switches that stage off, so a shader without gs_main also clears out one that an earlier draw left bound. [rbx].shader.layout reads a field of the structure rbx points at, where C would write s->layout; the field's type gives the size, so no qword ptr is needed. Unlike LearnOpenGL's class, ours has no setters for uniforms: constant buffers are written as whole structs and bound by slot, as we just did, which needs no help from the shader.
The lesson then needs only two lines, one at startup and one each frame:
invoke(shader_create, s&, "src/1.getting_started/3.1.shaders/shaders.hlsl", vertex_layout&, 2)
invoke(shader_use, s&)
where s is a global, s: shader = {}.
The full program is here, and its shaders here.
Additional resources
- HLSL reference: Microsoft's language reference, including every intrinsic function.
- Semantics: the system values and how inputs and outputs are matched.
- Packing rules for constant variables: the 16-byte register rules, with examples.
- How to use dynamic resources:
Map with WRITE_DISCARD and its relatives. - LearnOpenGL's Shaders: the same chapter in GLSL.
Exercises
- Change the vertex shader so that both triangles are drawn upside down.
- Add a
float offsetto theframeconstant buffer and use it in the vertex shader to move the left triangle sideways. You will need to growframe_constantsto match (anf32field, then padding, such aspad: [3]f32, so its size stays a multiple of 16 bytes), and bind the buffer to the vertex shader stage as well, withVSSetConstantBuffers orgpu.rasm'sbind_constants . - Output the vertex position from the vertex shader under its own semantic and use it as the pixel colour. Both triangles turn into gradients. Why is the left triangle's bottom-left corner black?