view 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
line wrap: on
line source

import std/options

import semicongine/vulkan
import semicongine/platform/window
import semicongine/math

type
  Vertex = object
    pos: Vec3
  Uniforms = object
    time: float32


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()

  # 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

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

  # setup render pipeline
  var (swapchain, res) = device.createSwapchain(device.physicalDevice.getSurfaceFormats().filterSurfaceFormat())
  if res != VK_SUCCESS:
    raise newException(Exception, "Unable to create swapchain")
  var renderpass = device.simpleForwardRenderPass(swapchain.format)
  var framebuffers: seq[Framebuffer]
  for imageview in swapchain.imageviews:
    framebuffers.add device.createFramebuffer(renderpass, [imageview], swapchain.dimension)

  # todo: could be create inside "device", but it would be nice to have nim v2 with support for circular dependencies first
  var
    commandPool = device.createCommandPool(family=device.firstGraphicsQueue().get().family, nBuffers=1)
    imageAvailable = device.createSemaphore()
    renderFinished = device.createSemaphore()
    inflight = device.createFence()

  var vertexshader = shader(Vertex, Uniforms, device):
    shadertype: VK_SHADER_STAGE_VERTEX_BIT
    entrypoint: "main"
    version: 450
    code: """"""
  var fragmentshader = shader(Vertex, Uniforms, device):
    shadertype: VK_SHADER_STAGE_FRAGMENT_BIT
    entrypoint: "main"
    version: 450
    code: ""

  #var vertexshader = loadShaderCode[Vertex, Uniforms](device, vertexshadercode)
  #var fragmentshader = loadShaderCode[Vertex, Uniforms](device, fragmentshadercode)
  #var pipeline = renderpass.createPipeline(vertexshaderhandle, fragmentshaderhandle)

  echo "All successfull"
  echo "Start cleanup"

  # cleanup
  #pipeline.destroy()
  #vertexshader.destroy()
  #fragmentshader.destroy()
  inflight.destroy()
  imageAvailable.destroy()
  renderFinished.destroy()
  commandPool.destroy()
  for fb in framebuffers.mitems:
    fb.destroy()
  renderpass.destroy()
  swapchain.destroy()
  device.destroy()

  debugger.destroy()
  instance.destroy()