view tests/test_vulkan_wrapper.nim @ 112:0c5a74885796

did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
author Sam <sam@basx.dev>
date Sat, 01 Apr 2023 00:40:02 +0700
parents 6fd10b7e2d6a
children 7b695fb335ed
line wrap: on
line source

import std/options

import semicongine/vulkan
import semicongine/platform/window
import semicongine/math
import semicongine/entity
import semicongine/scene
import semicongine/gpu_data
import semicongine/mesh

proc diagnostics(instance: Instance) =
  # diagnostic output
  echo "Devices"
  for device in instance.getPhysicalDevices():
    echo "  " & $device
    echo "  Rating: " & $device.rateGraphics()
    echo "  Extensions"
    for extension in device.getExtensions():
      echo "    " & $extension
    echo "  Properties"
    echo "  " & $device.getProperties()
    echo "  Features"
    echo "  " & $device.getFeatures()
    echo "  Queue families"
    for queueFamily in device.getQueueFamilies():
      echo "    " & $queueFamily
    echo "  Surface present modes"
    for mode in device.getSurfacePresentModes():
      echo "    " & $mode
    echo "  Surface formats"
    for format in device.getSurfaceFormats():
      echo "    " & $format

when isMainModule:
  # print basic driver infos
  echo "Layers"
  for layer in getLayers():
    echo "  " & layer
  echo "Instance extensions"
  for extension in getInstanceExtensions():
    echo "  " & extension

  # create instance
  var thewindow = createWindow("Test")
  var instance = thewindow.createInstance(
    vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
    instanceExtensions= @["VK_EXT_debug_utils"],
    layers= @["VK_LAYER_KHRONOS_validation"]
  )
  var debugger = instance.createDebugMessenger()

  # create devices
  let selectedPhysicalDevice = instance.getPhysicalDevices().filterBestGraphics()
  var device = instance.createDevice(
    selectedPhysicalDevice,
    @[],
    @[],
    selectedPhysicalDevice.filterForGraphicsPresentationQueues()
  )

  const inputs = AttributeGroup(attributes: @[attr(name="pos", thetype=Float32, components=3)])
  const uniforms = AttributeGroup()
  const outputs = AttributeGroup(attributes: @[attr(name="fragpos", thetype=Float32, components=3)])
  const fragOutput = AttributeGroup(attributes: @[attr(name="color", thetype=Float32, components=4)])
  const vertexBinary = shaderCode(inputs=inputs, uniforms=uniforms, outputs=outputs, stage=VK_SHADER_STAGE_VERTEX_BIT, version=450, entrypoint="main", "fragpos = pos;")
  const fragmentBinary = shaderCode(inputs=outputs, uniforms=uniforms, outputs=fragOutput, stage=VK_SHADER_STAGE_FRAGMENT_BIT, version=450, entrypoint="main", "color = vec4(1, 1, 1, 0);")
  var
    vertexshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_VERTEX_BIT, "main", vertexBinary)
    fragmentshader = device.createShader(inputs, uniforms, outputs, VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentBinary)
    surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat()
    renderPass = device.simpleForwardRenderPass(surfaceFormat.format, vertexshader, fragmentshader, 2)
  var (swapchain, res) = device.createSwapchain(renderPass, surfaceFormat, device.firstGraphicsQueue().get().family, 2)
  if res != VK_SUCCESS:
    raise newException(Exception, "Unable to create swapchain")

  var thescene = Scene(name: "main", root: newEntity("triangle", newMesh([newVec3(-1, -1), newVec3(0, 1), newVec3(1, -1)])))
  thescene.setupDrawables(renderPass)

  echo "Setup successfull, start rendering"
  for i in 0 ..< 1:
    discard swapchain.drawScene(thescene)
  echo "Rendered ", swapchain.framesRendered, " frames"
  echo "Start cleanup"


  # cleanup
  checkVkResult device.vk.vkDeviceWaitIdle()
  vertexshader.destroy()
  fragmentshader.destroy()
  renderPass.destroy()
  swapchain.destroy()
  device.destroy()

  debugger.destroy()
  instance.destroy()