Mercurial > games > semicongine
annotate tests/hello_triangle.nim @ 1443:768bf1a8407b default tip main
add: hello-triangle example, did: update according readme parts
author | sam <sam@basx.dev> |
---|---|
date | Mon, 24 Feb 2025 10:09:41 +0700 |
parents | |
children |
rev | line source |
---|---|
1443
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import ../semicongine |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
2 import ../semicongine/rendering |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import ../semicongine/input |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
4 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
5 # required |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
6 initEngine("Hello triangle") |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
8 # set up a simple render pass to render the displayed frame |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
9 var renderpass = createDirectPresentationRenderPass( |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
10 depthBuffer = false, samples = VK_SAMPLE_COUNT_1_BIT |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
11 ) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
13 # the swapchain, needs to be attached to the main renderpass |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
14 setupSwapchain(renderpass = renderpass) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
16 # render data is used for memory management on the GPU |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
17 var renderdata = initRenderData() |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
18 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
19 type |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
20 # define a push constant, to have something moving |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
21 PushConstant = object |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
22 scale: float32 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
23 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
24 # This is how we define shaders: the interface needs to be "typed" |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
25 # but the shader code itself can freely be written in glsl |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
26 Shader = object |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
27 position {.VertexAttribute.}: Vec3f |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
28 color {.VertexAttribute.}: Vec3f |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
29 pushConstant {.PushConstant.}: PushConstant |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
30 fragmentColor {.Pass.}: Vec3f |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
31 outColor {.ShaderOutput.}: Vec4f |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
32 # code |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
33 vertexCode: string = |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
34 """void main() { |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
35 fragmentColor = color; |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
36 gl_Position = vec4(position * pushConstant.scale, 1);}""" |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
37 fragmentCode: string = |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
38 """void main() { |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
39 outColor = vec4(fragmentColor, 1);}""" |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
40 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
41 # And we also need to define our Mesh, which does describe the vertex layout |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
42 TriangleMesh = object |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
43 position: GPUArray[Vec3f, VertexBuffer] |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
44 color: GPUArray[Vec3f, VertexBuffer] |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
45 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
46 # instantiate the mesh and fill with data |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
47 var mesh = TriangleMesh( |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
48 position: asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
49 color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
50 ) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
52 # this allocates GPU data, uploads the data to the GPU and flushes any thing that is host-cached |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
53 # this is a shortcut version, more fine-grained control is possible |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
54 assignBuffers(renderdata, mesh) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
55 renderdata.flushAllMemory() |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
56 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
57 # Now we need to instantiate the shader as a pipeline object that is attached to a renderpass |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
58 var pipeline = createPipeline(Shader(), renderPass = renderPass) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
59 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
60 # the main render-loop will exit if we get a kill-signal from the OS |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
61 while updateInputs(): |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
62 # starts the drawing for the next frame and provides us necesseary framebuffer and commandbuffer objects in this scope |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
63 withNextFrame(framebuffer, commandbuffer): |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
64 # start the main (and only) renderpass we have, needs to know the target framebuffer and a commandbuffer |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
65 withRenderPass( |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
66 renderPass, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
67 framebuffer, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
68 commandbuffer, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
69 frameWidth(), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
70 frameHeight(), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
71 vec4(0, 0, 0, 0), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
72 ): |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
73 # now activate our shader-pipeline |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
74 withPipeline(commandbuffer, pipeline): |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
75 # and finally, draw the mesh and set a single parameter |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
76 # more complicated setups with descriptors/uniforms are of course possible |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
77 renderWithPushConstant( |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
78 commandbuffer = commandbuffer, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
79 pipeline = pipeline, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
80 mesh = mesh, |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
81 pushConstant = PushConstant(scale: 0.3), |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
82 ) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
83 |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
84 # cleanup |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
85 checkVkResult vkDeviceWaitIdle(engine().vulkan.device) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
86 destroyPipeline(pipeline) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
87 destroyRenderData(renderdata) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
88 destroyRenderPass(renderpass) |
768bf1a8407b
add: hello-triangle example, did: update according readme parts
sam <sam@basx.dev>
parents:
diff
changeset
|
89 destroyVulkan() |