Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 563:fb42da98c1aa
did: update to use new shader concept
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 17 Mar 2023 01:11:58 +0700 |
parents | 8de8a2102071 |
children | 1e2027dfc642 |
rev | line source |
---|---|
557 | 1 import std/options |
2 | |
554 | 3 import semicongine/vulkan |
555 | 4 import semicongine/platform/window |
559 | 5 import semicongine/math |
554 | 6 |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
7 type |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
8 Vertex = object |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
9 pos: Vec3 |
563 | 10 Uniforms = object |
11 time: float32 | |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
12 |
554 | 13 |
14 when isMainModule: | |
15 # print basic driver infos | |
16 echo "Layers" | |
17 for layer in getLayers(): | |
18 echo " " & layer | |
19 echo "Instance extensions" | |
20 for extension in getInstanceExtensions(): | |
21 echo " " & extension | |
22 | |
23 # create instance | |
557 | 24 var thewindow = createWindow("Test") |
25 var instance = thewindow.createInstance( | |
554 | 26 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), |
555 | 27 instanceExtensions= @["VK_EXT_debug_utils"], |
28 layers= @["VK_LAYER_KHRONOS_validation"] | |
554 | 29 ) |
30 var debugger = instance.createDebugMessenger() | |
555 | 31 |
32 # diagnostic output | |
554 | 33 echo "Devices" |
34 for device in instance.getPhysicalDevices(): | |
35 echo " " & $device | |
557 | 36 echo " Rating: " & $device.rateGraphics() |
554 | 37 echo " Extensions" |
38 for extension in device.getExtensions(): | |
39 echo " " & $extension | |
557 | 40 echo " Properties" |
41 echo " " & $device.getProperties() | |
42 echo " Features" | |
43 echo " " & $device.getFeatures() | |
554 | 44 echo " Queue families" |
45 for queueFamily in device.getQueueFamilies(): | |
46 echo " " & $queueFamily | |
555 | 47 echo " Surface present modes" |
557 | 48 for mode in device.getSurfacePresentModes(): |
555 | 49 echo " " & $mode |
50 echo " Surface formats" | |
557 | 51 for format in device.getSurfaceFormats(): |
555 | 52 echo " " & $format |
554 | 53 |
54 # create devices | |
557 | 55 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
56 var device = instance.createDevice( | |
57 selectedPhysicalDevice, | |
58 @[], | |
59 @[], | |
60 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
61 ) | |
62 | |
559 | 63 # setup render pipeline |
557 | 64 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()) |
65 if res != VK_SUCCESS: | |
66 raise newException(Exception, "Unable to create swapchain") | |
559 | 67 var renderpass = device.simpleForwardRenderPass(swapchain.format) |
68 var framebuffers: seq[Framebuffer] | |
69 for imageview in swapchain.imageviews: | |
70 framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension) | |
71 | |
72 # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first | |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
73 var |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
74 commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1) |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
75 imageAvailable = device.createSemaphore() |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
76 renderFinished = device.createSemaphore() |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
77 inflight = device.createFence() |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
78 |
563 | 79 var vertexshader = shader(Vertex, Uniforms, device): |
80 shadertype: VK_SHADER_STAGE_VERTEX_BIT | |
81 entrypoint: "main" | |
82 version: 450 | |
83 code: """""" | |
84 var fragmentshader = shader(Vertex, Uniforms, device): | |
85 shadertype: VK_SHADER_STAGE_FRAGMENT_BIT | |
86 entrypoint: "main" | |
87 version: 450 | |
88 code: "" | |
89 | |
90 #var vertexshader = loadShaderCode[Vertex, Uniforms](device, vertexshadercode) | |
91 #var fragmentshader = loadShaderCode[Vertex, Uniforms](device, fragmentshadercode) | |
92 #var pipeline = renderpass.createPipeline(vertexshaderhandle, fragmentshaderhandle) | |
557 | 93 |
94 echo "All successfull" | |
95 echo "Start cleanup" | |
554 | 96 |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
97 # cleanup |
563 | 98 #pipeline.destroy() |
99 #vertexshader.destroy() | |
100 #fragmentshader.destroy() | |
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
101 inflight.destroy() |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
102 imageAvailable.destroy() |
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
103 renderFinished.destroy() |
559 | 104 commandPool.destroy() |
105 for fb in framebuffers.mitems: | |
106 fb.destroy() | |
107 renderpass.destroy() | |
557 | 108 swapchain.destroy() |
109 device.destroy() | |
554 | 110 |
111 debugger.destroy() | |
112 instance.destroy() |