Mercurial > games > semicongine
annotate tests/test_vulkan_wrapper.nim @ 561:4a9fc283c3af
add: initial version of better shader-shit
| author | Sam <sam@basx.dev> |
|---|---|
| date | Fri, 17 Mar 2023 01:11:18 +0700 |
| parents | 8de8a2102071 |
| children | c782d7e52b25 |
| 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 |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
10 |
| 554 | 11 |
| 12 when isMainModule: | |
| 13 # print basic driver infos | |
| 14 echo "Layers" | |
| 15 for layer in getLayers(): | |
| 16 echo " " & layer | |
| 17 echo "Instance extensions" | |
| 18 for extension in getInstanceExtensions(): | |
| 19 echo " " & extension | |
| 20 | |
| 21 # create instance | |
| 557 | 22 var thewindow = createWindow("Test") |
| 23 var instance = thewindow.createInstance( | |
| 554 | 24 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), |
| 555 | 25 instanceExtensions= @["VK_EXT_debug_utils"], |
| 26 layers= @["VK_LAYER_KHRONOS_validation"] | |
| 554 | 27 ) |
| 28 var debugger = instance.createDebugMessenger() | |
| 555 | 29 |
| 30 # diagnostic output | |
| 554 | 31 echo "Devices" |
| 32 for device in instance.getPhysicalDevices(): | |
| 33 echo " " & $device | |
| 557 | 34 echo " Rating: " & $device.rateGraphics() |
| 554 | 35 echo " Extensions" |
| 36 for extension in device.getExtensions(): | |
| 37 echo " " & $extension | |
| 557 | 38 echo " Properties" |
| 39 echo " " & $device.getProperties() | |
| 40 echo " Features" | |
| 41 echo " " & $device.getFeatures() | |
| 554 | 42 echo " Queue families" |
| 43 for queueFamily in device.getQueueFamilies(): | |
| 44 echo " " & $queueFamily | |
| 555 | 45 echo " Surface present modes" |
| 557 | 46 for mode in device.getSurfacePresentModes(): |
| 555 | 47 echo " " & $mode |
| 48 echo " Surface formats" | |
| 557 | 49 for format in device.getSurfaceFormats(): |
| 555 | 50 echo " " & $format |
| 554 | 51 |
| 52 # create devices | |
| 557 | 53 let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics() |
| 54 var device = instance.createDevice( | |
| 55 selectedPhysicalDevice, | |
| 56 @[], | |
| 57 @[], | |
| 58 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
| 59 ) | |
| 60 | |
| 559 | 61 # setup render pipeline |
| 557 | 62 var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()) |
| 63 if res != VK_SUCCESS: | |
| 64 raise newException(Exception, "Unable to create swapchain") | |
| 559 | 65 var renderpass = device.simpleForwardRenderPass(swapchain.format) |
| 66 var framebuffers: seq[Framebuffer] | |
| 67 for imageview in swapchain.imageviews: | |
| 68 framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension) | |
| 69 | |
| 70 # 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
|
71 var |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
72 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
|
73 imageAvailable = device.createSemaphore() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
74 renderFinished = device.createSemaphore() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
75 inflight = device.createFence() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
76 |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
77 var vertexshader = device.createVertexShader("#version 450\nvoid main() {}", Vertex()) |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
78 var fragmentshader = device.createFragmentShader("#version 450\nvoid main() {}") |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
79 var pipeline = renderpass.createPipeline(vertexshader, fragmentshader) |
| 557 | 80 |
| 81 echo "All successfull" | |
| 82 echo "Start cleanup" | |
| 554 | 83 |
|
560
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
84 # cleanup |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
85 pipeline.destroy() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
86 vertexshader.destroy() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
87 fragmentshader.destroy() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
88 inflight.destroy() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
89 imageAvailable.destroy() |
|
8de8a2102071
add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
559
diff
changeset
|
90 renderFinished.destroy() |
| 559 | 91 commandPool.destroy() |
| 92 for fb in framebuffers.mitems: | |
| 93 fb.destroy() | |
| 94 renderpass.destroy() | |
| 557 | 95 swapchain.destroy() |
| 96 device.destroy() | |
| 554 | 97 |
| 98 debugger.destroy() | |
| 99 instance.destroy() |
