gamma_correction.rasm
src/5.advanced_lighting/2.gamma_correction/gamma_correction.rasm · 201 lines
; 2. Gamma correction
;
; Monitors do not show a pixel value of 0.5 as half the light of 1.0, but as
; about a fifth: brightness follows roughly value^2.2. Images are stored with
; the inverse curve applied (sRGB), so they look right on screen. Lighting,
; though, is maths on physical light and only adds up in linear space.
;
; Done properly, the round trip is:
;
; 1. texture -> linear the texture is created as an _SRGB format, so
; sampling it decodes to linear for free
; 2. lighting in linear where two lights really are twice one light
; 3. linear -> screen the back buffer is written through an _SRGB render
; target view, which encodes on the way in
;
; GL switches this with GL_FRAMEBUFFER_SRGB; in D3D it is a format, chosen per
; view. With gamma correction the four lights fall off physically, as 1/d^2.
; Without it that looks far too steep, which is why uncorrected code fell back
; on a linear 1/d that is wrong but looks closer.
;
; Tab gamma corrected (1/d^2) / uncorrected (1/d)
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 = { ; 6 vertices of 8 floats
; position normal uv
-10.0, -0.5, 10.0, 0.0, 1.0, 0.0, 0.0, 10.0,
10.0, -0.5, 10.0, 0.0, 1.0, 0.0, 10.0, 10.0,
10.0, -0.5, -10.0, 0.0, 1.0, 0.0, 10.0, 0.0,
-10.0, -0.5, 10.0, 0.0, 1.0, 0.0, 0.0, 10.0,
10.0, -0.5, -10.0, 0.0, 1.0, 0.0, 10.0, 0.0,
-10.0, -0.5, -10.0, 0.0, 1.0, 0.0, 0.0, 0.0,
}
; Mirrors `cbuffer lights` in gamma_correction.hlsl: four lights in a row
; along x, each brighter than the last.
lights_constants: struct = {
positions: [4]vec4
colors: [4]vec4
gamma: u32
pad: [3]u32
}
camera_start: const vec3 = { 0.0, 1.0, 4.0 }
clear_color: [4]const f32 = { 0.0, 0.0, 0.0, 1.0 }
lamp_color: const vec4 = { 1.0, 1.0, 1.0, 1.0 }
t: transforms = {}
lights: lights_constants = {
.positions = { { -3.0, -0.4, 0.0, 0.0 }, { -1.0, -0.4, 0.0, 0.0 }, { 1.0, -0.4, 0.0, 0.0 }, { 3.0, -0.4, 0.0, 0.0 } },
.colors = { { 0.25, 0.25, 0.25, 0.0 }, { 0.5, 0.5, 0.5, 0.0 }, { 0.75, 0.75, 0.75, 0.0 }, { 1.0, 1.0, 1.0, 0.0 } },
}
model: mat4 = {}
s: shader = {}
lamp_shader: shader = {}
cam: camera = {}
cube_vb: *ID3D11Buffer = {}
plane_vb: *ID3D11Buffer = {}
transforms_buffer: *ID3D11Buffer = {}
light_buffer: *ID3D11Buffer = {}
lamp_buffer: *ID3D11Buffer = {}
wood_linear: *ID3D11ShaderResourceView = {}
wood_srgb: *ID3D11ShaderResourceView = {}
sampler: *ID3D11SamplerState = {}
backbuffer_srgb_rtv: *ID3D11RenderTargetView = {}
target: *ID3D11RenderTargetView = {} ; the view drawn into this frame, an array of one
; A second view of the same back buffer, as sRGB, for the corrected mode. Its
; description is known in full now.
srgb_view: D3D11_RENDER_TARGET_VIEW_DESC = {
.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
.ViewDimension = D3D11_RTV_DIMENSION.D3D11_RTV_DIMENSION_TEXTURE2D,
}
; timing
delta_time: f32 = 0.0
last_frame: f32 = 0.0
main: proc() -> i32 = {
push rbx
push rsi
sub rsp, 40 ; ClearDepthStencilView and draw_vertices take 5: 32 + 8; entry 8 + two pushes 16 + 40 lands on 16
invoke(window_create, "LearnD3D11 (Tab: corrected / uncorrected)")
call window_create_depth
invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_ANISOTROPIC, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_WRAP)
mov sampler, rax
vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, sampler&)
vcall(device, ID3D11DeviceVtbl.CreateRenderTargetView, backbuffer, srgb_view&, backbuffer_srgb_rtv&)
assert_hr()
; build and compile shaders
; -------------------------
invoke(shader_create, s&, "src/5.advanced_lighting/2.gamma_correction/gamma_correction.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
invoke(shader_create, lamp_shader&, "src/5.advanced_lighting/2.gamma_correction/light_cube.hlsl", cube_layout&, CUBE_LAYOUT_COUNT)
; set up vertex data and textures: the same image, read two ways
; --------------------------------------------------------------
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_constant_buffer, sizeof(transforms))
mov transforms_buffer, rax
invoke(create_constant_buffer, sizeof(lights_constants))
mov light_buffer, rax
invoke(create_constant_buffer, sizeof(vec4))
mov lamp_buffer, rax
invoke(filesystem_path, "resources/textures/wood.png")
mov rbx, rax
invoke(load_texture, rbx, 0)
mov wood_linear, rax
invoke(load_texture, rbx, 1)
mov wood_srgb, rax
lea rcx, cam
lea rdx, camera_start
movss xmm2, -90.0 ; yaw
movss xmm3, -15.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&)
; mode 0 corrected, mode 1 uncorrected
invoke(window_mode, 2)
mov ecx, 1
sub ecx, eax
mov lights.gamma, ecx
; render, through the sRGB view when correcting
; ------
mov rax, backbuffer_rtv
cmp lights.gamma, 0
je plain_target
mov rax, backbuffer_srgb_rtv
plain_target:
mov target, rax
vcall(context, ID3D11DeviceContextVtbl.OMSetRenderTargets, 1, target&, depth_dsv)
vcall(context, ID3D11DeviceContextVtbl.RSSetViewports, 1, viewport&)
vcall(context, ID3D11DeviceContextVtbl.ClearRenderTargetView, target, clear_color&)
movss xmm3, 1.0 ; the depth, a float: xmm3
vcall(context, ID3D11DeviceContextVtbl.ClearDepthStencilView, depth_dsv, D3D11_CLEAR_FLAG.D3D11_CLEAR_DEPTH, 0, 0)
vcall(context, ID3D11DeviceContextVtbl.IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY.D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST)
invoke(bind_constants, 0, transforms_buffer)
invoke(transforms_from_camera, t&, cam&)
invoke(update_buffer, light_buffer, lights&, sizeof(lights_constants))
invoke(shader_use, s&)
invoke(bind_constants, 1, light_buffer)
mov rax, wood_linear
cmp lights.gamma, 0
je plain_wood
mov rax, wood_srgb
plain_wood:
invoke(bind_texture, 0, rax)
invoke(m4_identity, model&)
invoke(draw_vertices, transforms_buffer, t&, plane_vb, 6, model&)
; a small lamp at each light
invoke(update_buffer, lamp_buffer, lamp_color&, sizeof(vec4))
invoke(shader_use, lamp_shader&)
invoke(bind_constants, 1, lamp_buffer)
lea rsi, lights.positions
xor ebx, ebx
lamps:
lea rcx, model
mov rdx, rsi
movss xmm2, 0.05
call m4_translate_scale
invoke(draw_vertices, transforms_buffer, t&, cube_vb, 36, model&)
add rsi, sizeof(vec4)
inc ebx
cmp ebx, 4
jb lamps
call window_present
jmp render_loop
render_loop_done:
invoke(ExitProcess, 0)
}