comparison tests/test_vulkan_wrapper.nim @ 122:506090173619

did: implement uniforms, some refactoring
author Sam <sam@basx.dev>
date Sun, 09 Apr 2023 01:04:54 +0700
parents dfaddaf96f09
children 55be3579dc30
comparison
equal deleted inserted replaced
121:dfaddaf96f09 122:506090173619
1 import std/os
2 import std/options 1 import std/options
3 2
4 import semicongine 3 import semicongine
5 4
6 proc diagnostics(instance: Instance) = 5 proc diagnostics(instance: Instance) =
53 selectedPhysicalDevice.filterForGraphicsPresentationQueues() 52 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
54 ) 53 )
55 54
56 # INIT RENDERER: 55 # INIT RENDERER:
57 const 56 const
58 vertexInput = initAttributeGroup( 57 vertexInput = @[
59 attr[Vec3f]("position"), 58 attr[Vec3f]("position"),
60 attr[Vec3f]("color"), 59 attr[Vec3f]("color"),
61 ) 60 attr[Mat4]("transform", perInstance=true)
62 vertexOutput = initAttributeGroup(attr[Vec3f]("outcolor")) 61 ]
63 fragOutput = initAttributeGroup(attr[Vec4f]("color")) 62 vertexOutput = @[attr[Vec3f]("outcolor")]
63 uniforms = @[attr[float32]("time")]
64 fragOutput = @[attr[Vec4f]("color")]
64 vertexCode = compileGlslShader( 65 vertexCode = compileGlslShader(
65 stage=VK_SHADER_STAGE_VERTEX_BIT, 66 stage=VK_SHADER_STAGE_VERTEX_BIT,
66 inputs=vertexInput, 67 inputs=vertexInput,
68 uniforms=uniforms,
67 outputs=vertexOutput, 69 outputs=vertexOutput,
68 body="""gl_Position = vec4(position, 1.0); outcolor = color;""" 70 body="""gl_Position = vec4(position, 1.0); outcolor = color * sin(Uniforms.time) * 0.5 + 0.5;"""
69 ) 71 )
70 fragmentCode = compileGlslShader( 72 fragmentCode = compileGlslShader(
71 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 73 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
72 inputs=vertexOutput, 74 inputs=vertexOutput,
75 uniforms=uniforms,
73 outputs=fragOutput, 76 outputs=fragOutput,
74 body="color = vec4(outcolor, 1);" 77 body="color = vec4(outcolor, 1);"
75 ) 78 )
76 var 79 var
77 vertexshader = device.createShaderModule(vertexCode) 80 vertexshader = device.createShaderModule(vertexCode)
81 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2) 84 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
82 if res != VK_SUCCESS: 85 if res != VK_SUCCESS:
83 raise newException(Exception, "Unable to create swapchain") 86 raise newException(Exception, "Unable to create swapchain")
84 87
85 # INIT SCENE 88 # INIT SCENE
89 var time = initShaderGlobal("time", 0.0'f32)
86 var thescene = Scene( 90 var thescene = Scene(
87 name: "main", 91 name: "main",
88 root: newEntity("root", 92 root: newEntity("root",
93 newEntity("stuff", time),
89 newEntity("triangle1", newMesh( 94 newEntity("triangle1", newMesh(
90 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 95 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
91 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 96 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
92 )), 97 )),
93 newEntity("triangle1b", newMesh( 98 newEntity("triangle1b", newMesh(
117 autoResize=false 122 autoResize=false
118 )), 123 )),
119 ) 124 )
120 ) 125 )
121 thescene.setupDrawables(renderPass) 126 thescene.setupDrawables(renderPass)
127 swapchain.setupUniforms(thescene)
122 128
123 # MAINLOOP 129 # MAINLOOP
124 echo "Setup successfull, start rendering" 130 echo "Setup successfull, start rendering"
125 for i in 0 ..< 10000: 131 for i in 0 ..< 1:
132 setValue[float32](time.value, get[float32](time.value) + 0.0005)
133 echo get[float32](time.value)
126 discard swapchain.drawScene(thescene) 134 discard swapchain.drawScene(thescene)
127 echo "Rendered ", swapchain.framesRendered, " frames" 135 echo "Rendered ", swapchain.framesRendered, " frames"
128 checkVkResult device.vk.vkDeviceWaitIdle() 136 checkVkResult device.vk.vkDeviceWaitIdle()
129 137
130 # cleanup 138 # cleanup
141 149
142 # destroy engine 150 # destroy engine
143 device.destroy() 151 device.destroy()
144 debugger.destroy() 152 debugger.destroy()
145 instance.destroy() 153 instance.destroy()
154 thewindow.destroy()