comparison tests/test_vulkan_wrapper.nim @ 124:cb9e27a30165

fix: completely overhole buffer handling for drawing, fix shit
author Sam <sam@basx.dev>
date Mon, 10 Apr 2023 20:09:37 +0700
parents 55be3579dc30
children 6e2c48cb6f60
comparison
equal deleted inserted replaced
123:55be3579dc30 124:cb9e27a30165
31 echo " " & $mode 31 echo " " & $mode
32 echo " Surface formats" 32 echo " Surface formats"
33 for format in device.getSurfaceFormats(): 33 for format in device.getSurfaceFormats():
34 echo " " & $format 34 echo " " & $format
35 35
36 when isMainModule: 36 proc scene_different_mesh_types(): Scene =
37 # INIT ENGINE: 37 result = Scene(
38 # create instance
39 var thewindow = createWindow("Test")
40 var instance = thewindow.createInstance(
41 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
42 instanceExtensions= @["VK_EXT_debug_utils"],
43 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
44 )
45 var debugger = instance.createDebugMessenger()
46 # create devices
47 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
48 var device = instance.createDevice(
49 selectedPhysicalDevice,
50 @[],
51 @["VK_EXT_index_type_uint8"],
52 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
53 )
54
55 # INIT RENDERER:
56 const
57 vertexInput = @[
58 attr[Vec3f]("position"),
59 attr[Vec3f]("color"),
60 attr[Vec3f]("translate", perInstance=true)
61 ]
62 vertexOutput = @[attr[Vec3f]("outcolor")]
63 uniforms = @[attr[float32]("time")]
64 fragOutput = @[attr[Vec4f]("color")]
65 vertexCode = compileGlslShader(
66 stage=VK_SHADER_STAGE_VERTEX_BIT,
67 inputs=vertexInput,
68 uniforms=uniforms,
69 outputs=vertexOutput,
70 body="""gl_Position = vec4(position, 1.0); outcolor = color * sin(Uniforms.time) * 0.5 + 0.5;"""
71 )
72 fragmentCode = compileGlslShader(
73 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
74 inputs=vertexOutput,
75 uniforms=uniforms,
76 outputs=fragOutput,
77 body="color = vec4(outcolor, 1);"
78 )
79 var
80 vertexshader = device.createShaderModule(vertexCode)
81 fragmentshader = device.createShaderModule(fragmentCode)
82 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
83 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
84 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
85 if res != VK_SUCCESS:
86 raise newException(Exception, "Unable to create swapchain")
87
88 # INIT SCENE
89 var time = initShaderGlobal("time", 0.0'f32)
90 #[
91 var thescene = Scene(
92 name: "main", 38 name: "main",
93 root: newEntity("root", 39 root: newEntity("root",
94 newEntity("stuff", time),
95 newEntity("triangle1", newMesh( 40 newEntity("triangle1", newMesh(
96 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 41 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
97 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 42 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
98 )), 43 )),
99 newEntity("triangle1b", newMesh( 44 newEntity("triangle1b", newMesh(
122 indices=[[0'u32, 2'u32, 1'u32]], 67 indices=[[0'u32, 2'u32, 1'u32]],
123 autoResize=false 68 autoResize=false
124 )), 69 )),
125 ) 70 )
126 ) 71 )
127 ]# 72
128 var mymesh = newMesh( 73 proc scene_simple(): Scene =
74 var mymesh1 = newMesh(
75 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)],
76 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
77 )
78 var mymesh2 = newMesh(
129 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 79 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
130 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 80 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)],
131 ) 81 )
132 setInstanceData[Vec3f](mymesh, "translate", @[newVec3f(0.3, 0.3)]) 82 var mymesh3 = newMesh(
133 var thescene = Scene( 83 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)],
84 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)],
85 indices=[[0'u32, 1'u32, 2'u32]],
86 autoResize=false
87 )
88 var mymesh4 = newMesh(
89 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)],
90 colors=[newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0)],
91 indices=[[0'u16, 1'u16, 2'u16]],
92 )
93 setMeshData[Vec3f](mymesh1, "translate", @[newVec3f(0.3, 0.3)])
94 result = Scene(
134 name: "main", 95 name: "main",
135 root: newEntity("root", 96 root: newEntity("root", newEntity("triangle", mymesh4, mymesh3, mymesh2, mymesh1),)
136 newEntity("stuff", time), 97 )
137 newEntity("triangle", mymesh), 98
99
100 when isMainModule:
101 # INIT ENGINE:
102 # create instance
103 var thewindow = createWindow("Test")
104 var instance = thewindow.createInstance(
105 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
106 instanceExtensions= @["VK_EXT_debug_utils"],
107 layers= @["VK_LAYER_KHRONOS_validation", "VK_LAYER_MESA_overlay"]
108 )
109 var debugger = instance.createDebugMessenger()
110 # create devices
111 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
112 var device = instance.createDevice(
113 selectedPhysicalDevice,
114 @[],
115 @["VK_EXT_index_type_uint8"],
116 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
117 )
118
119 # INIT RENDERER:
120 const
121 vertexInput = @[
122 attr[Vec3f]("position", memoryLocation=VRAM),
123 attr[Vec3f]("color", memoryLocation=VRAM),
124 # attr[Vec3f]("translate", perInstance=true)
125 ]
126 vertexOutput = @[attr[Vec3f]("outcolor")]
127 uniforms = @[attr[float32]("time")]
128 fragOutput = @[attr[Vec4f]("color")]
129 vertexCode = compileGlslShader(
130 stage=VK_SHADER_STAGE_VERTEX_BIT,
131 inputs=vertexInput,
132 uniforms=uniforms,
133 outputs=vertexOutput,
134 body="""gl_Position = vec4(position, 1.0); outcolor = color * sin(Uniforms.time) * 0.5 + 0.5;"""
138 ) 135 )
139 ) 136 fragmentCode = compileGlslShader(
137 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
138 inputs=vertexOutput,
139 uniforms=uniforms,
140 outputs=fragOutput,
141 body="color = vec4(outcolor, 1);"
142 )
143 var
144 vertexshader = device.createShaderModule(vertexCode)
145 fragmentshader = device.createShaderModule(fragmentCode)
146 surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
147 renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
148 (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
149 if res != VK_SUCCESS:
150 raise newException(Exception, "Unable to create swapchain")
151
152 # INIT SCENE
153 var time = initShaderGlobal("time", 0.0'f32)
154
155 var thescene = scene_simple()
156 thescene.root.components.add time
140 thescene.setupDrawables(renderPass) 157 thescene.setupDrawables(renderPass)
141 swapchain.setupUniforms(thescene) 158 swapchain.setupUniforms(thescene)
142 159
143 # MAINLOOP 160 # MAINLOOP
144 echo "Setup successfull, start rendering" 161 echo "Setup successfull, start rendering"
145 for i in 0 ..< 1: 162 for i in 0 ..< 10000:
146 setValue[float32](time.value, get[float32](time.value) + 0.0005) 163 setValue[float32](time.value, get[float32](time.value) + 0.0005)
147 echo get[float32](time.value)
148 discard swapchain.drawScene(thescene) 164 discard swapchain.drawScene(thescene)
149 echo "Rendered ", swapchain.framesRendered, " frames" 165 echo "Rendered ", swapchain.framesRendered, " frames"
150 checkVkResult device.vk.vkDeviceWaitIdle() 166 checkVkResult device.vk.vkDeviceWaitIdle()
151 167
152 # cleanup 168 # cleanup