-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathrender_backend_nil.odin
More file actions
178 lines (143 loc) · 5.08 KB
/
Copy pathrender_backend_nil.odin
File metadata and controls
178 lines (143 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#+private file
package karl2d
@(private="package")
RENDER_BACKEND_NIL :: Render_Backend_Interface {
state_size = rbnil_state_size,
init = rbnil_init,
shutdown = rbnil_shutdown,
clear = rbnil_clear,
present = rbnil_present,
draw = rbnil_draw,
resize_swapchain = rbnil_resize_swapchain,
get_swapchain_width = rbnil_get_swapchain_width,
get_swapchain_height = rbnil_get_swapchain_height,
set_internal_state = rbnil_set_internal_state,
create_texture = rbnil_create_texture,
load_texture = rbnil_load_texture,
update_texture = rbnil_update_texture,
destroy_texture = rbnil_destroy_texture,
texture_needs_vertical_flip = rbnil_texture_needs_vertical_flip,
create_render_texture = rbnil_create_render_texture,
destroy_render_target = rbnil_destroy_render_target,
set_texture_filter = rbnil_set_texture_filter,
load_shader = rbnil_load_shader,
destroy_shader = rbnil_destroy_shader,
default_shader_vertex_source = rbnil_default_shader_vertex_source,
default_shader_fragment_source = rbnil_default_shader_fragment_source,
}
import "log"
// The nil backend hands out handles from a monotonic counter. Nothing ever looks the handles up, we
// just need them to be non-zero: a zero handle means TEXTURE_NONE/SHADER_NONE and makes the drawing
// procedures in karl2d.odin bail out early.
rbnil_next_handle_idx: u32 = 1
rbnil_handle :: proc() -> Handle {
h := Handle { idx = rbnil_next_handle_idx, gen = 1 }
rbnil_next_handle_idx += 1
return h
}
// The nil backend has no state of its own (`state_size` is 0), so the swapchain size lives here.
// It still needs to report a real size: the default projection matrix is built from it.
rbnil_swapchain_width: int
rbnil_swapchain_height: int
rbnil_state_size :: proc() -> int {
return 0
}
rbnil_init :: proc(
state: rawptr,
glue: Window_Render_Glue,
swapchain_width,
swapchain_height: int,
options: Init_Options,
allocator := context.allocator
) {
log.info("Render Backend nil init")
rbnil_swapchain_width = swapchain_width
rbnil_swapchain_height = swapchain_height
}
rbnil_shutdown :: proc() {
log.info("Render Backend nil shutdown")
}
rbnil_clear :: proc(render_texture: Render_Target_Handle, color: Color) {
}
rbnil_present :: proc() {
}
rbnil_draw :: proc(vertex_buffer: []u8, draw_calls: []Draw_Call) {
}
rbnil_resize_swapchain :: proc(w, h: int) {
rbnil_swapchain_width = w
rbnil_swapchain_height = h
}
rbnil_get_swapchain_width :: proc() -> int {
return rbnil_swapchain_width
}
rbnil_get_swapchain_height :: proc() -> int {
return rbnil_swapchain_height
}
rbnil_set_internal_state :: proc(state: rawptr) {
}
rbnil_create_texture :: proc(width: int, height: int, format: Pixel_Format) -> Texture_Handle {
return Texture_Handle(rbnil_handle())
}
rbnil_load_texture :: proc(data: []u8, width: int, height: int, format: Pixel_Format) -> Texture_Handle {
return Texture_Handle(rbnil_handle())
}
rbnil_update_texture :: proc(th: Texture_Handle, data: []u8, rect: Rect) -> bool {
return true
}
rbnil_destroy_texture :: proc(th: Texture_Handle) {
}
rbnil_texture_needs_vertical_flip :: proc(th: Texture_Handle) -> bool {
return false
}
rbnil_create_render_texture :: proc(width: int, height: int) -> (Texture_Handle, Render_Target_Handle) {
return Texture_Handle(rbnil_handle()), Render_Target_Handle(rbnil_handle())
}
rbnil_destroy_render_target :: proc(render_target: Render_Target_Handle) {
}
rbnil_set_texture_filter :: proc(
th: Texture_Handle,
scale_down_filter: Texture_Filter,
scale_up_filter: Texture_Filter,
mip_filter: Texture_Filter,
) {
}
// The nil backend does not parse shader source. It reports the same layout the built-in default
// shader uses, which is what the batching code in karl2d.odin needs in order to lay out vertices.
// That's enough to run the library headless (tests, benchmarks, dedicated servers). Custom shaders
// with a different vertex layout will be reported as if they had the default one.
rbnil_load_shader :: proc(
vs_source: []byte,
fs_source: []byte,
desc_allocator := frame_allocator,
layout_formats: []Pixel_Format = {},
) -> (
handle: Shader_Handle,
desc: Shader_Desc,
) {
inputs := make([]Shader_Input, 3, desc_allocator)
inputs[0] = { name = "position", register = 0, type = .Vec2, format = .RG_32_Float }
inputs[1] = { name = "texcoord", register = 1, type = .Vec2, format = .RG_32_Float }
inputs[2] = { name = "color", register = 2, type = .Vec4, format = .RGBA_8_Norm }
for &input, input_idx in inputs {
if input_idx < len(layout_formats) && layout_formats[input_idx] != .Unknown {
input.format = layout_formats[input_idx]
}
}
constants := make([]Shader_Constant_Desc, 1, desc_allocator)
constants[0] = { name = "view_projection", size = size_of(matrix[4,4]f32) }
texture_bindpoints := make([]Shader_Texture_Bindpoint_Desc, 1, desc_allocator)
texture_bindpoints[0] = { name = "tex" }
return Shader_Handle(rbnil_handle()), {
constants = constants,
texture_bindpoints = texture_bindpoints,
inputs = inputs,
}
}
rbnil_destroy_shader :: proc(h: Shader_Handle) {
}
rbnil_default_shader_vertex_source :: proc() -> []byte {
return {}
}
rbnil_default_shader_fragment_source :: proc() -> []byte {
return {}
}