Mercurial > games > semicongine
comparison semiconginev2/rendering/renderer.nim @ 1228:4e465583ea32
did: rename texture to image
author | sam <sam@basx.dev> |
---|---|
date | Thu, 18 Jul 2024 16:33:24 +0700 |
parents | 56781cc0fc7c |
children | 5dcb503ef0c0 |
comparison
equal
deleted
inserted
replaced
1227:4d97cfc4888b | 1228:4e465583ea32 |
---|---|
57 | 57 |
58 template size(gpuArray: GPUArray): uint64 = | 58 template size(gpuArray: GPUArray): uint64 = |
59 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64 | 59 (gpuArray.data.len * sizeof(elementType(gpuArray.data))).uint64 |
60 template size(gpuValue: GPUValue): uint64 = | 60 template size(gpuValue: GPUValue): uint64 = |
61 sizeof(gpuValue.data).uint64 | 61 sizeof(gpuValue.data).uint64 |
62 func size(texture: Texture): uint64 = | 62 func size(image: Image): uint64 = |
63 texture.data.len.uint64 * sizeof(elementType(texture.data)).uint64 | 63 image.data.len.uint64 * sizeof(elementType(image.data)).uint64 |
64 | 64 |
65 template rawPointer(gpuArray: GPUArray): pointer = | 65 template rawPointer(gpuArray: GPUArray): pointer = |
66 addr(gpuArray.data[0]) | 66 addr(gpuArray.data[0]) |
67 template rawPointer(gpuValue: GPUValue): pointer = | 67 template rawPointer(gpuValue: GPUValue): pointer = |
68 addr(gpuValue.data) | 68 addr(gpuValue.data) |
81 | 81 |
82 # santization checks | 82 # santization checks |
83 for theName, value in descriptorSet.data.fieldPairs: | 83 for theName, value in descriptorSet.data.fieldPairs: |
84 when typeof(value) is GPUValue: | 84 when typeof(value) is GPUValue: |
85 assert value.buffer.vk.Valid | 85 assert value.buffer.vk.Valid |
86 elif typeof(value) is Texture: | 86 elif typeof(value) is Image: |
87 assert value.vk.Valid | 87 assert value.vk.Valid |
88 assert value.imageview.Valid | 88 assert value.imageview.Valid |
89 assert value.sampler.Valid | 89 assert value.sampler.Valid |
90 elif typeof(value) is array: | 90 elif typeof(value) is array: |
91 when elementType(value) is Texture: | 91 when elementType(value) is Image: |
92 for t in value: | 92 for t in value: |
93 assert t.vk.Valid | 93 assert t.vk.Valid |
94 assert t.imageview.Valid | 94 assert t.imageview.Valid |
95 assert t.sampler.Valid | 95 assert t.sampler.Valid |
96 elif elementType(value) is GPUValue: | 96 elif elementType(value) is GPUValue: |
133 descriptorType: descriptorType, | 133 descriptorType: descriptorType, |
134 descriptorCount: descriptorCount, | 134 descriptorCount: descriptorCount, |
135 pImageInfo: nil, | 135 pImageInfo: nil, |
136 pBufferInfo: addr(bufferWrites[^1]), | 136 pBufferInfo: addr(bufferWrites[^1]), |
137 ) | 137 ) |
138 elif typeof(fieldValue) is Texture: | 138 elif typeof(fieldValue) is Image: |
139 imageWrites.add VkDescriptorImageInfo( | 139 imageWrites.add VkDescriptorImageInfo( |
140 sampler: fieldValue.sampler, | 140 sampler: fieldValue.sampler, |
141 imageView: fieldValue.imageView, | 141 imageView: fieldValue.imageView, |
142 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | 142 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
143 ) | 143 ) |
150 descriptorCount: descriptorCount, | 150 descriptorCount: descriptorCount, |
151 pImageInfo: addr(imageWrites[^1]), | 151 pImageInfo: addr(imageWrites[^1]), |
152 pBufferInfo: nil, | 152 pBufferInfo: nil, |
153 ) | 153 ) |
154 elif typeof(fieldValue) is array: | 154 elif typeof(fieldValue) is array: |
155 when elementType(fieldValue) is Texture: | 155 when elementType(fieldValue) is Image: |
156 for texture in fieldValue: | 156 for image in fieldValue: |
157 imageWrites.add VkDescriptorImageInfo( | 157 imageWrites.add VkDescriptorImageInfo( |
158 sampler: texture.sampler, | 158 sampler: image.sampler, |
159 imageView: texture.imageView, | 159 imageView: image.imageView, |
160 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, | 160 imageLayout: VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, |
161 ) | 161 ) |
162 descriptorSetWrites.add VkWriteDescriptorSet( | 162 descriptorSetWrites.add VkWriteDescriptorSet( |
163 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, | 163 sType: VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, |
164 dstSet: descriptorSet.vk[i], | 164 dstSet: descriptorSet.vk[i], |
435 minLod: 0, | 435 minLod: 0, |
436 maxLod: 0, | 436 maxLod: 0, |
437 ) | 437 ) |
438 checkVkResult vkCreateSampler(vulkan.device, addr(samplerInfo), nil, addr(result)) | 438 checkVkResult vkCreateSampler(vulkan.device, addr(samplerInfo), nil, addr(result)) |
439 | 439 |
440 proc createTextureImage(renderData: var RenderData, texture: var Texture) = | 440 proc createVulkanImage(renderData: var RenderData, image: var Image) = |
441 assert texture.vk == VkImage(0), "Texture has already been created" | 441 assert image.vk == VkImage(0), "Image has already been created" |
442 var usage = @[VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_SAMPLED_BIT] | 442 var usage = @[VK_IMAGE_USAGE_TRANSFER_DST_BIT, VK_IMAGE_USAGE_SAMPLED_BIT] |
443 if texture.isRenderTarget: | 443 if image.isRenderTarget: |
444 usage.add VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | 444 usage.add VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
445 let format = GetVkFormat(elementType(texture.data) is TVec1[uint8], usage = usage) | 445 let format = GetVkFormat(elementType(image.data) is TVec1[uint8], usage = usage) |
446 | 446 |
447 texture.vk = svkCreate2DImage(texture.width, texture.height, format, usage) | 447 image.vk = svkCreate2DImage(image.width, image.height, format, usage) |
448 renderData.images.add texture.vk | 448 renderData.images.add image.vk |
449 texture.sampler = createSampler(magFilter = texture.interpolation, minFilter = texture.interpolation) | 449 image.sampler = createSampler(magFilter = image.interpolation, minFilter = image.interpolation) |
450 renderData.samplers.add texture.sampler | 450 renderData.samplers.add image.sampler |
451 | 451 |
452 let memoryRequirements = texture.vk.svkGetImageMemoryRequirements() | 452 let memoryRequirements = image.vk.svkGetImageMemoryRequirements() |
453 let memoryType = BestMemory(mappable = false, filter = memoryRequirements.memoryTypes) | 453 let memoryType = BestMemory(mappable = false, filter = memoryRequirements.memoryTypes) |
454 # check if there is an existing allocated memory block that is large enough to be used | 454 # check if there is an existing allocated memory block that is large enough to be used |
455 var selectedBlockI = -1 | 455 var selectedBlockI = -1 |
456 for i in 0 ..< renderData.memory[memoryType].len: | 456 for i in 0 ..< renderData.memory[memoryType].len: |
457 let memoryBlock = renderData.memory[memoryType][i] | 457 let memoryBlock = renderData.memory[memoryType][i] |
471 memoryRequirements.alignment, | 471 memoryRequirements.alignment, |
472 ) | 472 ) |
473 | 473 |
474 checkVkResult vkBindImageMemory( | 474 checkVkResult vkBindImageMemory( |
475 vulkan.device, | 475 vulkan.device, |
476 texture.vk, | 476 image.vk, |
477 selectedBlock.vk, | 477 selectedBlock.vk, |
478 renderData.memory[memoryType][selectedBlockI].offsetNextFree, | 478 renderData.memory[memoryType][selectedBlockI].offsetNextFree, |
479 ) | 479 ) |
480 renderData.memory[memoryType][selectedBlockI].offsetNextFree += memoryRequirements.size | 480 renderData.memory[memoryType][selectedBlockI].offsetNextFree += memoryRequirements.size |
481 | 481 |
482 # imageview can only be created after memory is bound | 482 # imageview can only be created after memory is bound |
483 texture.imageview = svkCreate2DImageView(texture.vk, format) | 483 image.imageview = svkCreate2DImageView(image.vk, format) |
484 renderData.imageViews.add texture.imageview | 484 renderData.imageViews.add image.imageview |
485 | 485 |
486 # data transfer and layout transition | 486 # data transfer and layout transition |
487 TransitionImageLayout(texture.vk, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) | 487 TransitionImageLayout(image.vk, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) |
488 WithStagingBuffer( | 488 WithStagingBuffer( |
489 (texture.vk, texture.width, texture.height), | 489 (image.vk, image.width, image.height), |
490 memoryRequirements.size, | 490 memoryRequirements.size, |
491 stagingPtr | 491 stagingPtr |
492 ): | 492 ): |
493 copyMem(stagingPtr, texture.data.ToCPointer, texture.size) | 493 copyMem(stagingPtr, image.data.ToCPointer, image.size) |
494 TransitionImageLayout(texture.vk, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) | 494 TransitionImageLayout(image.vk, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) |
495 | 495 |
496 | 496 |
497 proc UploadTextures*(renderdata: var RenderData, descriptorSet: var DescriptorSet) = | 497 proc UploadImages*(renderdata: var RenderData, descriptorSet: var DescriptorSet) = |
498 for name, value in fieldPairs(descriptorSet.data): | 498 for name, value in fieldPairs(descriptorSet.data): |
499 when typeof(value) is Texture: | 499 when typeof(value) is Image: |
500 renderdata.createTextureImage(value) | 500 renderdata.createVulkanImage(value) |
501 elif typeof(value) is array: | 501 elif typeof(value) is array: |
502 when elementType(value) is Texture: | 502 when elementType(value) is Image: |
503 for texture in value.mitems: | 503 for image in value.mitems: |
504 renderdata.createTextureImage(texture) | 504 renderdata.createVulkanImage(image) |
505 | 505 |
506 proc HasGPUValueField[T](name: static string): bool {.compileTime.} = | 506 proc HasGPUValueField[T](name: static string): bool {.compileTime.} = |
507 for fieldname, value in default(T).fieldPairs(): | 507 for fieldname, value in default(T).fieldPairs(): |
508 when typeof(value) is GPUValue and fieldname == name: return true | 508 when typeof(value) is GPUValue and fieldname == name: return true |
509 return false | 509 return false |