v / vlib / sokol / gfx / gfx.c.v
334 lines · 276 sloc · 7.99 KB · 66c72b149fdc9e8ab1ad4d2554fd1050ca36ea03
Raw
1module gfx
2
3import sokol.c as _
4import sokol.memory
5
6pub const version = 1
7
8// setup initialises the SOKOL's gfx library, based on the information passed in `desc`
9pub fn setup(desc &Desc) {
10 if desc.allocator.alloc_fn == unsafe { nil } && desc.allocator.free_fn == unsafe { nil } {
11 unsafe {
12 desc.allocator.alloc_fn = memory.salloc
13 desc.allocator.free_fn = memory.sfree
14 desc.allocator.user_data = voidptr(0x1006fec5)
15 }
16 }
17 if desc.logger.func == unsafe { nil } {
18 unsafe {
19 desc.logger.func = memory.slog
20 }
21 }
22 C.sg_setup(desc)
23}
24
25// shutdown tells the SOKOL's gfx library to shutdown, and release the resources it is using
26pub fn shutdown() {
27 C.sg_shutdown()
28}
29
30@[inline]
31pub fn is_valid() bool {
32 return C.sg_isvalid()
33}
34
35@[inline]
36fn ensure_initialized(op string) {
37 if is_valid() {
38 return
39 }
40 panic('sokol.gfx is not initialized; call gfx.setup(...) before ${op}')
41}
42
43@[inline]
44pub fn reset_state_cache() {
45 C.sg_reset_state_cache()
46}
47
48// resource creation, destruction and updating
49// make_buffer creates a GPU buffer.
50// make_buffer requires `gfx.setup(...)` to have completed first.
51@[inline]
52pub fn make_buffer(desc &BufferDesc) Buffer {
53 ensure_initialized('gfx.make_buffer(...)')
54 return C.sg_make_buffer(desc)
55}
56
57// make_image creates a GPU image.
58// make_image requires `gfx.setup(...)` to have completed first.
59@[inline]
60pub fn make_image(desc &ImageDesc) Image {
61 ensure_initialized('gfx.make_image(...)')
62 return C.sg_make_image(desc)
63}
64
65// make_sampler creates a GPU sampler.
66// make_sampler requires `gfx.setup(...)` to have completed first.
67@[inline]
68pub fn make_sampler(desc &SamplerDesc) Sampler {
69 ensure_initialized('gfx.make_sampler(...)')
70 return C.sg_make_sampler(desc)
71}
72
73// make_shader creates a GPU shader.
74// make_shader requires `gfx.setup(...)` to have completed first.
75@[inline]
76pub fn make_shader(desc &ShaderDesc) Shader {
77 ensure_initialized('gfx.make_shader(...)')
78 return C.sg_make_shader(desc)
79}
80
81// make_pipeline creates a GPU pipeline.
82// make_pipeline requires `gfx.setup(...)` to have completed first.
83@[inline]
84pub fn make_pipeline(desc &PipelineDesc) Pipeline {
85 ensure_initialized('gfx.make_pipeline(...)')
86 return C.sg_make_pipeline(desc)
87}
88
89// make_attachments creates an `Attachments` instance from `const_desc`.
90// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
91@[inline]
92pub fn make_attachments(const_desc &AttachmentsDesc) Attachments {
93 ensure_initialized('gfx.make_attachments(...)')
94 return C.sg_make_attachments(const_desc)
95}
96
97@[inline]
98pub fn destroy_buffer(buf Buffer) {
99 C.sg_destroy_buffer(buf)
100}
101
102@[inline]
103pub fn destroy_image(img Image) {
104 C.sg_destroy_image(img)
105}
106
107@[inline]
108pub fn destroy_sampler(smp Sampler) {
109 C.sg_destroy_sampler(smp)
110}
111
112@[inline]
113pub fn destroy_shader(shd Shader) {
114 C.sg_destroy_shader(shd)
115}
116
117@[inline]
118pub fn destroy_pipeline(pip Pipeline) {
119 C.sg_destroy_pipeline(pip)
120}
121
122// destroy_attachments destroys the `atts` `Attachments`
123// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
124@[inline]
125pub fn destroy_attachments(atts Attachments) {
126 C.sg_destroy_attachments(atts)
127}
128
129@[inline]
130pub fn update_buffer(buf Buffer, data &Range) {
131 C.sg_update_buffer(buf, data)
132}
133
134@[inline]
135pub fn update_image(img Image, data &ImageData) {
136 C.sg_update_image(img, data)
137}
138
139@[inline]
140pub fn append_buffer(buf Buffer, data &Range) int {
141 return C.sg_append_buffer(buf, data)
142}
143
144@[inline]
145pub fn query_buffer_overflow(buf Buffer) bool {
146 return C.sg_query_buffer_overflow(buf)
147}
148
149// rendering functions
150
151// begin_pass begins a rendering pass.
152// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
153@[inline]
154pub fn begin_pass(const_pass &Pass) {
155 C.sg_begin_pass(const_pass)
156}
157
158@[inline]
159pub fn apply_viewport(x int, y int, width int, height int, origin_top_left bool) {
160 C.sg_apply_viewport(x, y, width, height, origin_top_left)
161}
162
163@[inline]
164pub fn apply_scissor_rect(x int, y int, width int, height int, origin_top_left bool) {
165 C.sg_apply_scissor_rect(x, y, width, height, origin_top_left)
166}
167
168@[inline]
169pub fn apply_pipeline(pip Pipeline) {
170 C.sg_apply_pipeline(pip)
171}
172
173@[inline]
174pub fn apply_bindings(bindings &Bindings) {
175 C.sg_apply_bindings(bindings)
176}
177
178@[inline]
179pub fn apply_uniforms(stage ShaderStage, ub_index int, data &Range) {
180 C.sg_apply_uniforms(stage, ub_index, data)
181}
182
183@[inline]
184pub fn draw(base_element int, num_elements int, num_instances int) {
185 C.sg_draw(base_element, num_elements, num_instances)
186}
187
188@[inline]
189pub fn end_pass() {
190 C.sg_end_pass()
191}
192
193@[inline]
194pub fn commit() {
195 C.sg_commit()
196}
197
198// getting information
199@[inline]
200pub fn query_desc() Desc {
201 return C.sg_query_desc()
202}
203
204@[inline]
205pub fn query_backend() Backend {
206 unsafe {
207 return Backend(C.sg_query_backend())
208 }
209}
210
211@[inline]
212pub fn query_features() Features {
213 return C.sg_query_features()
214}
215
216@[inline]
217pub fn query_limits() Limits {
218 return C.sg_query_limits()
219}
220
221@[inline]
222pub fn query_pixelformat(fmt PixelFormat) PixelFormatInfo {
223 return C.sg_query_pixelformat(fmt)
224}
225
226// get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID)
227@[inline]
228pub fn query_buffer_state(buf Buffer) ResourceState {
229 return ResourceState(C.sg_query_buffer_state(buf))
230}
231
232@[inline]
233pub fn query_image_state(img Image) ResourceState {
234 return ResourceState(C.sg_query_image_state(img))
235}
236
237@[inline]
238pub fn query_shader_state(shd Shader) ResourceState {
239 return ResourceState(C.sg_query_shader_state(shd))
240}
241
242@[inline]
243pub fn query_pipeline_state(pip Pipeline) ResourceState {
244 return ResourceState(C.sg_query_pipeline_state(pip))
245}
246
247// query_attachments_state returns the current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID)
248// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
249@[inline]
250pub fn query_attachments_state(atts Attachments) ResourceState {
251 return ResourceState(C.sg_query_attachments_state(atts))
252}
253
254// get runtime information about a resource
255@[inline]
256pub fn query_buffer_info(buf Buffer) BufferInfo {
257 return C.sg_query_buffer_info(buf)
258}
259
260@[inline]
261pub fn query_image_info(img Image) ImageInfo {
262 return C.sg_query_image_info(img)
263}
264
265@[inline]
266pub fn query_shader_info(shd Shader) ShaderInfo {
267 return C.sg_query_shader_info(shd)
268}
269
270@[inline]
271pub fn query_pipeline_info(pip Pipeline) PipelineInfo {
272 return C.sg_query_pipeline_info(pip)
273}
274
275// query_attachments_info returns runtime information about the `atts` / `Attachments`.
276// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
277@[inline]
278pub fn query_attachments_info(atts Attachments) AttachmentsInfo {
279 return C.sg_query_attachments_info(atts)
280}
281
282// get resource creation desc struct with their default values replaced
283@[inline]
284pub fn query_buffer_defaults(desc &Buffer) BufferDesc {
285 return C.sg_query_buffer_defaults(unsafe { &BufferDesc(voidptr(desc)) })
286}
287
288@[inline]
289pub fn query_image_defaults(desc &Image) ImageDesc {
290 return C.sg_query_image_defaults(unsafe { &ImageDesc(voidptr(desc)) })
291}
292
293@[inline]
294pub fn query_shader_defaults(desc &Shader) ShaderDesc {
295 return C.sg_query_shader_defaults(unsafe { &ShaderDesc(voidptr(desc)) })
296}
297
298@[inline]
299pub fn query_pipeline_defaults(desc &Pipeline) PipelineDesc {
300 return C.sg_query_pipeline_defaults(unsafe { &PipelineDesc(voidptr(desc)) })
301}
302
303// query_attachments_defaults returns `AttachmentsDesc` with default values replaced.
304// See also: documentation at the top of thirdparty/sokol/sokol_gfx.h
305@[inline]
306pub fn query_attachments_defaults(desc &AttachmentsDesc) AttachmentsDesc {
307 return C.sg_query_attachments_defaults(unsafe { &AttachmentsDesc(voidptr(desc)) })
308}
309
310// frame stats
311
312// enable_frame_stats enables the sokol frame statistics.
313@[inline]
314pub fn enable_frame_stats() {
315 C.sg_enable_frame_stats()
316}
317
318// disable_frame_stats disables the sokol frame statistics.
319@[inline]
320pub fn disable_frame_stats() {
321 C.sg_disable_frame_stats()
322}
323
324// frame_stats_enabled returns `true` if the sokol frame statistics is enabled.
325@[inline]
326pub fn frame_stats_enabled() bool {
327 return C.sg_frame_stats_enabled()
328}
329
330// query_frame_stats returns the sokol frame statistics for the current frame.
331@[inline]
332pub fn query_frame_stats() FrameStats {
333 return C.sg_query_frame_stats()
334}
335