-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello_triangle.golem
396 lines (319 loc) · 12.1 KB
/
hello_triangle.golem
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import("sdl2")
import("gl")
addCFlags("-Werror-implicit-function-declaration")
type VertexArrayObject = struct {
handle: u32
}
type VertexArrayObjectBinding = struct {
index: u32
}
type UniformBufType_436214163 = struct {
#mvp: Mat[4, 4, float32]
mvp: array(16,f32)
}
type Location = struct {
index: i32
}
trait Any(T) = {}
type ArrayBuffer[T] = struct {
handle: u32
}
# type openarray[T] = struct {
# len: int
# data: ptr T
# }
type LineInfo = struct {
filename: str
line: int
column: int
}
type Program = struct {
handle: u32
}
type Shader = struct {
handle: u32
}
type Pipeline = struct {
program: Program
vao: VertexArrayObject
uniformBufferHandle: u32 # GLuint
uniformBufferData: pointer
uniformBufferSize: i32 #GLint
}
proc "importc" strlen(str: cstr) -> uint;
proc "importc" malloc(size: uint) -> pointer;
proc makeString(cstr: cstr) -> str = {
# TODO not implemented
""
}
proc shaderSource(shader: Shader, source: cstr) -> void = {
var source: cstr = source
var length: i32 = i32(strlen(source))
glShaderSource(shader.handle, 1, addr(source), addr(length))
}
proc compileStatus(shader: Shader) -> bool = {
var status: i32
glGetShaderiv(shader.handle, GL_COMPILE_STATUS, addr(status))
status != 0
}
proc linkStatus(program: Program) -> bool = {
var status: i32 # GLint
glGetProgramiv(program.handle, GL_LINK_STATUS, addr(status))
status != 0
}
proc shaderInfoLog(shader: Shader) -> str = {
var length: i32 = 0
glGetShaderiv(shader.handle, GL_INFO_LOG_LENGTH, addr(length))
# let result = newString(length.int)
# glGetShaderInfoLog(shader.handle, length, nil, result)
"" # TODO implement this
}
# type GLenum = u32
proc showError(a: str, b: cstr, d: LineInfo) -> void = {}
proc compileShader(shaderType: u32, source: cstr, lineinfo: LineInfo) -> Shader = {
var result: Shader
result.handle = glCreateShader(shaderType)
shaderSource(result, source)
glCompileShader(result.handle)
if not compileStatus(result) do {
showError(shaderInfoLog(result), source, lineinfo)
}
result
}
proc attachAndDeleteShader(program: Program, shader: Shader) -> void = {
glAttachShader(program.handle, shader.handle)
glDeleteShader(shader.handle)
}
proc infoLog(program: Program) -> str = {
var length = 0:i32 #GLint = 0
glGetProgramiv(program.handle, GL_INFO_LOG_LENGTH, addr(length));
#result = newString(length.int)
#glGetProgramInfoLog(program.handle, length, nil, result);
"TODO *infoLog* not yet implemented"
}
proc linkOrDelete(program: Program) -> void = {
glLinkProgram(program.handle)
if not linkStatus(program) do {
printf("Log: %s\n", infoLog(program))
glDeleteProgram(program.handle)
}
}
## Typed wrapper around `glCreateVertexArrays`.
proc createVertexArrayObject(label: str) -> VertexArrayObject = {
var result: VertexArrayObject
glCreateVertexArrays(1, addr(result.handle))
if len(label) > 0 do {
glObjectLabel(GL_VERTEX_ARRAY, result.handle, i32(len(label)), cstr(label))
}
result
}
proc attributeLocation(program: Program, name: str) -> Location = [index = glGetAttribLocation(program.handle, cstr(name))]
type OffsetAlign = struct {
offset: i32
align: i32
}
trait HasSizeOf(T) = {
proc sizeof(_: T) -> i64
}
# TODO enable this
# proc write[T : HasSizeOf](dst: pointer, var offset: i32, value: T) -> void = {
# (cast(type ptr T, cast(type uint, dst) + cast(type uint, offset)))[] = value
# offset += i32(sizeof(value))
# }
# template write(dst: pointer, var offset: i32, value: f32): void = {
# indexOp((dist | cast(type uint) + offset | cast(type uint)) | cast(type ptr f32)) = value
# offset += sizeof(value) | conv(type i32)
# }
proc write(dst: pointer, var offset: i32, value: array(16, f32)) -> void = {
indexOp((dst | cast(type(uint)) + offset | cast(type(uint))) | cast(type(ptr array(16, f32)))) = value
offset += sizeof(value) | conv(type(i32))
}
proc write(dst: pointer, var offset: i32, value: f32) -> void = {
indexOp((dst | cast(type(uint)) + offset | cast(type(uint))) | cast(type(ptr f32))) = value
offset += sizeof(value) | conv(type(i32))
}
proc alignFunc(address, alignment: i32) -> i32 = bitand(address + (alignment - 1), bitnot(alignment - 1))
# proc std140AlignedWrite(dst: pointer, offset: i32, value: f32): OffsetAlign = {
# var result: OffsetAlign = [offset = alignFunc(offset, 4), align = 4]
# write(dst, result.offset, u32(value))
# }
# proc std140AlignedWrite*(dst: pointer, offset: int32, value: SomeNumber): tuple[offset, align: int32] =
# result.align = max(sizeof(SomeNumber), 4)
# result.offset = alignFunc(offset, result.align)
# debug result
# dst.write(result.offset, value)
#proc max(a,b: i64): i64 = if a > b do a else b
proc max(a,b: i32) -> i32 = {if a > b do a else b}
proc std140AlignedWrite(dst: pointer, offset: i32, value: array(16, f32)) -> OffsetAlign = {
var result: OffsetAlign
result.align = 16
result.offset = alignFunc(offset, result.align)
write(dst, result.offset, value)
result
}
proc std140AlignedWrite(dst: pointer, offset: i32, uniformObject: UniformBufType_436214163) -> OffsetAlign = {
var result: OffsetAlign
result.align = 4
result.offset = alignFunc(offset, 4)
# write(dst, result.offset, uint32(value))
std140AlignedWrite(dst, offset, uniformObject.mvp)
}
# TODO should be ArrayBuffer[T]
proc new[U](var arrayBuffer: ArrayBuffer[U]) -> void = {
glCreateBuffers(1, addr(arrayBuffer.handle))
}
## Wrapper for `glNamedBufferData`.
proc bufferData[HasSizeOf(X)](buffer: ArrayBuffer[X], data: openarray(X), usage: GLenum) -> void = {
if int(buffer.handle) > 0 do {
glNamedBufferData(buffer.handle,
len(data) * sizeof(type(X)) | conv(type(GLsizeiptr)),
data(data) | conv(type(pointer)),
usage
)
}
}
# TODO, this should be ArrayBuffer[T]
proc arrayBuffer[HasSizeOf(T)](data: openarray(T), usage: GLenum, label: str) -> ArrayBuffer[T] = {
var result: ArrayBuffer[T]
result | new
result | bufferData(data, usage)
if len(label) > 0 do {
glObjectLabel(GL_BUFFER, result.handle, len(label) | conv(type(GLsizei)), cstr(label))
}
return result
}
proc main() -> i32 = {
let window = SDL_CreateWindow("hello triangle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL)
assert(window != nil)
# SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4)
# SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6)
# SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG)
# SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)
let context: SDL_GLContext = SDL_GL_CreateContext(window)
var running: bool = true
let vertices = arrayBuffer([
[-1,-1, 0, 1]:f32x4,
[ 1,-1, 0, 1],
[ 0, 1, 0, 1]
], GL_STATIC_DRAW, "vertices":str)
let colors = arrayBuffer([
[ 1, 0,0,1]:f32x4,
[0, 1,0,1],
[0,0,1,1]
]:openarray(f32x4), GL_STATIC_DRAW, "colors":str)
# var buffers : array(2, u32)
var vertexData = [-1:f32,-1,0,1, 1,-1,0,1, 0,1,0,1]
var colorData = [ 1:f32, 0,0,0, 0, 1,0,0, 0,0,1,0]
# glCreateBuffers(2, addr(buffers[0]))
# glNamedBufferStorage(buffers[0], len(vertexData) * sizeof(vertexData[0]), pointer(addr(vertexData[0])), 0)
# glNamedBufferStorage(buffers[1], len(colorData) * sizeof(colorData[0]), pointer(addr(colorData[0])), 0)
# let vertexData = [-1:f32,-1,0,1, 1,-1,0,1, 0,1,0,1]
# let colorData = [ 1:f32, 0,0,0, 0, 1,0,0, 0,0,1,0]
var p_436214159: Pipeline
var vao_436214160: VertexArrayObject
if p_436214159.program.handle == 0 do {
p_436214159.program.handle = glCreateProgram()
attachAndDeleteShader(p_436214159.program, compileShader(GL_VERTEX_SHADER, "#version 440
#extension GL_ARB_enhanced_layouts : enable
#define M_PI 3.1415926535897932384626433832795
layout(std140, binding=0) uniform dynamic_shader_data {
mat4x4 mvp;
};
in vec4 a_vertex;
in vec4 a_color;
out vec4 v_color;
void main() {
////////////////////////////////////////////////////////////////////////////////
// user code begins here //
gl_Position = mvp * a_vertex;
v_color = a_color;
// user code ends here //
////////////////////////////////////////////////////////////////////////////////
}
", [filename= "hello_triangle.golem", line= 124 , column= 6]))
attachAndDeleteShader(p_436214159.program, compileShader(GL_FRAGMENT_SHADER, "#version 440
#extension GL_ARB_enhanced_layouts : enable
#define M_PI 3.1415926535897932384626433832795
layout(std140, binding=0) uniform dynamic_shader_data {
mat4x4 mvp;
};
in vec4 v_color;
layout(location = 0) out vec4 color;
void main() {
////////////////////////////////////////////////////////////////////////////////
// user code begins here //
color = v_color;
// user code ends here //
////////////////////////////////////////////////////////////////////////////////
}
", [filename= "hello_triangle.golem", line= 73, column= 6]))
linkOrDelete(p_436214159.program)
glUseProgram(p_436214159.program.handle)
vao_436214160 = createVertexArrayObject("")
let uniformBlockIndex = glGetUniformBlockIndex(p_436214159.program.handle, "dynamic_shader_data")
const loc: LineInfo = [filename= "nimgen.nim", line= 31, column= 15]
# bind(instantiationInfo)
# mixin(failedAssertImpl)
assert(uniformBlockIndex != GL_INVALID_INDEX)
var blockSize_436214162: i32 # GLint
glGetActiveUniformBlockiv(p_436214159.program.handle, uniformBlockIndex, 35392, addr(blockSize_436214162))
p_436214159.uniformBufferSize = blockSize_436214162
glCreateBuffers(1, addr(p_436214159.uniformBufferHandle))
glNamedBufferStorage(p_436214159.uniformBufferHandle, int(blockSize_436214162), nil, 256)
p_436214159.uniformBufferData = malloc(u64(blockSize_436214162)) # allocImpl(blockSize_436214162)
glEnableVertexArrayAttrib(vao_436214160.handle, 0)
glVertexArrayBindingDivisor(vao_436214160.handle, 0, 0)
glVertexArrayAttribFormat(vao_436214160.handle, 0, 4, 5126, false, 0)
let location1 = attributeLocation(p_436214159.program, "a_vertex")
if location1.index < 0 do {
printf("unused attribute: a_vertex\n")
} else {
glVertexArrayAttribBinding(vao_436214160.handle, 0, u32(location1.index))
}
glEnableVertexArrayAttrib(vao_436214160.handle, 1)
glVertexArrayBindingDivisor(vao_436214160.handle, 1, 0)
glVertexArrayAttribFormat(vao_436214160.handle, 1, 4, 5126, false, 0)
let location2 = attributeLocation(p_436214159.program, "a_color")
if location2.index < 0 do {
printf("unused attribute: a_color\n")
} else {
glVertexArrayAttribBinding(vao_436214160.handle, 1, u32(location2.index))
}
}
let mvp : array(16, f32) = [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1] # identity
glUseProgram(p_436214159.program.handle)
glBindVertexArray(vao_436214160.handle)
var uniformObject_436214161: UniformBufType_436214163
uniformObject_436214161.mvp = mvp
discard std140AlignedWrite(p_436214159.uniformBufferData, 0, uniformObject_436214161)
glNamedBufferSubData(p_436214159.uniformBufferHandle, 0, i64(p_436214159.uniformBufferSize), p_436214159.uniformBufferData)
glBindBufferBase(35345, 0, p_436214159.uniformBufferHandle)
var buffers: array(2, GLuint) = [vertices.handle, colors.handle]
var offsets: array(2, GLintptr) = [GLintptr(totalOffset(vertices)), int(totalOffset(colors))]
var strides: array(2, GLsizei) = [16:GLsizei, 16:GLsizei]
glVertexArrayVertexBuffers(vao_436214160.handle, 0, 2:GLsizei, addr(buffers[0]), addr(offsets[0]), addr(strides[0]))
glDrawArrays(4, 0, 3:GLsizei)
glBindVertexArray(0)
glUseProgram(0)
while running do {
var event: SDL_Event
while SDL_PollEvent(addr(event)) do {
if event.`type` == SDL_QUIT do {
running = false
} elif event.`type` == SDL_KEYDOWN and event.key.keysym.scancode == SDL_SCANCODE_ESCAPE do {
running = false
}
}
glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)
glBegin(GL_TRIANGLES)
glColor3f(1,0,0)
glVertex2f(-1,-1)
glColor3f(0,1,0)
glVertex2f(1,-1)
glColor3f(0,0,1)
glVertex2f(0,1)
glEnd()
SDL_GL_SwapWindow(window)
}
return 0
}