# HG changeset patch # User sam # Date 1724326319 -25200 # Node ID df3c075e5dea262418ec10b6cc76c6c71d4fabaa # Parent 1abdd42f5cfedea542b79a224060c01996a08c9c did: formatting diff -r 1abdd42f5cfe -r df3c075e5dea tests/test_audio.nim --- a/tests/test_audio.nim Thu Aug 22 18:31:03 2024 +0700 +++ b/tests/test_audio.nim Thu Aug 22 18:31:59 2024 +0700 @@ -4,12 +4,10 @@ import ../semicongine - proc test1() = mixer[].addSound("test1", sineSoundData(1000, 2, 44100)) mixer[].addSound("test2", sineSoundData(500, 2, 44100)) - let s1 = mixer[].play("test1", loop = true) let s2 = mixer[].play("test2", loop = true) @@ -45,14 +43,8 @@ # song frerejaquesData = concat( - f, g, a, f, - f, g, a, f, - a, bb, c2, c2, - a, bb, c2, c2, - c2Short, d2Short, c2Short, bbShort, a, f, - c2Short, d2Short, c2Short, bbShort, a, f, - f, c, f, f, - f, c, f, f, + f, g, a, f, f, g, a, f, a, bb, c2, c2, a, bb, c2, c2, c2Short, d2Short, c2Short, + bbShort, a, f, c2Short, d2Short, c2Short, bbShort, a, f, f, c, f, f, f, c, f, f, ) mixer[].addSound("frerejaques", frerejaquesData) @@ -67,7 +59,6 @@ mixer[].addTrack("effects") discard mixer[].play("toccata et fugue") - when isMainModule: test1() mixer[].stop() @@ -78,9 +69,10 @@ while mixer[].isPlaying(): # on windows we re-open stdin and this will not work when defined(linux): - discard mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5) + discard + mixer[].play("ping", track = "effects", stopOtherSounds = true, level = 0.5) echo "Press q and enter to exit" if stdin.readLine() == "q": mixer[].stop() elif defined(windows): - sleep(1000) \ No newline at end of file + sleep(1000) diff -r 1abdd42f5cfe -r df3c075e5dea tests/test_gltf.nim --- a/tests/test_gltf.nim Thu Aug 22 18:31:03 2024 +0700 +++ b/tests/test_gltf.nim Thu Aug 22 18:31:59 2024 +0700 @@ -13,10 +13,12 @@ ObjectData = object transform: Mat4 materialId: int32 + Camera = object view: Mat4 normal: Mat4 projection: Mat4 + Material = object color: Vec4f = vec4(1, 1, 1, 1) # colorTexture: int32 = -1 @@ -25,11 +27,12 @@ # metallicRoughnessTexture: int32 = -1 # normalTexture: int32 = -1 # occlusionTexture: int32 = -1 - emissive: Vec4f = vec4(0, 0, 0, 0) - # emissiveTexture: int32 = -1 + emissive: Vec4f = vec4(0, 0, 0, 0) # emissiveTexture: int32 = -1 + MainDescriptors = object materials: array[50, GPUValue[Material, UniformBuffer]] camera: GPUValue[Camera, UniformBufferMapped] + Shader = object objectData {.PushConstant.}: ObjectData position {.VertexAttribute.}: Vec3f @@ -41,7 +44,8 @@ outColor {.ShaderOutput.}: Vec4f descriptors {.DescriptorSet: 0.}: MainDescriptors # code - vertexCode: string = """ + vertexCode: string = + """ void main() { mat4 modelView = objectData.transform * camera.view; mat3 normalMat = mat3(transpose(inverse(objectData.transform))); @@ -51,7 +55,8 @@ fragmentNormal = normal * normalMat; gl_Position = vec4(position, 1) * (modelView * camera.projection); }""" - fragmentCode: string = """ + fragmentCode: string = + """ const vec3 lightPosition = vec3(7, 9, -12); const float shininess = 40; const vec3 ambientColor = vec3(0, 0, 0); @@ -82,6 +87,7 @@ outColor = vec4(color, fragmentColor.a); }""" + Mesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec4f, VertexBuffer] @@ -109,31 +115,34 @@ occlusionTexture: "occlusionTexture", emissiveTexture: "emissiveTexture", emissiveFactor: "emissive", - ) + ), ) var descriptors = asDescriptorSetData( MainDescriptors( - camera: asGPUValue(Camera( - view: Unit4, - normal: Unit4, - projection: Unit4, - ), UniformBufferMapped) + camera: asGPUValue( + Camera(view: Unit4, normal: Unit4, projection: Unit4), UniformBufferMapped + ) ) ) for i in 0 ..< gltfData.materials.len: descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) for mesh in mitems(gltfData.meshes): for primitive in mitems(mesh): - primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer) + primitive[0].color = asGPUArray( + newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer + ) renderdata.assignBuffers(primitive[0]) renderdata.assignBuffers(descriptors) - var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode=[]) + var pipeline = + createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode = []) initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) renderdata.flushAllMemory() - proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4) = + proc drawNode( + commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 + ) = let nodeTransform = gltfData.nodes[nodeId].transform * transform if gltfData.nodes[nodeId].mesh >= 0: for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh].mitems: @@ -141,10 +150,16 @@ commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0], - pushConstant = ObjectData(transform: nodeTransform, materialId: primitive[0].material) + pushConstant = + ObjectData(transform: nodeTransform, materialId: primitive[0].material), ) for childNode in gltfData.nodes[nodeId].children: - drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) + drawNode( + commandbuffer = commandbuffer, + pipeline = pipeline, + nodeId = childNode, + transform = nodeTransform, + ) var camPos: Vec3f var camYaw: float32 @@ -158,15 +173,19 @@ let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32 lastT = getMonoTime() - camYaw += mouseMove().x.float32 / 1000'f32 + camYaw += mouseMove().x.float32 / 1000'f32 camPitch += mouseMove().y.float32 / 1000'f32 var forward = 0'f32 sideward = 0'f32 - if keyIsDown(W): forward += 2 - if keyIsDown(S): forward -= 2 - if keyIsDown(A): sideward -= 2 - if keyIsDown(D): sideward += 2 + if keyIsDown(W): + forward += 2 + if keyIsDown(S): + forward -= 2 + if keyIsDown(A): + sideward -= 2 + if keyIsDown(D): + sideward += 2 let camDir = (rotate(camYaw, Y) * rotate(camPitch, X)) * Z let camDirSide = camDir.cross(-Y).normalized @@ -176,14 +195,20 @@ let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos) descriptors.data.camera.data.view = view descriptors.data.camera.data.normal = view - descriptors.data.camera.data.projection = projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20) + descriptors.data.camera.data.projection = + projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20) updateGPUBuffer(descriptors.data.camera) withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): bindDescriptorSet(commandbuffer, descriptors, 0, pipeline) for nodeId in gltfData.scenes[0]: @@ -191,7 +216,7 @@ commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId, - transform = rotate(PI / 2, Z) + transform = rotate(PI / 2, Z), ) # cleanup @@ -203,7 +228,9 @@ var time = 1000'f32 initVulkan() - var renderpass = createDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT) + var renderpass = createDirectPresentationRenderPass( + depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT + ) setupSwapchain(renderpass = renderpass) lockMouse(true) # showSystemCursor(false) diff -r 1abdd42f5cfe -r df3c075e5dea tests/test_rendering.nim --- a/tests/test_rendering.nim Thu Aug 22 18:31:03 2024 +0700 +++ b/tests/test_rendering.nim Thu Aug 22 18:31:59 2024 +0700 @@ -14,6 +14,7 @@ type PushConstant = object scale: float32 + Shader = object position {.VertexAttribute.}: Vec3f color {.VertexAttribute.}: Vec3f @@ -21,16 +22,22 @@ fragmentColor {.Pass.}: Vec3f outColor {.ShaderOutput.}: Vec4f # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { fragmentColor = color; gl_Position = vec4(position * pushConstant.scale, 1);}""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor, 1);}""" + TriangleMesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] + var mesh = TriangleMesh( - position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer + ), color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) assignBuffers(renderdata, mesh) @@ -40,21 +47,30 @@ var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - - renderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh, pushConstant = PushConstant(scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000))) + renderWithPushConstant( + commandbuffer = commandbuffer, + pipeline = pipeline, + mesh = mesh, + pushConstant = PushConstant( + scale: 0.3 + ((getMonoTime() - start).inMilliseconds().int / 1000) + ), + ) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) destroyPipeline(pipeline) destroyRenderData(renderdata) - proc test_02_triangle_quad_instanced(time: float32) = var renderdata = initRenderData() @@ -67,37 +83,50 @@ fragmentColor {.Pass.}: Vec3f outColor {.ShaderOutput.}: Vec4f # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { fragmentColor = color; gl_Position = vec4((position * scale) + pos, 1);}""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor, 1);}""" + TriangleMesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] + QuadMesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] + Instances = object pos: GPUArray[Vec3f, VertexBuffer] scale: GPUArray[float32, VertexBuffer] + var tri = TriangleMesh( - position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer + ), color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var quad = QuadMesh( - position: asGPUArray([vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.3, -0.3, 0), vec3(-0.3, 0.3, 0), vec3(0.3, 0.3, 0), vec3(0.3, -0.3, 0)], + VertexBuffer, + ), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), - color: asGPUArray([vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer), + color: asGPUArray( + [vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1), vec3(1, 1, 1)], VertexBuffer + ), ) var instancesA: Instances - for n in 1..100: + for n in 1 .. 100: instancesA.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(-0.8'f32 .. 0'f32), 0) instancesA.scale.data.add rand(0.3'f32 .. 0.4'f32) var instancesB: Instances - for n in 1..100: + for n in 1 .. 100: instancesB.pos.data.add vec3(rand(-0.8'f32 .. 0.8'f32), rand(0'f32 .. 0.8'f32), 0) instancesB.scale.data.add rand(0.1'f32 .. 0.2'f32) @@ -111,17 +140,40 @@ var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - - render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesA) - render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad, instances = instancesB) - render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesA) - render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = tri, instances = instancesB) + render( + commandbuffer = commandbuffer, + pipeline = pipeline, + mesh = quad, + instances = instancesA, + ) + render( + commandbuffer = commandbuffer, + pipeline = pipeline, + mesh = quad, + instances = instancesB, + ) + render( + commandbuffer = commandbuffer, + pipeline = pipeline, + mesh = tri, + instances = instancesA, + ) + render( + commandbuffer = commandbuffer, + pipeline = pipeline, + mesh = tri, + instances = instancesB, + ) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) @@ -146,14 +198,17 @@ outColor {.ShaderOutput.}: Vec4f descriptorSets {.DescriptorSet: 0.}: Uniforms # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { fragmentColor = material.baseColor; gl_Position = vec4(position, 1); gl_Position.x += ((material.baseColor.b - 0.5) * 2) - 0.5; uv = position.xy + 0.5; }""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor, 1) * texture(texture1, uv);}""" + QuadMesh = object position: GPUArray[Vec3f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] @@ -164,20 +219,29 @@ let W = BGRA([255'u8, 255'u8, 255'u8, 255'u8]) var quad = QuadMesh( - position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.5, -0.5, 0), vec3(-0.5, 0.5, 0), vec3(0.5, 0.5, 0), vec3(0.5, -0.5, 0)], + VertexBuffer, + ), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) uniforms1 = asDescriptorSetData( Uniforms( material: asGPUValue(Material(baseColor: vec3(1, 1, 1)), UniformBuffer), - texture1: Image[BGRA](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), + texture1: Image[BGRA]( + width: 3, + height: 3, + data: @[R, G, B, G, B, R, B, R, G], + minInterpolation: VK_FILTER_NEAREST, + magInterpolation: VK_FILTER_NEAREST, + ), ) ) uniforms2 = asDescriptorSetData( Uniforms( material: asGPUValue(Material(baseColor: vec3(0.5, 0.5, 0.5)), UniformBuffer), texture1: Image[BGRA](width: 2, height: 2, data: @[R, G, B, W]), - ) + ) ) assignBuffers(renderdata, quad) @@ -194,13 +258,16 @@ var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) @@ -218,20 +285,25 @@ type RenderSettings = object brigthness: float32 + Material = object baseColor: Vec3f + ObjectSettings = object scale: float32 materialIndex: uint32 + Constants = object offset: Vec2f ConstSet = object constants: GPUValue[Constants, UniformBuffer] + MainSet = object renderSettings: GPUValue[RenderSettings, UniformBufferMapped] material: array[2, GPUValue[Material, UniformBuffer]] texture1: array[2, Image[Gray]] + OtherSet = object objectSettings: GPUValue[ObjectSettings, UniformBufferMapped] @@ -244,28 +316,31 @@ descriptorSets1 {.DescriptorSet: 1.}: MainSet descriptorSets2 {.DescriptorSet: 2.}: OtherSet # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { fragmentColor = material[objectSettings.materialIndex].baseColor * renderSettings.brigthness; gl_Position = vec4(position * objectSettings.scale, 1); gl_Position.xy += constants.offset.xy; gl_Position.x += material[objectSettings.materialIndex].baseColor.b - 0.5; uv = position.xy + 0.5; }""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor * texture(texture1[objectSettings.materialIndex], uv).rrr, 1); }""" + QuadMesh = object position: GPUArray[Vec3f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] var quad = QuadMesh( - position: asGPUArray([vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer), + position: asGPUArray( + [vec3(-0.5, -0.5), vec3(-0.5, 0.5), vec3(0.5, 0.5), vec3(0.5, -0.5)], VertexBuffer + ), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) var constset = asDescriptorSetData( - ConstSet( - constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer), - ) + ConstSet(constants: asGPUValue(Constants(offset: vec2(-0.3, 0.2)), UniformBuffer)) ) let G = Gray([50'u8]) let W = Gray([255'u8]) @@ -275,21 +350,35 @@ material: [ asGPUValue(Material(baseColor: vec3(1, 1, 0)), UniformBuffer), asGPUValue(Material(baseColor: vec3(1, 0, 1)), UniformBuffer), - ], - texture1: [ - Image[Gray](width: 2, height: 2, data: @[W, G, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), - Image[Gray](width: 3, height: 3, data: @[W, G, W, G, W, G, W, G, W], minInterpolation: VK_FILTER_NEAREST, magInterpolation: VK_FILTER_NEAREST), - ], - ), + ], + texture1: [ + Image[Gray]( + width: 2, + height: 2, + data: @[W, G, G, W], + minInterpolation: VK_FILTER_NEAREST, + magInterpolation: VK_FILTER_NEAREST, + ), + Image[Gray]( + width: 3, + height: 3, + data: @[W, G, W, G, W, G, W, G, W], + minInterpolation: VK_FILTER_NEAREST, + magInterpolation: VK_FILTER_NEAREST, + ), + ], + ) ) var otherset1 = asDescriptorSetData( OtherSet( - objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped), + objectSettings: + asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 0), UniformBufferMapped) ) ) var otherset2 = asDescriptorSetData( OtherSet( - objectSettings: asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped), + objectSettings: + asGPUValue(ObjectSettings(scale: 1.0, materialIndex: 1), UniformBufferMapped) ) ) @@ -310,23 +399,29 @@ var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): bindDescriptorSet(commandbuffer, constset, 0, pipeline) bindDescriptorSet(commandbuffer, mainset, 1, pipeline) - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - bindDescriptorSet(commandbuffer, otherset1, 2, pipeline) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) bindDescriptorSet(commandbuffer, otherset2, 2, pipeline) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = quad) - mainset.data.renderSettings.data.brigthness = ((getMonoTime() - start).inMilliseconds().int / 1000) / time - otherset1.data.objectSettings.data.scale = 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time + mainset.data.renderSettings.data.brigthness = + ((getMonoTime() - start).inMilliseconds().int / 1000) / time + otherset1.data.objectSettings.data.scale = + 0.5 + ((getMonoTime() - start).inMilliseconds().int / 1000) / time updateGPUBuffer(mainset.data.renderSettings) updateGPUBuffer(otherset1.data.objectSettings) renderdata.flushAllMemory() @@ -338,11 +433,12 @@ proc test_05_cube(time: float32) = type - UniformData = object mvp: Mat4 + Uniforms = object data: GPUValue[UniformData, UniformBufferMapped] + CubeShader = object position {.VertexAttribute.}: Vec3f color {.VertexAttribute.}: Vec4f @@ -350,22 +446,30 @@ outColor {.ShaderOutput.}: Vec4f descriptorSets {.DescriptorSet: 0.}: Uniforms # code - vertexCode = """void main() { + vertexCode = + """void main() { fragmentColor = color; gl_Position = vec4(position, 1) * data.mvp; }""" - fragmentCode = """void main() { + fragmentCode = + """void main() { outColor = fragmentColor; }""" + Mesh = object position: GPUArray[Vec3f, VertexBuffer] normals: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec4f, VertexBuffer] - let quad = @[ - vec3(-0.5, -0.5), vec3(-0.5, +0.5), vec3(+0.5, +0.5), - vec3(+0.5, +0.5), vec3(+0.5, -0.5), vec3(-0.5, -0.5), - ] + let quad = + @[ + vec3(-0.5, -0.5), + vec3(-0.5, +0.5), + vec3(+0.5, +0.5), + vec3(+0.5, +0.5), + vec3(+0.5, -0.5), + vec3(-0.5, -0.5), + ] proc transf(data: seq[Vec3f], mat: Mat4): seq[Vec3f] = for v in data: result.add mat * v @@ -415,16 +519,17 @@ assignBuffers(renderdata, mesh) var floor = Mesh( - position: asGPUArray(quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), VertexBuffer), + position: asGPUArray( + quad.transf(scale(10, 10, 10) * rotate(-PI / 2, X) * translate(0, 0, 0.05)), + VertexBuffer, + ), color: asGPUArray(newSeqWith(6, vec4(0.1, 0.1, 0.1, 1)), VertexBuffer), normals: asGPUArray(newSeqWith(6, Y), VertexBuffer), ) assignBuffers(renderdata, floor) var uniforms1 = asDescriptorSetData( - Uniforms( - data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped) - ) + Uniforms(data: asGPUValue(UniformData(mvp: Unit4), UniformBufferMapped)) ) assignBuffers(renderdata, uniforms1) @@ -441,18 +546,23 @@ let tStartLoop = getMonoTime() - tStart uniforms1.data.data.data.mvp = ( - projection(-PI / 2, getAspectRatio(), 0.01, 100) * - translate(0, 0, 2) * - rotate(PI / 4, X) * - rotate(PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y) + projection(-PI / 2, getAspectRatio(), 0.01, 100) * translate(0, 0, 2) * + rotate(PI / 4, X) * rotate( + PI * 0.1 * (tStartLoop.inMicroseconds() / 1_000_000), Y + ) ) updateGPUBuffer(uniforms1.data.data, flush = true) withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = floor) @@ -478,36 +588,67 @@ fragmentColor {.Pass.}: Vec3f outColor {.ShaderOutput.}: Vec4f # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { gl_PointSize = 100; fragmentColor = color; gl_Position = vec4(position, 1);}""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor, 1);}""" + TriangleMesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] + var triangle = TriangleMesh( - position: asGPUArray([vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.5, -0.5, 0), vec3(0, 0.5, 0), vec3(0.5, -0.5, 0)], VertexBuffer + ), color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var lines = TriangleMesh( - position: asGPUArray([vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], VertexBuffer), - color: asGPUArray([vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer), + position: asGPUArray( + [vec3(-0.9, 0, 0), vec3(-0.05, -0.9, 0), vec3(0.05, -0.9, 0), vec3(0.9, 0, 0)], + VertexBuffer, + ), + color: asGPUArray( + [vec3(1, 1, 0), vec3(1, 1, 0), vec3(0, 1, 0), vec3(0, 1, 0)], VertexBuffer + ), ) assignBuffers(renderdata, triangle) assignBuffers(renderdata, lines) renderdata.flushAllMemory() - var pipeline1 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_LINE, lineWidth = 20'f32) - var pipeline2 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT) - var pipeline3 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth = 5) - var pipeline4 = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST) + var pipeline1 = createPipeline[Shader]( + renderPass = vulkan.swapchain.renderPass, + polygonMode = VK_POLYGON_MODE_LINE, + lineWidth = 20'f32, + ) + var pipeline2 = createPipeline[Shader]( + renderPass = vulkan.swapchain.renderPass, polygonMode = VK_POLYGON_MODE_POINT + ) + var pipeline3 = createPipeline[Shader]( + renderPass = vulkan.swapchain.renderPass, + topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, + lineWidth = 5, + ) + var pipeline4 = createPipeline[Shader]( + renderPass = vulkan.swapchain.renderPass, + topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST, + ) var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: withNextFrame(framebuffer, commandbuffer): - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline1): render(commandbuffer = commandbuffer, pipeline = pipeline1, mesh = triangle) withPipeline(commandbuffer, pipeline2): @@ -531,6 +672,7 @@ type Uniforms = object texture1: Image[BGRA] + Shader = object position {.VertexAttribute.}: Vec3f uv {.VertexAttribute.}: Vec2f @@ -538,49 +680,59 @@ outColor {.ShaderOutput.}: Vec4f descriptorSets {.DescriptorSet: 0.}: Uniforms # code - vertexCode: string = """ + vertexCode: string = + """ void main() { fragmentUv = uv; gl_Position = vec4(position, 1); }""" - fragmentCode: string = """ + fragmentCode: string = + """ void main() { outColor = texture(texture1, fragmentUv); }""" + Quad = object position: GPUArray[Vec3f, VertexBuffer] uv: GPUArray[Vec2f, VertexBuffer] + var mesh = Quad( - position: asGPUArray([ - vec3(-0.8, -0.5), vec3(-0.8, 0.5), vec3(0.8, 0.5), - vec3(0.8, 0.5), vec3(0.8, -0.5), vec3(-0.8, -0.5), - ], VertexBuffer), - uv: asGPUArray([ - vec2(0, 1), vec2(0, 0), vec2(1, 0), - vec2(1, 0), vec2(1, 1), vec2(0, 1), - ], VertexBuffer), + position: asGPUArray( + [ + vec3(-0.8, -0.5), + vec3(-0.8, 0.5), + vec3(0.8, 0.5), + vec3(0.8, 0.5), + vec3(0.8, -0.5), + vec3(-0.8, -0.5), + ], + VertexBuffer, + ), + uv: asGPUArray( + [vec2(0, 1), vec2(0, 0), vec2(1, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1)], + VertexBuffer, + ), ) assignBuffers(renderdata, mesh) renderdata.flushAllMemory() var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass) - var uniforms1 = asDescriptorSetData( - Uniforms( - texture1: loadImage[BGRA]("art.png"), - ) - ) + var uniforms1 = asDescriptorSetData(Uniforms(texture1: loadImage[BGRA]("art.png"))) uploadImages(renderdata, uniforms1) initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], uniforms1) var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): - - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): - bindDescriptorSet(commandbuffer, uniforms1, 0, pipeline) render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh) @@ -589,8 +741,11 @@ destroyPipeline(pipeline) destroyRenderData(renderdata) -proc test_08_triangle_2pass(time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits) = - var (offscreenRP, presentRP) = createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) +proc test_08_triangle_2pass( + time: float32, depthBuffer: bool, samples: VkSampleCountFlagBits +) = + var (offscreenRP, presentRP) = + createIndirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) setupSwapchain(renderpass = presentRP) @@ -599,27 +754,33 @@ type Uniforms = object frameTexture: Image[BGRA] + TriangleShader = object position {.VertexAttribute.}: Vec3f color {.VertexAttribute.}: Vec3f fragmentColor {.Pass.}: Vec3f outColor {.ShaderOutput.}: Vec4f # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { fragmentColor = color; gl_Position = vec4(position, 1);}""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { outColor = vec4(fragmentColor, 1);}""" + PresentShader = object position {.VertexAttribute.}: Vec2f uv {.Pass.}: Vec2f outColor {.ShaderOutput.}: Vec4f descriptorSets {.DescriptorSet: 0.}: Uniforms # code - vertexCode: string = """void main() { + vertexCode: string = + """void main() { uv = ((position + 1) * 0.5) * vec2(1, -1); gl_Position = vec4(position, 0, 1);}""" - fragmentCode: string = """void main() { + fragmentCode: string = + """void main() { vec2 uv1 = uv + vec2(0.001, 0.001); vec2 uv2 = uv + vec2(0.001, -0.001); vec2 uv3 = uv + vec2(-0.001, 0.001); @@ -631,23 +792,32 @@ texture(frameTexture, uv4) ) / 4; }""" + TriangleMesh = object position: GPUArray[Vec3f, VertexBuffer] color: GPUArray[Vec3f, VertexBuffer] + QuadMesh = object position: GPUArray[Vec2f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] + var mesh = TriangleMesh( - position: asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), + position: + asGPUArray([vec3(-0.5, -0.5), vec3(0, 0.5), vec3(0.5, -0.5)], VertexBuffer), color: asGPUArray([vec3(0, 0, 1), vec3(0, 1, 0), vec3(1, 0, 0)], VertexBuffer), ) var quad = QuadMesh( - position: asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer), + position: + asGPUArray([vec2(-1, -1), vec2(-1, 1), vec2(1, 1), vec2(1, -1)], VertexBuffer), indices: asGPUArray([0'u16, 1'u16, 2'u16, 2'u16, 3'u16, 0'u16], IndexBuffer), ) var uniforms1 = asDescriptorSetData( Uniforms( - frameTexture: Image[BGRA](width: vulkan.swapchain.width, height: vulkan.swapchain.height, isRenderTarget: true), + frameTexture: Image[BGRA]( + width: vulkan.swapchain.width, + height: vulkan.swapchain.height, + isRenderTarget: true, + ) ) ) assignBuffers(renderdata, mesh) @@ -676,19 +846,11 @@ ) let requirements = svkGetImageMemoryRequirements(depthImage) depthMemory = svkAllocateMemory( - requirements.size, - bestMemory(mappable = false, filter = requirements.memoryTypes) + requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes) ) - checkVkResult vkBindImageMemory( - vulkan.device, - depthImage, - depthMemory, - 0, - ) + checkVkResult vkBindImageMemory(vulkan.device, depthImage, depthMemory, 0) depthImageView = svkCreate2DImageView( - image = depthImage, - format = DEPTH_FORMAT, - aspect = VK_IMAGE_ASPECT_DEPTH_BIT + image = depthImage, format = DEPTH_FORMAT, aspect = VK_IMAGE_ASPECT_DEPTH_BIT ) # create msaa images (will not use the one in the swapchain @@ -706,15 +868,9 @@ ) let requirements = svkGetImageMemoryRequirements(msaaImage) msaaMemory = svkAllocateMemory( - requirements.size, - bestMemory(mappable = false, filter = requirements.memoryTypes) + requirements.size, bestMemory(mappable = false, filter = requirements.memoryTypes) ) - checkVkResult vkBindImageMemory( - vulkan.device, - msaaImage, - msaaMemory, - 0, - ) + checkVkResult vkBindImageMemory(vulkan.device, msaaImage, msaaMemory, 0) msaaImageView = svkCreate2DImageView(image = msaaImage, format = SURFACE_FORMAT) var attachments: seq[VkImageView] @@ -725,29 +881,37 @@ attachments = @[uniforms1.data.frameTexture.imageview] else: if offscreenRP.depthBuffer: - attachments = @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] + attachments = + @[msaaImageView, depthImageView, uniforms1.data.frameTexture.imageview] else: attachments = @[msaaImageView, uniforms1.data.frameTexture.imageview] var offscreenFB = svkCreateFramebuffer( - offscreenRP.vk, - vulkan.swapchain.width, - vulkan.swapchain.height, - attachments + offscreenRP.vk, vulkan.swapchain.width, vulkan.swapchain.height, attachments ) var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: - withNextFrame(framebuffer, commandbuffer): - - withRenderPass(offscreenRP, offscreenFB, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + offscreenRP, + offscreenFB, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, drawPipeline): render(commandbuffer = commandbuffer, pipeline = drawPipeline, mesh = mesh) - withRenderPass(presentRP, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): - + withRenderPass( + presentRP, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, presentPipeline): - bindDescriptorSet(commandbuffer, uniforms1, 0, presentPipeline) render(commandbuffer = commandbuffer, pipeline = presentPipeline, mesh = quad) @@ -775,16 +939,16 @@ var mainRenderpass: RenderPass var renderPasses = [ - (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), - (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), - (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), - (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), + (depthBuffer: false, samples: VK_SAMPLE_COUNT_1_BIT), + (depthBuffer: false, samples: VK_SAMPLE_COUNT_4_BIT), + (depthBuffer: true, samples: VK_SAMPLE_COUNT_1_BIT), + (depthBuffer: true, samples: VK_SAMPLE_COUNT_4_BIT), ] - # test normal for i, (depthBuffer, samples) in renderPasses: - var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) + var renderpass = + createDirectPresentationRenderPass(depthBuffer = depthBuffer, samples = samples) setupSwapchain(renderpass = renderpass) # tests a simple triangle with minimalistic shader and vertex format diff -r 1abdd42f5cfe -r df3c075e5dea tests/test_storage.nim --- a/tests/test_storage.nim Thu Aug 22 18:31:03 2024 +0700 +++ b/tests/test_storage.nim Thu Aug 22 18:31:59 2024 +0700 @@ -34,6 +34,5 @@ SystemStorage.purge() UserStorage.purge() - when isMainModule: main() diff -r 1abdd42f5cfe -r df3c075e5dea tests/test_text.nim --- a/tests/test_text.nim Thu Aug 22 18:31:03 2024 +0700 +++ b/tests/test_text.nim Thu Aug 22 18:31:59 2024 +0700 @@ -9,37 +9,35 @@ import ../semicongine -type - FontDS = object - fontAtlas: Image[Gray] +type FontDS = object + fontAtlas: Image[Gray] proc test_01_static_label(time: float32) = var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) var renderdata = initRenderData() - var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) + var pipeline = + createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) uploadImages(renderdata, ds) - initDescriptorSet( - renderdata, - pipeline.layout(0), - ds, - ) + initDescriptorSet(renderdata, pipeline.layout(0), ds) - var label1 = initTextbox( - renderdata, - pipeline.layout(0), - font, - 0.0005, - "Hello semicongine!", - ) + var label1 = + initTextbox(renderdata, pipeline.layout(0), font, 0.0005, "Hello semicongine!") var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: label1.refresh() withNextFrame(framebuffer, commandbuffer): bindDescriptorSet(commandbuffer, ds, 0, pipeline) - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): render(commandbuffer, pipeline, label1, vec3(), vec4(1, 1, 1, 1)) @@ -54,7 +52,8 @@ var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) var renderdata = initRenderData() - var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) + var pipeline = + createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) var ds1 = asDescriptorSetData(FontDS(fontAtlas: font1.fontAtlas)) uploadImages(renderdata, ds1) @@ -69,27 +68,9 @@ initDescriptorSet(renderdata, pipeline.layout(0), ds3) var labels = [ - initTextbox( - renderdata, - pipeline.layout(0), - font1, - 0.004, - " 0", - ), - initTextbox( - renderdata, - pipeline.layout(0), - font2, - 0.001, - " 1", - ), - initTextbox( - renderdata, - pipeline.layout(0), - font3, - 0.001, - " 2", - ) + initTextbox(renderdata, pipeline.layout(0), font1, 0.004, " 0"), + initTextbox(renderdata, pipeline.layout(0), font2, 0.001, " 1"), + initTextbox(renderdata, pipeline.layout(0), font3, 0.001, " 2"), ] var start = getMonoTime() @@ -101,31 +82,38 @@ labels[i].refresh() inc p withNextFrame(framebuffer, commandbuffer): - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): bindDescriptorSet(commandbuffer, ds1, 0, pipeline) render( commandbuffer, pipeline, labels[0], - position=vec3(0 / labels.len, 0.1 + progress * 0.5), - color=vec4(1, 1, 1, 1), + position = vec3(0 / labels.len, 0.1 + progress * 0.5), + color = vec4(1, 1, 1, 1), ) bindDescriptorSet(commandbuffer, ds2, 0, pipeline) render( commandbuffer, pipeline, labels[1], - position=vec3(1 / labels.len, 0.1 + progress * 0.5), - color=vec4(1, 1, 1, 1), + position = vec3(1 / labels.len, 0.1 + progress * 0.5), + color = vec4(1, 1, 1, 1), ) bindDescriptorSet(commandbuffer, ds3, 0, pipeline) render( commandbuffer, pipeline, labels[2], - position=vec3(2 / labels.len, 0.1 + progress * 0.5), - color=vec4(1, 1, 1, 1), + position = vec3(2 / labels.len, 0.1 + progress * 0.5), + color = vec4(1, 1, 1, 1), ) # cleanup @@ -137,15 +125,12 @@ var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) var renderdata = initRenderData() - var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) + var pipeline = + createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) uploadImages(renderdata, ds) - initDescriptorSet( - renderdata, - pipeline.layout(0), - ds, - ) + initDescriptorSet(renderdata, pipeline.layout(0), ds) var labels: seq[Textbox] @@ -186,10 +171,23 @@ let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time withNextFrame(framebuffer, commandbuffer): bindDescriptorSet(commandbuffer, ds, 0, pipeline) - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): for i in 0 ..< labels.len: - render(commandbuffer, pipeline, labels[i], vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), vec4(1, 1, 1, 1)) + render( + commandbuffer, + pipeline, + labels[i], + vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), + vec4(1, 1, 1, 1), + ) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) @@ -200,15 +198,12 @@ var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) var renderdata = initRenderData() - var pipeline = createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) + var pipeline = + createPipeline[DefaultFontShader[FontDS]](renderPass = vulkan.swapchain.renderPass) var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas)) uploadImages(renderdata, ds) - initDescriptorSet( - renderdata, - pipeline.layout(0), - ds, - ) + initDescriptorSet(renderdata, pipeline.layout(0), ds) var labels: seq[Textbox] var positions = newSeq[Vec3f](100) @@ -216,15 +211,10 @@ var scales = newSeq[Vec2f](100) for i in 0 ..< 100: positions[i] = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1)) - colors[i] = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)) + colors[i] = + vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)) scales[i] = vec2(rand(0.5'f32 .. 1.5'f32), rand(0.5'f32 .. 1.5'f32)) - labels.add initTextbox( - renderdata, - pipeline.layout(0), - font, - 0.001, - $i, - ) + labels.add initTextbox(renderdata, pipeline.layout(0), font, 0.001, $i) var start = getMonoTime() while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: @@ -232,10 +222,19 @@ l.refresh() withNextFrame(framebuffer, commandbuffer): bindDescriptorSet(commandbuffer, ds, 0, pipeline) - withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): + withRenderPass( + vulkan.swapchain.renderPass, + framebuffer, + commandbuffer, + vulkan.swapchain.width, + vulkan.swapchain.height, + vec4(0, 0, 0, 0), + ): withPipeline(commandbuffer, pipeline): for i in 0 ..< labels.len: - render(commandbuffer, pipeline, labels[i], positions[i], colors[i], scales[i]) + render( + commandbuffer, pipeline, labels[i], positions[i], colors[i], scales[i] + ) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device)