# HG changeset patch # User sam # Date 1721192582 -25200 # Node ID 518a952eccbf955247b06c397ed4d8c8763b2197 # Parent d9799f74f5a7b986eb536fcf84a9d97ab17bff8f did: increase texture format compatability diff -r d9799f74f5a7 -r 518a952eccbf semicongine/rendering.nim --- a/semicongine/rendering.nim Wed Jul 17 00:33:32 2024 +0700 +++ b/semicongine/rendering.nim Wed Jul 17 12:03:02 2024 +0700 @@ -72,7 +72,7 @@ type # type aliases SupportedGPUType = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] - TextureType = TVec1[uint8] | TVec2[uint8] | TVec3[uint8] | TVec4[uint8] + TextureType = TVec1[uint8] | TVec4[uint8] # shader related types DescriptorSet*[T: object] = object diff -r d9799f74f5a7 -r 518a952eccbf semicongine/rendering/renderer.nim --- a/semicongine/rendering/renderer.nim Wed Jul 17 00:33:32 2024 +0700 +++ b/semicongine/rendering/renderer.nim Wed Jul 17 12:03:02 2024 +0700 @@ -1,6 +1,3 @@ -func depth(texture: Texture): int = - default(elementType(texture.data)).len - func pointerAddOffset[T: SomeInteger](p: pointer, offset: T): pointer = cast[pointer](cast[T](p) + offset) @@ -13,17 +10,12 @@ of UniformBuffer: @[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] of UniformBufferMapped: @[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_TRANSFER_DST_BIT] -proc GetVkFormat(depth: int, usage: openArray[VkImageUsageFlagBits]): VkFormat = - const DEPTH_FORMAT_MAP = [ - 0: [VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED], - 1: [VK_FORMAT_R8_SRGB, VK_FORMAT_R8_UNORM], - 2: [VK_FORMAT_R8G8_SRGB, VK_FORMAT_R8G8_UNORM], - 3: [VK_FORMAT_R8G8B8_SRGB, VK_FORMAT_R8G8B8_UNORM], - 4: [VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_R8G8B8A8_UNORM], - ] +proc GetVkFormat(grayscale: bool, usage: openArray[VkImageUsageFlagBits]): VkFormat = + let formats = if grayscale: [VK_FORMAT_R8_SRGB, VK_FORMAT_R8_UNORM] + else: [VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_R8G8B8A8_UNORM] var formatProperties = VkImageFormatProperties2(sType: VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2) - for format in DEPTH_FORMAT_MAP[depth]: + for format in formats: var formatInfo = VkPhysicalDeviceImageFormatInfo2( sType: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, format: format, @@ -446,9 +438,9 @@ checkVkResult vkCreateSampler(vulkan.device, addr(samplerInfo), nil, addr(result)) proc createTextureImage(renderData: var RenderData, texture: var Texture) = - assert texture.vk == VkImage(0) + assert texture.vk == VkImage(0), "Texture has already been created" const usage = [VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_SAMPLED_BIT] - let format = GetVkFormat(texture.depth, usage = usage) + let format = GetVkFormat(elementType(texture.data) is TVec1[uint8], usage = usage) texture.vk = svkCreate2DImage(texture.width, texture.height, format, usage) renderData.images.add texture.vk diff -r d9799f74f5a7 -r 518a952eccbf tests/test_rendering Binary file tests/test_rendering has changed diff -r d9799f74f5a7 -r 518a952eccbf tests/test_rendering.nim --- a/tests/test_rendering.nim Wed Jul 17 00:33:32 2024 +0700 +++ b/tests/test_rendering.nim Wed Jul 17 12:03:02 2024 +0700 @@ -128,7 +128,7 @@ Uniforms = object material: GPUValue[Material, UniformBuffer] - texture1: Texture[TVec3[uint8]] + texture1: Texture[TVec4[uint8]] QuadShader = object position {.VertexAttribute.}: Vec3f @@ -149,10 +149,10 @@ position: GPUArray[Vec3f, VertexBuffer] indices: GPUArray[uint16, IndexBuffer] - let R = TVec3[uint8]([255'u8, 0'u8, 0'u8]) - let G = TVec3[uint8]([0'u8, 255'u8, 0'u8]) - let B = TVec3[uint8]([0'u8, 0'u8, 255'u8]) - let W = TVec3[uint8]([255'u8, 255'u8, 255'u8]) + let R = TVec4[uint8]([255'u8, 0'u8, 0'u8, 255'u8]) + let G = TVec4[uint8]([0'u8, 255'u8, 0'u8, 255'u8]) + let B = TVec4[uint8]([0'u8, 0'u8, 255'u8, 255'u8]) + let W = TVec4[uint8]([255'u8, 255'u8, 255'u8, 255'u8]) var quad = QuadMesh( position: asGPUArray([NewVec3f(-0.5, -0.5), NewVec3f(-0.5, 0.5), NewVec3f(0.5, 0.5), NewVec3f(0.5, -0.5)], VertexBuffer), @@ -161,14 +161,14 @@ uniforms1 = asDescriptorSet( Uniforms( material: asGPUValue(Material(baseColor: NewVec3f(1, 1, 1)), UniformBuffer), - texture1: Texture[TVec3[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST), + texture1: Texture[TVec4[uint8]](width: 3, height: 3, data: @[R, G, B, G, B, R, B, R, G], interpolation: VK_FILTER_NEAREST), ) ) uniforms2 = asDescriptorSet( Uniforms( material: asGPUValue(Material(baseColor: NewVec3f(0.5, 0.5, 0.5)), UniformBuffer), - texture1: Texture[TVec3[uint8]](width: 2, height: 2, data: @[R, G, B, W]), - ) + texture1: Texture[TVec4[uint8]](width: 2, height: 2, data: @[R, G, B, W]), + ) ) AssignBuffers(renderdata, quad) @@ -344,7 +344,7 @@ mainRenderpass = CreatePresentationRenderPass(samples = VK_SAMPLE_COUNT_4_BIT) swapchain = InitSwapchain(renderpass = mainRenderpass, samples = VK_SAMPLE_COUNT_4_BIT).get() - test_01_triangle(99999999) + # test_01_triangle(99999999) checkVkResult vkDeviceWaitIdle(vulkan.device) vkDestroyRenderPass(vulkan.device, mainRenderpass, nil)