annotate examples/E02_squares.nim @ 592:cda5874a8454

add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
author Sam <sam@basx.dev>
date Wed, 19 Apr 2023 01:45:16 +0700
parents 21c276c0a968
children fdae5f50fa00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/times
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/math
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/random
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
5
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
6 import semicongine
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
7
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
8 type
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
9 VertexDataA = object
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
10 position11: PositionAttribute[Vec2]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
11 color22: ColorAttribute[Vec3]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
12 index: GenericAttribute[uint32]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
13 Uniforms = object
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
14 t: Descriptor[float32]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
15
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
16 var
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
17 pipeline: RenderPipeline[VertexDataA, Uniforms]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
18 uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
19
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents: 529
diff changeset
20 proc globalUpdate(engine: var Engine, t, dt: float32) =
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
21 uniformdata.t.value += dt
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
22 engine.vulkan.device.updateUniformData(pipeline, uniformdata)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
23
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
24 when isMainModule:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
25 randomize()
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
26 var myengine = igniteEngine("Squares")
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
27 const
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
28 COLUMNS = 10
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
29 ROWS = 10
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
30 WIDTH = 2'f32 / COLUMNS
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
31 HEIGHT = 2'f32 / ROWS
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
32 var
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
33 vertices: array[COLUMNS * ROWS * 4, Vec2]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
34 colors: array[COLUMNS * ROWS * 4, Vec3]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
35 iValues: array[COLUMNS * ROWS * 4, uint32]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
36 indices: array[COLUMNS * ROWS * 2, array[3, uint16]]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
37
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
38 for row in 0 ..< ROWS:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
39 for col in 0 ..< COLUMNS:
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
40 let
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
41 y: float32 = (row * 2 / COLUMNS) - 1
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
42 x: float32 = (col * 2 / ROWS) - 1
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
43 color = Vec3([(x + 1) / 2, (y + 1) / 2, 0'f32])
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
44 squareIndex = row * COLUMNS + col
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
45 vertIndex = squareIndex * 4
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
46 vertices[vertIndex + 0] = Vec2([x, y])
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
47 vertices[vertIndex + 1] = Vec2([x + WIDTH, y])
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
48 vertices[vertIndex + 2] = Vec2([x + WIDTH, y + HEIGHT])
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
49 vertices[vertIndex + 3] = Vec2([x, y + HEIGHT])
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
50 colors[vertIndex + 0] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
51 colors[vertIndex + 1] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
52 colors[vertIndex + 2] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
53 colors[vertIndex + 3] = color
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
54 iValues[vertIndex + 0] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
55 iValues[vertIndex + 1] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
56 iValues[vertIndex + 2] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
57 iValues[vertIndex + 3] = uint32(squareIndex)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
58 indices[squareIndex * 2 + 0] = [uint16(vertIndex + 0), uint16(vertIndex +
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
59 1), uint16(vertIndex + 2)]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
60 indices[squareIndex * 2 + 1] = [uint16(vertIndex + 2), uint16(vertIndex +
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
61 3), uint16(vertIndex + 0)]
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
62
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
63
529
eb92723db7b2 fix: API changes
Sam <sam@basx.dev>
parents: 522
diff changeset
64 type PIndexedMesh = Mesh[VertexDataA,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
65 uint16] # required so we can use ctor with ref/on heap
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
66 var squaremesh = PIndexedMesh(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
67 vertexData: VertexDataA(
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
68 position11: PositionAttribute[Vec2](data: @vertices),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
69 color22: ColorAttribute[Vec3](data: @colors),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
70 index: GenericAttribute[uint32](data: @iValues),
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
71 ),
529
eb92723db7b2 fix: API changes
Sam <sam@basx.dev>
parents: 522
diff changeset
72 indexed: true,
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
73 indices: @indices
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
74 )
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
75 var scene = newThing("scene", newThing("squares", squaremesh))
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
76
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
77 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
78 """
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
79 float pos_weight = index / 100.0; // add some gamma correction?
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
80 float t = sin(uniforms.t * 0.5) * 0.5 + 0.5;
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
81 float v = min(1, max(0, pow(pos_weight - t, 2)));
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
82 v = pow(1 - v, 3000);
529
eb92723db7b2 fix: API changes
Sam <sam@basx.dev>
parents: 522
diff changeset
83 out_color = vec4(in_color.r, in_color.g, v * 0.5, 1.0);
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
84 """
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
85 )
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
86 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
529
eb92723db7b2 fix: API changes
Sam <sam@basx.dev>
parents: 522
diff changeset
87 static:
eb92723db7b2 fix: API changes
Sam <sam@basx.dev>
parents: 522
diff changeset
88 echo vertexShader
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
89 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
90 myengine,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
91 scene,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
92 vertexShader,
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
93 fragmentShader
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
94 )
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
95 myengine.run(pipeline, globalUpdate)
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
96 pipeline.trash()
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
97 myengine.trash()