Gpu cleanup

This commit is contained in:
luboslenco
2025-07-10 16:35:19 +02:00
parent 9515dc81ad
commit ebdd602741
19 changed files with 90 additions and 354 deletions
Binary file not shown.
+9 -54
View File
@@ -13,8 +13,6 @@
#include <iron_math.h>
#include <backends/windows_system.h>
void iron_memory_emergency();
bool gpu_transpose_mat = false;
static ID3D12Device *device = NULL;
static ID3D12CommandQueue *queue;
@@ -177,7 +175,6 @@ void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height
render_target->width = width;
render_target->height = height;
render_target->impl.readback = NULL;
render_target->data = NULL;
render_target->state = (framebuffer_index >= 0) ? GPU_TEXTURE_STATE_PRESENT : GPU_TEXTURE_STATE_SHADER_RESOURCE;
DXGI_FORMAT dxgi_format = convert_format(format);
@@ -223,18 +220,8 @@ void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height
window_swapchain->lpVtbl->GetBuffer(window_swapchain, framebuffer_index, &IID_ID3D12Resource, &render_target->impl.image);
}
else {
HRESULT result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
&clear_value, &IID_ID3D12Resource, &render_target->impl.image);
if (result != S_OK) {
for (int i = 0; i < 10; ++i) {
iron_memory_emergency();
result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
&clear_value, &IID_ID3D12Resource, &render_target->impl.image);
if (result == S_OK) {
break;
}
}
}
device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
&clear_value, &IID_ID3D12Resource, &render_target->impl.image);
}
D3D12_RENDER_TARGET_VIEW_DESC view_desc = {
@@ -816,7 +803,6 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
texture->width = width;
texture->height = height;
texture->format = format;
texture->data = data;
texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE;
DXGI_FORMAT d3d_format = convert_format(format);
int _format_size = format_size(d3d_format);
@@ -843,20 +829,9 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
.Flags = D3D12_RESOURCE_FLAG_NONE,
};
HRESULT result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc,
device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, NULL, &IID_ID3D12Resource, &texture->impl.image);
if (result != S_OK) {
for (int i = 0; i < 10; ++i) {
iron_memory_emergency();
result = device->lpVtbl->CreateCommittedResource(device, &heap_properties, D3D12_HEAP_FLAG_NONE, &resource_desc,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, NULL, &IID_ID3D12Resource, &texture->impl.image);
if (result == S_OK) {
break;
}
}
}
D3D12_HEAP_PROPERTIES heap_properties_upload = {
.Type = D3D12_HEAP_TYPE_UPLOAD,
.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
@@ -866,7 +841,7 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
};
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint;
UINT64 upload_buffer_size = 0;
UINT64 upload_buffer_size;
device->lpVtbl->GetCopyableFootprints(device, &resource_desc, 0, 1, 0, &footprint, NULL, NULL, &upload_buffer_size);
D3D12_RESOURCE_DESC resource_desc_upload = {
@@ -884,27 +859,15 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
};
struct ID3D12Resource *upload_image;
result = device->lpVtbl->CreateCommittedResource(device, &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &resource_desc_upload,
device->lpVtbl->CreateCommittedResource(device, &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &resource_desc_upload,
D3D12_RESOURCE_STATE_GENERIC_READ, NULL, &IID_ID3D12Resource, &upload_image);
if (result != S_OK) {
for (int i = 0; i < 10; ++i) {
iron_memory_emergency();
result = device->lpVtbl->CreateCommittedResource(device, &heap_properties_upload, D3D12_HEAP_FLAG_NONE, &resource_desc_upload,
D3D12_RESOURCE_STATE_GENERIC_READ, NULL, &IID_ID3D12Resource, &upload_image);
if (result == S_OK) {
break;
}
}
}
texture->impl.stride = (int)ceilf(upload_buffer_size / (float)(height * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)) * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
int stride = (int)ceilf(upload_buffer_size / (float)(height * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT)) * D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
BYTE *pixel;
upload_image->lpVtbl->Map(upload_image, 0, NULL, (void **)&pixel);
for (int y = 0; y < texture->height; ++y) {
memcpy(&pixel[y * texture->impl.stride], &((uint8_t *)data)[y * texture->width * _format_size], texture->width * _format_size);
memcpy(&pixel[y * stride], &((uint8_t *)data)[y * texture->width * _format_size], texture->width * _format_size);
}
upload_image->lpVtbl->Unmap(upload_image, 0, NULL);
@@ -953,14 +916,8 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
command_list->lpVtbl->CopyTextureRegion(command_list, &destination, 0, 0, 0, &source, NULL);
barrier = (D3D12_RESOURCE_BARRIER){
.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE,
.Transition.pResource = texture->impl.image,
.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST,
.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
};
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST,
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
command_list->lpVtbl->ResourceBarrier(command_list, 1, &barrier);
// TODO:
@@ -983,8 +940,6 @@ void gpu_texture_destroy(gpu_texture_t *render_target) {
}
}
void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {}
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {
gpu_render_target_init2(target, width, height, format, -1);
}
-1
View File
@@ -55,7 +55,6 @@ typedef struct {
struct ID3D12Resource *readback;
struct ID3D12DescriptorHeap *srv_descriptor_heap;
struct ID3D12DescriptorHeap *rtv_descriptor_heap;
int stride;
} gpu_texture_impl_t;
typedef struct {
-1
View File
@@ -22,7 +22,6 @@ typedef struct {
typedef struct {
void *_tex;
void *data;
bool has_mipmaps;
void *_texReadback;
} gpu_texture_impl_t;
+13 -93
View File
@@ -147,7 +147,6 @@ void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_t
memset(target, 0, sizeof(gpu_texture_t));
target->width = width;
target->height = height;
target->data = NULL;
target->state = GPU_TEXTURE_STATE_RENDER_TARGET;
target->impl._texReadback = NULL;
@@ -616,9 +615,11 @@ void gpu_shader_init(gpu_shader_t *shader, const void *data, size_t length, gpu_
shader->impl.length = length;
}
static void create_texture(gpu_texture_t *texture, int width, int height, int format, bool writable) {
texture->impl.has_mipmaps = false;
id<MTLDevice> device = getMetalDevice();
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) {
texture->width = width;
texture->height = height;
texture->format = format;
texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE;
MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((gpu_texture_format_t)format)
width:width
@@ -631,43 +632,17 @@ static void create_texture(gpu_texture_t *texture, int width, int height, int fo
descriptor.pixelFormat = convert_image_format((gpu_texture_format_t)format);
descriptor.arrayLength = 1;
descriptor.mipmapLevelCount = 1;
// TODO: Make less textures writable
if (writable) {
descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
}
descriptor.usage = MTLTextureUsageShaderRead; // MTLTextureUsageShaderWrite
id<MTLDevice> device = getMetalDevice();
texture->impl._tex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor];
}
int gpu_texture_stride(gpu_texture_t *texture) {
switch (texture->format) {
case GPU_TEXTURE_FORMAT_R8:
return texture->width;
case GPU_TEXTURE_FORMAT_RGBA32:
default:
return texture->width * 4;
case GPU_TEXTURE_FORMAT_RGBA64:
return texture->width * 8;
case GPU_TEXTURE_FORMAT_RGBA128:
return texture->width * 16;
}
}
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) {
texture->width = width;
texture->height = height;
texture->format = format;
texture->data = data;
texture->impl.data = NULL;
texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE;
create_texture(texture, width, height, format, true);
id<MTLTexture> tex = (__bridge id<MTLTexture>)texture->impl._tex;
[tex replaceRegion:MTLRegionMake2D(0, 0, texture->width, texture->height)
mipmapLevel:0
slice:0
withBytes:data
bytesPerRow:gpu_texture_stride(texture)
bytesPerImage:gpu_texture_stride(texture) * texture->height];
[texture->impl._tex replaceRegion:MTLRegionMake2D(0, 0, width, height)
mipmapLevel:0
slice:0
withBytes:data
bytesPerRow:width * format_byte_size(texture)
bytesPerImage:width * format_byte_size(texture) * height];
}
void gpu_texture_destroy(gpu_texture_t *target) {
@@ -678,61 +653,6 @@ void gpu_texture_destroy(gpu_texture_t *target) {
id<MTLTexture> texReadback = (__bridge_transfer id<MTLTexture>)target->impl._texReadback;
texReadback = nil;
target->impl._texReadback = NULL;
if (target->impl.data != NULL) {
free(target->impl.data);
target->impl.data = NULL;
}
}
void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {
if (!texture->impl.has_mipmaps) {
id<MTLDevice> device = getMetalDevice();
MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((gpu_texture_format_t)texture->format)
width:texture->width
height:texture->height
mipmapped:YES];
descriptor.textureType = MTLTextureType2D;
descriptor.width = texture->width;
descriptor.height = texture->height;
descriptor.depth = 1;
descriptor.pixelFormat = convert_image_format((gpu_texture_format_t)texture->format);
descriptor.arrayLength = 1;
bool writable = true;
if (writable) {
descriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead;
}
void *mipmaptex = (__bridge_retained void *)[device newTextureWithDescriptor:descriptor];
id<MTLCommandQueue> commandQueue = getMetalQueue();
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
id<MTLBlitCommandEncoder> commandEncoder = [commandBuffer blitCommandEncoder];
[commandEncoder copyFromTexture:(__bridge id<MTLTexture>)texture->impl._tex
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(texture->width, texture->height, 1)
toTexture:(__bridge id<MTLTexture>)mipmaptex
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(0, 0, 0)];
[commandEncoder endEncoding];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
id<MTLTexture> tex = (__bridge_transfer id<MTLTexture>)texture->impl._tex;
tex = nil;
texture->impl._tex = mipmaptex;
texture->impl.has_mipmaps = true;
}
id<MTLTexture> tex = (__bridge id<MTLTexture>)texture->impl._tex;
[tex replaceRegion:MTLRegionMake2D(0, 0, mipmap->width, mipmap->height)
mipmapLevel:level
withBytes:mipmap->data
bytesPerRow:mipmap->width * format_byte_size(mipmap->format)];
}
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {
+51 -134
View File
@@ -417,9 +417,7 @@ VkSwapchainKHR cleanup_swapchain() {
void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int framebuffer_index) {
target->width = width;
target->height = height;
target->data = NULL;
target->impl.format = convert_image_format(format);
target->impl.stage = 0;
target->impl.readback_buffer_created = false;
target->state = (framebuffer_index >= 0) ? GPU_TEXTURE_STATE_PRESENT : GPU_TEXTURE_STATE_SHADER_RESOURCE;
@@ -1453,7 +1451,6 @@ void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size) {
}
void gpu_set_texture(int unit, gpu_texture_t *texture) {
texture->impl.stage = unit;
current_textures[unit] = texture;
}
@@ -1674,8 +1671,8 @@ void gpu_shader_destroy(gpu_shader_t *shader) {
shader->impl.source = NULL;
}
static void prepare_texture_image(uint8_t *tex_colors, uint32_t width, uint32_t height, gpu_texture_impl_t *tex_obj, VkImageTiling tiling,
VkImageUsageFlags usage, VkFlags required_props, VkDeviceSize *deviceSize, VkFormat tex_format) {
static void prepare_texture_image(uint8_t *pixels, uint32_t width, uint32_t height, gpu_texture_impl_t *impl, VkImageTiling tiling,
VkImageUsageFlags usage, VkFlags required_props, VkFormat tex_format) {
VkImageCreateInfo image_create_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
@@ -1701,15 +1698,15 @@ static void prepare_texture_image(uint8_t *tex_colors, uint32_t width, uint32_t
.memoryTypeIndex = 0,
};
vkCreateImage(device, &image_create_info, NULL, &tex_obj->image);
vkCreateImage(device, &image_create_info, NULL, &impl->image);
VkMemoryRequirements mem_reqs;
vkGetImageMemoryRequirements(device, tex_obj->image, &mem_reqs);
*deviceSize = mem_alloc.allocationSize = mem_reqs.size;
vkGetImageMemoryRequirements(device, impl->image, &mem_reqs);
mem_alloc.allocationSize = mem_reqs.size;
memory_type_from_properties(mem_reqs.memoryTypeBits, required_props, &mem_alloc.memoryTypeIndex);
vkAllocateMemory(device, &mem_alloc, NULL, &tex_obj->mem);
vkBindImageMemory(device, tex_obj->image, tex_obj->mem, 0);
vkAllocateMemory(device, &mem_alloc, NULL, &impl->mem);
vkBindImageMemory(device, impl->image, impl->mem, 0);
if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT && tex_colors != NULL) {
if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
VkImageSubresource subres = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
@@ -1718,49 +1715,37 @@ static void prepare_texture_image(uint8_t *tex_colors, uint32_t width, uint32_t
VkSubresourceLayout layout;
uint8_t *data;
vkGetImageSubresourceLayout(device, tex_obj->image, &subres, &layout);
vkMapMemory(device, tex_obj->mem, 0, mem_alloc.allocationSize, 0, (void **)&data);
vkGetImageSubresourceLayout(device, impl->image, &subres, &layout);
vkMapMemory(device, impl->mem, 0, mem_alloc.allocationSize, 0, (void **)&data);
if (tex_format == VK_FORMAT_R8_UNORM) {
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data[y * layout.rowPitch + x] = tex_colors[y * width + x];
}
}
}
else if (tex_format == VK_FORMAT_R32_SFLOAT) {
uint32_t *data32 = (uint32_t *)data;
uint32_t *tex_colors32 = (uint32_t *)tex_colors;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data32[y * (layout.rowPitch / 4) + x] = tex_colors32[y * width + x];
data[y * layout.rowPitch + x] = pixels[y * width + x];
}
}
}
else if (tex_format == VK_FORMAT_R16_SFLOAT) {
uint16_t *data16 = (uint16_t *)data;
uint16_t *tex_colors16 = (uint16_t *)tex_colors;
uint16_t *tex_colors16 = (uint16_t *)pixels;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data16[y * (layout.rowPitch / 4) + x] = tex_colors16[y * width + x];
}
}
}
else if (tex_format == VK_FORMAT_R32G32B32A32_SFLOAT) {
else if (tex_format == VK_FORMAT_R32_SFLOAT) {
uint32_t *data32 = (uint32_t *)data;
uint32_t *tex_colors32 = (uint32_t *)tex_colors;
uint32_t *tex_colors32 = (uint32_t *)pixels;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data32[y * (layout.rowPitch / 4) + x * 4 + 0] = tex_colors32[y * width * 4 + x * 4 + 0];
data32[y * (layout.rowPitch / 4) + x * 4 + 1] = tex_colors32[y * width * 4 + x * 4 + 1];
data32[y * (layout.rowPitch / 4) + x * 4 + 2] = tex_colors32[y * width * 4 + x * 4 + 2];
data32[y * (layout.rowPitch / 4) + x * 4 + 3] = tex_colors32[y * width * 4 + x * 4 + 3];
data32[y * (layout.rowPitch / 4) + x] = tex_colors32[y * width + x];
}
}
}
else if (tex_format == VK_FORMAT_R16G16B16A16_SFLOAT) {
uint16_t *data16 = (uint16_t *)data;
uint16_t *tex_colors16 = (uint16_t *)tex_colors;
uint16_t *tex_colors16 = (uint16_t *)pixels;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data16[y * (layout.rowPitch / 4) + x * 4 + 0] = tex_colors16[y * width * 4 + x * 4 + 0];
@@ -1770,80 +1755,69 @@ static void prepare_texture_image(uint8_t *tex_colors, uint32_t width, uint32_t
}
}
}
else if (tex_format == VK_FORMAT_R32G32B32A32_SFLOAT) {
uint32_t *data32 = (uint32_t *)data;
uint32_t *tex_colors32 = (uint32_t *)pixels;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
data32[y * (layout.rowPitch / 4) + x * 4 + 0] = tex_colors32[y * width * 4 + x * 4 + 0];
data32[y * (layout.rowPitch / 4) + x * 4 + 1] = tex_colors32[y * width * 4 + x * 4 + 1];
data32[y * (layout.rowPitch / 4) + x * 4 + 2] = tex_colors32[y * width * 4 + x * 4 + 2];
data32[y * (layout.rowPitch / 4) + x * 4 + 3] = tex_colors32[y * width * 4 + x * 4 + 3];
}
}
}
else if (tex_format == VK_FORMAT_B8G8R8A8_UNORM) {
for (uint32_t y = 0; y < height; y++) {
// uint32_t *row = (uint32_t *)((char *)data + layout.rowPitch * y);
for (uint32_t x = 0; x < width; x++) {
data[y * layout.rowPitch + x * 4 + 0] = tex_colors[y * width * 4 + x * 4 + 2];
data[y * layout.rowPitch + x * 4 + 1] = tex_colors[y * width * 4 + x * 4 + 1];
data[y * layout.rowPitch + x * 4 + 2] = tex_colors[y * width * 4 + x * 4 + 0];
data[y * layout.rowPitch + x * 4 + 3] = tex_colors[y * width * 4 + x * 4 + 3];
// row[x] = tex_colors[(x & 1) ^ (y & 1)];
data[y * layout.rowPitch + x * 4 + 0] = pixels[y * width * 4 + x * 4 + 2];
data[y * layout.rowPitch + x * 4 + 1] = pixels[y * width * 4 + x * 4 + 1];
data[y * layout.rowPitch + x * 4 + 2] = pixels[y * width * 4 + x * 4 + 0];
data[y * layout.rowPitch + x * 4 + 3] = pixels[y * width * 4 + x * 4 + 3];
}
}
}
else {
for (uint32_t y = 0; y < height; y++) {
// uint32_t *row = (uint32_t *)((char *)data + layout.rowPitch * y);
for (uint32_t x = 0; x < width; x++) {
data[y * layout.rowPitch + x * 4 + 0] = tex_colors[y * width * 4 + x * 4 + 0];
data[y * layout.rowPitch + x * 4 + 1] = tex_colors[y * width * 4 + x * 4 + 1];
data[y * layout.rowPitch + x * 4 + 2] = tex_colors[y * width * 4 + x * 4 + 2];
data[y * layout.rowPitch + x * 4 + 3] = tex_colors[y * width * 4 + x * 4 + 3];
// row[x] = tex_colors[(x & 1) ^ (y & 1)];
data[y * layout.rowPitch + x * 4 + 0] = pixels[y * width * 4 + x * 4 + 0];
data[y * layout.rowPitch + x * 4 + 1] = pixels[y * width * 4 + x * 4 + 1];
data[y * layout.rowPitch + x * 4 + 2] = pixels[y * width * 4 + x * 4 + 2];
data[y * layout.rowPitch + x * 4 + 3] = pixels[y * width * 4 + x * 4 + 3];
}
}
}
vkUnmapMemory(device, tex_obj->mem);
vkUnmapMemory(device, impl->mem);
}
if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
else {
tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
set_image_layout(tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, tex_obj->imageLayout);
}
static void update_stride(gpu_texture_t *texture) {
VkImageSubresource subres = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
.arrayLayer = 0,
};
VkSubresourceLayout layout;
vkGetImageSubresourceLayout(device, texture->impl.image, &subres, &layout);
texture->impl.stride = (int)layout.rowPitch;
impl->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
set_image_layout(impl->image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, impl->imageLayout);
}
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) {
texture->width = width;
texture->height = height;
texture->format = format;
texture->data = data;
texture->impl.stage = 0;
texture->state = GPU_TEXTURE_STATE_SHADER_RESOURCE;
const VkFormat tex_format = convert_image_format(format);
VkFormat tex_format = convert_image_format(format);
VkFormatProperties props;
vkGetPhysicalDeviceFormatProperties(gpu, tex_format, &props);
if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
prepare_texture_image((uint8_t *)data, (uint32_t)width, (uint32_t)height, &texture->impl, VK_IMAGE_TILING_LINEAR,
VK_IMAGE_USAGE_SAMPLED_BIT /*| VK_IMAGE_USAGE_STORAGE_BIT*/, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &texture->impl.deviceSize, tex_format);
VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, tex_format);
}
else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
gpu_texture_impl_t staging_texture;
memset(&staging_texture, 0, sizeof(staging_texture));
prepare_texture_image((uint8_t *)data, (uint32_t)width, (uint32_t)height, &staging_texture, VK_IMAGE_TILING_LINEAR,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &texture->impl.deviceSize, tex_format);
VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, tex_format);
prepare_texture_image((uint8_t *)data, (uint32_t)width, (uint32_t)height, &texture->impl, VK_IMAGE_TILING_OPTIMAL,
(VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT /*| VK_IMAGE_USAGE_STORAGE_BIT*/),
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture->impl.deviceSize, tex_format);
(VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, tex_format);
set_image_layout(staging_texture.image, VK_IMAGE_ASPECT_COLOR_BIT, staging_texture.imageLayout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
set_image_layout(texture->impl.image, VK_IMAGE_ASPECT_COLOR_BIT, texture->impl.imageLayout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
@@ -1874,7 +1848,13 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width,
vkFreeMemory(device, staging_texture.mem, NULL);
}
update_stride(texture);
VkImageSubresource subres = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
.arrayLayer = 0,
};
VkSubresourceLayout layout;
vkGetImageSubresourceLayout(device, texture->impl.image, &subres, &layout);
VkImageViewCreateInfo view = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
@@ -1908,69 +1888,6 @@ void gpu_texture_destroy(gpu_texture_t *target) {
}
}
void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {
// VkBuffer staging_buffer;
// VkDeviceMemory staging_buffer_mem;
// VkBufferCreateInfo buffer_info = {
// .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
// .size = mipmap->width * mipmap->height * format_size(mipmap->format),
// .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
// .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
// };
// vkCreateBuffer(device, &buffer_info, NULL, &staging_buffer);
// VkMemoryRequirements mem_req;
// vkGetBufferMemoryRequirements(device, staging_buffer, &mem_req);
// VkMemoryAllocateInfo alloc_info = {
// .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
// .allocationSize = mem_req.size,
// };
// memory_type_from_properties(mem_req.memoryTypeBits,
// VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &alloc_info.memoryTypeIndex);
// vkAllocateMemory(device, &alloc_info, NULL, &staging_buffer_mem);
// vkBindBufferMemory(device, staging_buffer, staging_buffer_mem, 0);
// void *mapped_data;
// vkMapMemory(device, staging_buffer_mem, 0, buffer_info.size, 0, &mapped_data);
// memcpy(mapped_data, mipmap->data, (size_t)buffer_info.size);
// vkUnmapMemory(device, staging_buffer_mem);
// set_image_layout(texture->impl.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
// VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
// VkBufferImageCopy region = {
// .bufferOffset = 0,
// .bufferRowLength = 0,
// .bufferImageHeight = 0,
// .imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
// .imageSubresource.mipLevel = level,
// .imageSubresource.baseArrayLayer = 0,
// .imageSubresource.layerCount = 1,
// .imageOffset = (VkOffset3D){0, 0, 0},
// .imageExtent = (VkExtent3D){(uint32_t)mipmap->width, (uint32_t)mipmap->height, 1},
// };
// vkCmdCopyBufferToImage(
// command_buffer,
// staging_buffer,
// texture->impl.image,
// VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
// 1,
// &region
// );
// set_image_layout(texture->impl.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
// VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
// vkFreeMemory(device, staging_buffer_mem, NULL);
// vkDestroyBuffer(device, staging_buffer, NULL);
}
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {
gpu_render_target_init2(target, width, height, format, -1);
}
-3
View File
@@ -24,8 +24,6 @@ typedef struct {
typedef struct {
VkImageLayout imageLayout;
VkDeviceSize deviceSize;
int stride;
VkImage image;
VkDeviceMemory mem;
VkImageView view;
@@ -33,7 +31,6 @@ typedef struct {
VkBuffer readback_buffer;
VkDeviceMemory readback_memory;
bool readback_buffer_created;
int stage;
} gpu_texture_impl_t;
typedef struct {
-1
View File
@@ -161,7 +161,6 @@ int gpu_index_buffer_count(gpu_buffer_t *buffer) {
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format) {}
void gpu_texture_destroy(gpu_texture_t *texture) {}
void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level) {}
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {
target->width = target->width = width;
+16 -28
View File
@@ -1350,38 +1350,26 @@ buffer_t *gpu_get_texture_pixels(gpu_texture_t *image) {
}
image->buffer->length = _format_byte_size(image->format) * image->width * image->height;
if (image->data == NULL) {
if (image->buffer->buffer == NULL) {
image->buffer->buffer = malloc(image->buffer->length);
}
uint8_t *b = (uint8_t *)image->buffer->buffer;
gpu_get_render_target_pixels(image, b);
// Release staging texture immediately to save memory
#ifdef IRON_DIRECT3D12
image->impl.readback->lpVtbl->Release(image->impl.readback);
image->impl.readback = NULL;
#elif defined(IRON_METAL)
// id<MTLTexture> texReadback = (__bridge_transfer id<MTLTexture>)image->impl._texReadback;
// texReadback = nil;
// image->impl._texReadback = NULL;
#endif
}
else {
image->buffer->buffer = image->data;
if (image->buffer->buffer == NULL) {
image->buffer->buffer = malloc(image->buffer->length);
}
uint8_t *b = (uint8_t *)image->buffer->buffer;
gpu_get_render_target_pixels(image, b);
// Release staging texture immediately to save memory
#ifdef IRON_DIRECT3D12
image->impl.readback->lpVtbl->Release(image->impl.readback);
image->impl.readback = NULL;
#elif defined(IRON_METAL)
// id<MTLTexture> texReadback = (__bridge_transfer id<MTLTexture>)image->impl._texReadback;
// texReadback = nil;
// image->impl._texReadback = NULL;
#endif
return image->buffer;
}
void gpu_set_mipmaps(gpu_texture_t *texture, any_array_t *mipmaps) {
for (int32_t i = 0; i < mipmaps->length; ++i) {
gpu_texture_t *img = mipmaps->buffer[i];
gpu_texture_t *img_tex = img;
gpu_texture_set_mipmap(texture, img_tex, i + 1);
}
}
void _gpu_begin(gpu_texture_t *render_target, any_array_t *additional, gpu_texture_t *depth_buffer, unsigned flags, unsigned color, float depth) {
if (render_target == NULL) {
gpu_begin(NULL, 0, NULL, flags, color, depth);
-2
View File
@@ -82,7 +82,6 @@ typedef struct gpu_texture {
int height;
gpu_texture_format_t format;
gpu_texture_compression_t compression;
void *data;
gpu_texture_state_t state;
buffer_t *buffer;
gpu_texture_impl_t impl;
@@ -173,7 +172,6 @@ void gpu_set_matrix4(int location, iron_matrix4x4_t value);
void gpu_vertex_structure_add(gpu_vertex_structure_t *structure, const char *name, gpu_vertex_data_t data);
void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, int height, gpu_texture_format_t format);
void gpu_texture_destroy(gpu_texture_t *texture);
void gpu_texture_set_mipmap(gpu_texture_t *texture, gpu_texture_t *mipmap, int level);
void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_texture_format_t format);
void gpu_render_target_init2(gpu_texture_t *render_target, int width, int height, gpu_texture_format_t format, int framebuffer_index);
void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structure_t *structure);
-2
View File
@@ -274,8 +274,6 @@ void iron_start(void) {
#endif
}
void iron_memory_emergency(void) {}
static uint8_t *current_file = NULL;
static size_t current_file_size = 0;
-1
View File
@@ -67,7 +67,6 @@ function import_envmap_run(path: string, image: gpu_texture_t) {
import_envmap_get_radiance_mip(import_envmap_mips[i], i, import_envmap_radiance);
array_push(import_envmap_mips_cpu, gpu_create_texture_from_bytes(gpu_get_texture_pixels(import_envmap_mips[i]), import_envmap_mips[i].width, import_envmap_mips[i].height, tex_format_t.RGBA128));
}
gpu_set_mipmaps(import_envmap_radiance_cpu, import_envmap_mips_cpu);
// Irradiance
scene_world._.irradiance = import_envmap_get_spherical_harmonics(radiance_pixels, import_envmap_radiance.width, import_envmap_radiance.height);
+1 -1
View File
@@ -274,7 +274,6 @@ declare function gpu_create_render_target(width: i32, height: i32, format: i32 =
declare function gpu_create_texture_from_bytes(data: buffer_t, width: i32, height: i32, format: i32 = tex_format_t.RGBA32): any;
declare function gpu_create_texture_from_encoded_bytes(data: buffer_t, format: string): any;
declare function gpu_get_texture_pixels(texture: any): buffer_t;
declare function gpu_set_mipmaps(texture: any, mipmaps: gpu_texture_t[]): void;
declare function gpu_viewport(x: i32, y: i32, width: i32, height: i32): void;
declare function gpu_scissor(x: i32, y: i32, width: i32, height: i32): void;
declare function gpu_disable_scissor(): void;
@@ -770,6 +769,7 @@ declare type gpu_texture_t = {
width: i32;
height: i32;
};
declare type ui_theme_t = any;
declare type ui_t = {
-23
View File
@@ -81,29 +81,6 @@ function material_context_create(raw: material_context_t): material_context_t {
let image: gpu_texture_t = data_get_image(tex.file);
array_push(raw._.textures, image);
// Set mipmaps
if (tex.mipmaps != null) {
let mipmaps: gpu_texture_t[] = [];
while (mipmaps.length < tex.mipmaps.length) {
array_push(mipmaps, null);
}
for (let j: i32 = 0; j < tex.mipmaps.length; ++j) {
let name: string = tex.mipmaps[j];
let mipimg: gpu_texture_t = data_get_image(name);
mipmaps[j] = mipimg;
}
gpu_set_mipmaps(image, mipmaps);
tex.mipmaps = null;
tex.generate_mipmaps = false;
}
else if (tex.generate_mipmaps == true && image != null) {
// gpu_texture_generate_mipmaps(image, 1000);
tex.mipmaps = null;
tex.generate_mipmaps = false;
}
}
}
-2
View File
@@ -6,7 +6,6 @@ type render_target_t = {
// Opt
format?: string;
scale?: f32;
mipmaps?: bool;
// Runtime
_image?: gpu_texture_t;
};
@@ -322,7 +321,6 @@ function render_path_get_tex_format(s: string): tex_format_t {
function render_target_create(): render_target_t {
let raw: render_target_t = {};
raw.scale = 1.0;
raw.mipmaps = false;
return raw;
}
-2
View File
@@ -553,8 +553,6 @@ type bind_const_t = {
type bind_tex_t = {
name?: string;
file?: string;
generate_mipmaps?: bool;
mipmaps?: string[]; // Reference image names
};
type shader_data_t = {
-1
View File
@@ -28,7 +28,6 @@ function world_data_parse(name: string, id: string): world_data_t {
let mipimg: gpu_texture_t = data_get_image(base + "_" + i + ext);
raw._.radiance_mipmaps[i] = mipimg;
}
gpu_set_mipmaps(raw._.radiance, raw._.radiance_mipmaps);
}
return raw;
-3
View File
@@ -2011,9 +2011,6 @@ function parser_material_make_bind_tex(tex_name: string, file: string): bind_tex
name: tex_name,
file: file
};
if (context_raw.texture_filter) {
tex.generate_mipmaps = true;
}
return tex;
}
-2
View File
@@ -255,8 +255,6 @@ function util_clone_bind_textures(texs: bind_tex_t[]): bind_tex_t[] {
let t: bind_tex_t = {};
t.name = texs[i].name;
t.file = texs[i].file;
t.generate_mipmaps = texs[i].generate_mipmaps;
t.mipmaps = util_clone_string_array(texs[i].mipmaps);
array_push(r, t);
}
return r;