瀏覽代碼

Color support when drawing

Karl Zylinski 7 月之前
父節點
當前提交
ab05ac2dbb
共有 3 個文件被更改,包括 38 次插入15 次删除
  1. 33 13
      karl2d_windows.odin
  2. 4 1
      shader.hlsl
  3. 1 1
      todo.txt

+ 33 - 13
karl2d_windows.odin

@@ -116,7 +116,8 @@ _init :: proc(width: int, height: int, title: string,
 	device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nil, &vertex_shader)
 
 	input_element_desc := [?]D3D11.INPUT_ELEMENT_DESC{
-		{ "POS", 0, .R32G32B32_FLOAT, 0,                            0, .VERTEX_DATA, 0 },
+		{ "POS", 0, .R32G32B32_FLOAT, 0, 0, .VERTEX_DATA, 0 },
+		{ "COL", 0, .R8G8B8A8_UNORM , 0, D3D11.APPEND_ALIGNED_ELEMENT, .VERTEX_DATA, 0 },
 	}
 
 	device->CreateInputLayout(&input_element_desc[0], len(input_element_desc), vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), &input_layout)
@@ -150,7 +151,7 @@ _init :: proc(width: int, height: int, title: string,
 	device->CreateBuffer(&constant_buffer_desc, nil, &constant_buffer)
 
 	vertex_buffer_desc := D3D11.BUFFER_DESC{
-		ByteWidth = VERTEX_BUFFER_MAX,
+		ByteWidth = VERTEX_BUFFER_MAX * size_of(Vertex),
 		Usage     = .DYNAMIC,
 		BindFlags = {.VERTEX_BUFFER},
 		CPUAccessFlags = {.WRITE},
@@ -159,7 +160,7 @@ _init :: proc(width: int, height: int, title: string,
 
 	vb_data: D3D11.MAPPED_SUBRESOURCE
 	s.device_context->Map(s.vertex_buffer, 0, .WRITE_NO_OVERWRITE, {}, &vb_data)
-	s.vertex_buffer_map = slice.from_ptr((^Vec3)(vb_data.pData), VERTEX_BUFFER_MAX)
+	s.vertex_buffer_map = slice.from_ptr((^Vertex)(vb_data.pData), VERTEX_BUFFER_MAX)
 
 	s.proj_matrix = make_default_projection(s.width, s.height)
 
@@ -172,6 +173,11 @@ Vec3 :: [3]f32
 
 shader_hlsl :: #load("shader.hlsl")
 
+Vertex :: struct {
+	pos: Vec3,
+	color: Color,
+}
+
 s: ^State
 
 constant_buffer: ^D3D11.IBuffer
@@ -188,7 +194,7 @@ State :: struct {
 	device_context: ^D3D11.IDeviceContext,
 	vertex_buffer: ^D3D11.IBuffer,
 	vertex_buffer_count: int,
-	vertex_buffer_map: []Vec3,
+	vertex_buffer_map: []Vertex,
 
 	run: bool,
 	custom_context: runtime.Context,
@@ -315,6 +321,15 @@ _draw_texture_rect :: proc(tex: Texture, rect: Rect, pos: Vec2, tint := WHITE) {
 	)
 }
 
+add_vertex :: proc(v: Vec3, color: Color) {
+	s.vertex_buffer_map[s.vertex_buffer_count] = {
+		pos = v,
+		color = color,
+	}
+
+	s.vertex_buffer_count += 1
+}
+
 _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot: f32, tint := WHITE) {
 	p := Vec2 {
 		dst.x, dst.y,
@@ -322,16 +337,21 @@ _draw_texture_ex :: proc(tex: Texture, src: Rect, dst: Rect, origin: Vec2, rot:
 
 	p -= origin
 
-	s.vertex_buffer_map[s.vertex_buffer_count + 0] = {p.x, p.y, 0.1}
-	s.vertex_buffer_map[s.vertex_buffer_count + 1] = {p.x + dst.w, p.y, 0.1}
-	s.vertex_buffer_map[s.vertex_buffer_count + 2] = {p.x + dst.w, p.y + dst.h, 0.1}
-	s.vertex_buffer_map[s.vertex_buffer_count + 3] = {p.x, p.y, 0.1}
-	s.vertex_buffer_map[s.vertex_buffer_count + 4] = {p.x + dst.w, p.y + dst.h, 0.1}
-	s.vertex_buffer_map[s.vertex_buffer_count + 5] = {p.x, p.y + dst.h, 0.1}
-	s.vertex_buffer_count += 6
+	add_vertex({p.x, p.y, 0.1}, tint)
+	add_vertex({p.x + dst.w, p.y, 0.1}, tint)
+	add_vertex({p.x + dst.w, p.y + dst.h, 0.1}, tint)
+	add_vertex({p.x, p.y, 0.1}, tint)
+	add_vertex({p.x + dst.w, p.y + dst.h, 0.1}, tint)
+	add_vertex({p.x, p.y + dst.h, 0.1}, tint)
 }
 
-_draw_rectangle :: proc(rect: Rect, color: Color) {
+_draw_rectangle :: proc(r: Rect, color: Color) {
+	add_vertex({r.x, r.y, 0.1}, color)
+	add_vertex({r.x + r.w, r.y, 0.1}, color)
+	add_vertex({r.x + r.w, r.y + r.h, 0.1}, color)
+	add_vertex({r.x, r.y, 0.1}, color)
+	add_vertex({r.x + r.w, r.y + r.h, 0.1}, color)
+	add_vertex({r.x, r.y + r.h, 0.1}, color)
 }
 
 _draw_rectangle_outline :: proc(rect: Rect, thickness: f32, color: Color) {
@@ -470,7 +490,7 @@ _present :: proc(do_flush := true) {
 	dc->IASetPrimitiveTopology(.TRIANGLELIST)
 	dc->IASetInputLayout(input_layout)
 	vertex_buffer_offset := u32(0)
-	vertex_buffer_stride := u32(3*4)
+	vertex_buffer_stride := u32(size_of(Vertex))
 	dc->IASetVertexBuffers(0, 1, &s.vertex_buffer, &vertex_buffer_stride, &vertex_buffer_offset)
 
 	dc->VSSetShader(vertex_shader, nil, 0)

+ 4 - 1
shader.hlsl

@@ -3,15 +3,18 @@ cbuffer constants : register(b0) {
 }
 struct vs_in {
 	float3 position : POS;
+	float4 color    : COL;
 };
 struct vs_out {
 	float4 position : SV_POSITION;
+	float4 color    : COL;
 };
 vs_out vs_main(vs_in input) {
 	vs_out output;
 	output.position = mul(projection, float4(input.position, 1.0f));
+	output.color = input.color;
 	return output;
 }
 float4 ps_main(vs_out input) : SV_TARGET {
-	return float4(1,1,1,1);
+	return input.color;
 }

+ 1 - 1
todo.txt

@@ -1,4 +1,4 @@
 X Make 0, 0 be at top left (should vertex data be flipped, or is it a transformation thingy?)
-* Construct vertex buffer from k2.draw_blabla calls. Do we need index buffer? 🤷‍
+X Construct vertex buffer from k2.draw_blabla calls. Do we need index buffer? 🤷‍
 * Organize the d3d11 things neatly. It's just a hack right now!
 * Load textures and somehow bind to shader -- split draw calls on texture switch -- needs a start of a batch system.