-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderc.scm
121 lines (96 loc) · 3.59 KB
/
shaderc.scm
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
(library (shaderc)
(export shaderc-vertex-shader
shaderc-fragment-shader
shaderc-compute-shader
shaderc-geometry-shader
shaderc-tess-control-shader
shaderc-tess-evaluation-shader
compile-shaders)
(import (chezscheme)
(ffi)
(only (srfi s13 strings) string-join))
(define s (load-shared-object "libshaderc_shared.so"))
;;;;;;;;;;;;;;;;;;;;;;;
;; low level wrapper ;;
;;;;;;;;;;;;;;;;;;;;;;;
;; types
(define-ftype shaderc-compiler uptr)
(define-ftype shaderc-compile-options uptr)
(define-ftype shaderc-compilation-result uptr)
;; functions
(define _init-compiler
(foreign-procedure "shaderc_compiler_initialize" () (* shaderc-compiler)))
(define _init-compile-options
(foreign-procedure "shaderc_compile_options_initialize" () (* shaderc-compile-options)))
(define-enum-ftype shaderc-shader-kind
shaderc-vertex-shader
shaderc-fragment-shader
shaderc-compute-shader
shaderc-geometry-shader
shaderc-tess-control-shader
shaderc-tess-evaluation-shader)
(define _compile->spv
(foreign-procedure "shaderc_compile_into_spv"
((* shaderc-compiler) string size_t shaderc-shader-kind string string
(* shaderc-compile-options))
(* shaderc-compilation-result)))
(define-enum-ftype shaderc-compilation-status
shaderc-compilation-status-success
shaderc-compilation-status-invalid-stage
shaderc-compilation-status-compilation-error
shaderc-compilation-status-internal-error
shaderc-compilation-status-null-result-object
shaderc-compilation-status-invalid-assembly
shaderc-compilation-status-validation-error
shaderc-compilation-status-configuration-error)
(define _get-compilation-status
(foreign-procedure "shaderc_result_get_compilation_status"
((* shaderc-compilation-result)) shaderc-compilation-status))
(define _get-result-length
(foreign-procedure "shaderc_result_get_length" ((* shaderc-compilation-result)) size_t))
(define _get-result-bytes
(foreign-procedure "shaderc_result_get_bytes"
((* shaderc-compilation-result)) (* unsigned-32)))
(define _release-result
(foreign-procedure "shaderc_result_release" ((* shaderc-compilation-result)) void))
(define _release-compiler
(foreign-procedure "shaderc_compiler_release" ((* shaderc-compiler)) void))
(define _release-compile-options
(foreign-procedure "shaderc_compile_options_release" ((* shaderc-compile-options)) void))
;;;;;;;;;;;;;;;;;;;;
;; scheme wrapper ;;
;;;;;;;;;;;;;;;;;;;;
(define file->string
(lambda (filename)
(call-with-input-file filename
(lambda (p)
(let lp ((l (get-line p))
(res '()))
(cond
((eof-object? l) (string-join (reverse res) "\n"))
(else (lp (get-line p)
(cons l res)))))))))
;; (file->string "shaders/shader.vert")
(define compile-shaders
(lambda (filename shader-kind)
;; todo perform cleanup
(let* ((compiler (_init-compiler))
(options (_init-compile-options))
(glsl-code (file->string filename))
(result (_compile->spv compiler
glsl-code
(string-length glsl-code)
shader-kind
filename
"main"
options)))
(cond
((equal? (_get-compilation-status result) 0)
(make-array-pointer (_get-result-length result)
(_get-result-bytes result)
'unsigned-32))
(else (error "shader compilation failed: " (_get-compilation-status result))))))))
;; (load "shaderc.scm")
;; (import (shaderc))
;; (compile-shaders "shaders/shader.vert" shaderc-vertex-shader)
;; (compile-shaders "shaders/shader.frag" shaderc-fragment-shader)