blending.rasm
src/4.advanced_d3d11/3.1.blending/blending.rasm · 291 lines
; 3.1 Blending
;
; Two kinds of see-through:
;
; grass each texel is either fully there or fully not. The pixel shader
; throws away (clip) pixels whose alpha is low, and what is left is
; ordinary opaque geometry: order does not matter
; windows partly transparent. The blend state mixes each new pixel with
; what is already in the target: src * a + dst * (1 - a)
;
; Blending reads what is behind, so the windows are drawn after everything
; opaque, from the farthest to the nearest. Drawn in the wrong order, a near
; window would be in the depth buffer first and hide the ones behind it.
;
; The grass and windows are sampled with clamp rather than wrap, so the top
; row of each image is not blended with its bottom row. In D3D a sampler is an
; object of its own, bound to a slot like a texture: this is a matter of
; binding a different one into s0 for those draws.
import "../../../includes/d3d11.rin"
import "../../../includes/calls.rasm"
import "../../../includes/macros.rasm"
import "../../../includes/math.rasm"
import "../../../includes/learn/window.rasm"
import "../../../includes/learn/shader.rasm"
import "../../../includes/learn/gpu.rasm"
import "../../../includes/learn/cube.rasm"
import "../../../includes/learn/camera.rasm"
import "../../../includes/learn/transforms.rasm"
plane_vertices: [48]const f32 = {
; position normal uv
-5.0, -0.5, 5.0, 0.0, 1.0, 0.0, 0.0, 2.0,
5.0, -0.5, 5.0, 0.0, 1.0, 0.0, 2.0, 2.0,
5.0, -0.5, -5.0, 0.0, 1.0, 0.0, 2.0, 0.0,
-5.0, -0.5, 5.0, 0.0, 1.0, 0.0, 0.0, 2.0,
5.0, -0.5, -5.0, 0.0, 1.0, 0.0, 2.0, 0.0,
-5.0, -0.5, -5.0, 0.0, 1.0, 0.0, 0.0, 0.0,
}
; A unit square standing on its bottom edge, facing +z: one blade of grass or
; one window.
quad_vertices: [48]const f32 = {
; position normal uv
-0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
-0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
-0.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
}
window_positions: [5]const vec3 = {
{ -1.5, -0.5, -0.48 },
{ 1.5, -0.5, 0.51 },
{ 0.0, -0.5, 0.7 },
{ -0.3, -0.5, -2.3 },
{ 0.5, -0.5, -0.6 },
}
grass_positions: [4]const vec3 = {
{ -2.2, -0.5, 0.4 },
{ 2.6, -0.5, 1.2 },
{ 0.6, -0.5, 1.4 },
{ -0.6, -0.5, -1.8 },
}
camera_start: const vec3 = { 0.0, 0.5, 4.5 }
cube_a: const vec3 = { -1.0, 0.0, -1.0 }
cube_b: const vec3 = { 2.0, 0.0, 0.0 }
clear_color: [4]const f32 = { 0.1, 0.1, 0.1, 1.0 }
t: transforms = {}
model: mat4 = {}
s: shader = {}
cam: camera = {}
cube_vb: *ID3D11Buffer = {}
plane_vb: *ID3D11Buffer = {}
quad_vb: *ID3D11Buffer = {}
transforms_buffer: *ID3D11Buffer = {}
cube_texture: *ID3D11ShaderResourceView = {}
floor_texture: *ID3D11ShaderResourceView = {}
grass: *ID3D11ShaderResourceView = {}
glass: *ID3D11ShaderResourceView = {}
sampler: *ID3D11SamplerState = {}
clamp_sampler: *ID3D11SamplerState = {}
alpha_blend: *ID3D11BlendState = {}
; timing
delta_time: f32 = 0.0
last_frame: f32 = 0.0
; create_alpha_blend's: written once, so all initialiser. Render target 0 is
; the only one bound; the other seven stay zero.
create_alpha_blend_desc: D3D11_BLEND_DESC = {
.RenderTarget = {
{
.BlendEnable = 1,
.SrcBlend = D3D11_BLEND.D3D11_BLEND_SRC_ALPHA,
.DestBlend = D3D11_BLEND.D3D11_BLEND_INV_SRC_ALPHA,
.BlendOp = D3D11_BLEND_OP.D3D11_BLEND_OP_ADD,
.SrcBlendAlpha = D3D11_BLEND.D3D11_BLEND_ONE,
.DestBlendAlpha = D3D11_BLEND.D3D11_BLEND_INV_SRC_ALPHA,
.BlendOpAlpha = D3D11_BLEND_OP.D3D11_BLEND_OP_ADD,
.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE.D3D11_COLOR_WRITE_ENABLE_ALL,
},
},
}
create_alpha_blend_out: *ID3D11BlendState = {}
; -> rax: a blend state for transparency: out = src * src_alpha + dst * (1 - src_alpha)
create_alpha_blend: proc() -> *ID3D11BlendState = {
sub rsp, 40 ; 32 shadow, and rsp back on 16
vcall(device, ID3D11DeviceVtbl.CreateBlendState, create_alpha_blend_desc&, create_alpha_blend_out&)
assert_hr()
mov rax, create_alpha_blend_out
add rsp, 40
ret
}
; sort_windows's
sort_windows_offset: vec3 = {} ; from the camera to one window
window_order: [5]u32 = {} ; the windows, farthest first
window_distance: [5]f32 = {}
; Puts the windows in window_order from the farthest from the camera to the
; nearest.
sort_windows: proc() = {
push rbx
push rsi
push rdi
push r12
sub rsp, 40 ; 32 shadow; entry 8 + four pushes 32 + 40 lands on 16
; A global indexed by a register needs its address in a register: an
; index on a global's name must be a constant.
lea rdi, window_order
lea r12, window_distance
xor ebx, ebx
distances:
mov [rdi + rbx * 4], ebx
imul eax, ebx, sizeof(vec3)
lea rdx, window_positions
add rdx, rax
invoke(v3_sub, sort_windows_offset&, rdx, cam.position&)
invoke(v3_length, sort_windows_offset&)
movss dword ptr [r12 + rbx * 4], xmm0
inc ebx
cmp ebx, 5
jb distances
; selection sort on the distances, carrying the order along
xor ebx, ebx
outer:
lea esi, [ebx + 1]
inner:
cmp esi, 5
jae inner_done
movss xmm0, dword ptr [r12 + rsi * 4]
comiss xmm0, dword ptr [r12 + rbx * 4]
jbe in_order
movss xmm1, dword ptr [r12 + rbx * 4]
movss dword ptr [r12 + rbx * 4], xmm0
movss dword ptr [r12 + rsi * 4], xmm1
mov eax, [rdi + rbx * 4]
mov edx, [rdi + rsi * 4]
mov [rdi + rbx * 4], edx
mov [rdi + rsi * 4], eax
in_order:
inc esi
jmp inner
inner_done:
inc ebx
cmp ebx, 4
jb outer
add rsp, 40
pop r12
pop rdi
pop rsi
pop rbx
ret
}
main: proc() -> i32 = {
push rbx
push rsi
sub rsp, 40 ; draw_vertices takes 5: 32 + 8; entry 8 + two pushes 16 + 40 lands on 16
invoke(window_create, "LearnD3D11")
call window_create_depth
invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_ANISOTROPIC, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_WRAP)
mov sampler, rax
invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_ANISOTROPIC, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_CLAMP)
mov clamp_sampler, rax
vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, sampler&)
call create_alpha_blend
mov alpha_blend, rax
invoke(shader_create, s&, "src/4.advanced_d3d11/3.1.blending/blending.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, cube_vertices&, sizeof(cube_vertices))
mov cube_vb, rax
invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, plane_vertices&, sizeof(plane_vertices))
mov plane_vb, rax
invoke(create_buffer, D3D11_BIND_FLAG.D3D11_BIND_VERTEX_BUFFER, quad_vertices&, sizeof(quad_vertices))
mov quad_vb, rax
invoke(create_constant_buffer, sizeof(transforms))
mov transforms_buffer, rax
invoke(filesystem_path, "resources/textures/marble.jpg")
invoke(load_texture, rax, 0)
mov cube_texture, rax
invoke(filesystem_path, "resources/textures/metal.png")
invoke(load_texture, rax, 0)
mov floor_texture, rax
invoke(filesystem_path, "resources/textures/grass.png")
invoke(load_texture, rax, 0)
mov grass, rax
invoke(filesystem_path, "resources/textures/window.png")
invoke(load_texture, rax, 0)
mov glass, rax
lea rcx, cam
lea rdx, camera_start
movss xmm2, -90.0 ; yaw
movss xmm3, -10.0 ; pitch
call camera_create
render_loop:
call window_poll
test eax, eax
jz render_loop_done
call get_time
movss xmm1, xmm0
subss xmm0, last_frame
movss delta_time, xmm0
movss last_frame, xmm1
invoke(camera_process_input, cam&, delta_time&)
invoke(window_begin, clear_color&)
vcall(context, ID3D11DeviceContextVtbl.IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
invoke(bind_constants, 0, transforms_buffer)
invoke(transforms_from_camera, t&, cam&)
invoke(shader_use, s&)
; everything opaque first: the floor and two cubes
invoke(bind_texture, 0, floor_texture)
invoke(m4_identity, model&)
invoke(draw_vertices, transforms_buffer, t&, plane_vb, 6, model&)
invoke(bind_texture, 0, cube_texture)
invoke(m4_translate, model&, cube_a&)
invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)
invoke(m4_translate, model&, cube_b&)
invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)
; grass: clipped, so in any order
vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, clamp_sampler&)
invoke(bind_texture, 0, grass)
lea rsi, grass_positions
xor ebx, ebx
blades:
invoke(m4_translate, model&, rsi)
invoke(draw_vertices, transforms_buffer, t&, quad_vb, 6, model&)
add rsi, sizeof(vec3)
inc ebx
cmp ebx, 4
jb blades
; windows: blended, so the farthest first
call sort_windows
vcall(context, ID3D11DeviceContextVtbl.OMSetBlendState, alpha_blend, 0, D3D11_DEFAULT_SAMPLE_MASK)
invoke(bind_texture, 0, glass)
xor ebx, ebx
windows:
lea rax, window_order
mov eax, [rax + rbx * 4]
imul eax, eax, sizeof(vec3)
lea rsi, window_positions
add rsi, rax
invoke(m4_translate, model&, rsi)
invoke(draw_vertices, transforms_buffer, t&, quad_vb, 6, model&)
inc ebx
cmp ebx, 5
jb windows
vcall(context, ID3D11DeviceContextVtbl.OMSetBlendState, 0, 0, D3D11_DEFAULT_SAMPLE_MASK)
vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, sampler&)
call window_present
jmp render_loop
render_loop_done:
invoke(ExitProcess, 0)
}