Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 102:c782d7e52b25
did: update to use new shader concept
| author | Sam <sam@basx.dev> |
|---|---|
| date | Fri, 17 Mar 2023 01:11:58 +0700 |
| parents | 4deffc94484a |
| children | 1e2027dfc642 |
| rev | line source |
|---|---|
| 96 | 1 import std/options |
| 2 | |
| 93 | 3 import semicongine/vulkan |
| 94 | 4 import semicongine/platform/window |
| 98 | 5 import semicongine/math |
| 93 | 6 |
|
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
7 type |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
8 Vertex = object |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
9 pos: Vec3 |
| 102 | 10 Uniforms = object |
| 11 time: float32 | |
|
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
12 |
| 93 | 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 | |
| 96 | 24 var thewindow = createWindow("Test") |
| 25 var instance = thewindow.createInstance( | |
| 93 | 26 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), |
| 94 | 27 instanceExtensions= @["VK_EXT_debug_utils"], |
| 28 layers= @["VK_LAYER_KHRONOS_validation"] | |
| 93 | 29 ) |
| 30 var debugger = instance.createDebugMessenger() | |
| 94 | 31 |
| 32 # diagnostic output | |
| 93 | 33 echo "Devices" |
| 34 for device in instance.getPhysicalDevices(): | |
| 35 echo " " & $device | |
| 96 | 36 echo " Rating: " & $device.rateGraphics() |
| 93 | 37 echo " Extensions" |
| 38 for extension in device.getExtensions(): | |
| 39 echo " " & $extension | |
| 96 | 40 echo " Properties" |
| 41 echo " " & $device.getProperties() | |
| 42 echo " Features" | |
| 43 echo " " & $device.getFeatures() | |
| 93 | 44 echo " Queue families" |
| 45 for queueFamily in device.getQueueFamilies(): | |
| 46 echo " " & $queueFamily | |
| 94 | 47 echo " Surface present modes" |
| 96 | 48 for mode in device.getSurfacePresentModes(): |
| 94 | 49 echo " " & $mode |
| 50 echo " Surface formats" | |
| 96 | 51 for format in device.getSurfaceFormats(): |
| 94 | 52 echo " " & $format |
| 93 | 53 |
| 54 # create devices | |
| 96 | 55 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
| 56 var device = instance.createDevice( | |
| 57 selectedPhysicalDevice, | |
| 58 @[], | |
| 59 @[], | |
| 60 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
| 61 ) | |
| 62 | |
| 98 | 63 # setup render pipeline |
| 96 | 64 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()) |
| 65 if res != VK_SUCCESS: | |
| 66 raise newException(Exception, "Unable to create swapchain") | |
| 98 | 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 | |
|
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
73 var |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
74 commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1) |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
75 imageAvailable = device.createSemaphore() |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
76 renderFinished = device.createSemaphore() |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
77 inflight = device.createFence() |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
78 |
| 102 | 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) | |
| 96 | 93 |
| 94 echo "All successfull" | |
| 95 echo "Start cleanup" | |
| 93 | 96 |
|
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
97 # cleanup |
| 102 | 98 #pipeline.destroy() |
| 99 #vertexshader.destroy() | |
| 100 #fragmentshader.destroy() | |
|
99
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
101 inflight.destroy() |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
102 imageAvailable.destroy() |
|
4deffc94484a
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
98
diff
changeset
|
103 renderFinished.destroy() |
| 98 | 104 commandPool.destroy() |
| 105 for fb in framebuffers.mitems: | |
| 106 fb.destroy() | |
| 107 renderpass.destroy() | |
| 96 | 108 swapchain.destroy() |
| 109 device.destroy() | |
| 93 | 110 |
| 111 debugger.destroy() | |
| 112 instance.destroy() |
