Skip to content

Commit

Permalink
Use g5
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Feb 17, 2025
1 parent fc74872 commit 4a6435b
Show file tree
Hide file tree
Showing 27 changed files with 251 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
render_target->impl.renderTarget->lpVtbl->GetDesc(render_target->impl.renderTarget, &desc);
DXGI_FORMAT dxgiFormat = desc.Format;
int formatByteSize = formatSize(dxgiFormat);
int rowPitch = render_target->tex_width * formatByteSize;
int rowPitch = render_target->width * formatByteSize;
int align = rowPitch % d3d12_textureAlignment();
if (align != 0)
rowPitch = rowPitch + (d3d12_textureAlignment() - align);
Expand All @@ -395,7 +395,7 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
D3D12_RESOURCE_DESC resourceDesc;
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
resourceDesc.Alignment = 0;
resourceDesc.Width = rowPitch * render_target->tex_height;
resourceDesc.Width = rowPitch * render_target->height;
resourceDesc.Height = 1;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
Expand Down Expand Up @@ -433,8 +433,8 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
dest.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
dest.PlacedFootprint.Offset = 0;
dest.PlacedFootprint.Footprint.Format = dxgiFormat;
dest.PlacedFootprint.Footprint.Width = render_target->tex_width;
dest.PlacedFootprint.Footprint.Height = render_target->tex_height;
dest.PlacedFootprint.Footprint.Width = render_target->width;
dest.PlacedFootprint.Footprint.Height = render_target->height;
dest.PlacedFootprint.Footprint.Depth = 1;
dest.PlacedFootprint.Footprint.RowPitch = rowPitch;

Expand All @@ -459,7 +459,7 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
// Read buffer
void *p;
render_target->impl.renderTargetReadback->lpVtbl->Map(render_target->impl.renderTargetReadback, 0, NULL, &p);
memcpy(data, p, render_target->tex_width * render_target->tex_height * formatByteSize);
memcpy(data, p, render_target->width * render_target->height * formatByteSize);
render_target->impl.renderTargetReadback->lpVtbl->Unmap(render_target->impl.renderTargetReadback, 0, NULL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,8 @@ void kinc_raytrace_set_target(kinc_g5_render_target_t *_output) {
heapProperties.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC desc = {};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = _output->tex_width;
desc.Height = _output->tex_height;
desc.Width = _output->width;
desc.Height = _output->height;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
Expand Down Expand Up @@ -893,8 +893,8 @@ void kinc_raytrace_dispatch_rays(kinc_g5_command_list_t *command_list) {
dispatchDesc.RayGenerationShaderRecord.StartAddress = pipeline->impl.raygen_shader_table->lpVtbl->GetGPUVirtualAddress(pipeline->impl.raygen_shader_table);
pipeline->impl.raygen_shader_table->lpVtbl->GetDesc(pipeline->impl.raygen_shader_table, &desc);
dispatchDesc.RayGenerationShaderRecord.SizeInBytes = desc.Width;
dispatchDesc.Width = output->tex_width;
dispatchDesc.Height = output->tex_height;
dispatchDesc.Width = output->width;
dispatchDesc.Height = output->height;
dispatchDesc.Depth = 1;
dxrCommandList->lpVtbl->SetPipelineState1(dxrCommandList, pipeline->impl.dxr_state);
dxrCommandList->lpVtbl->DispatchRays(dxrCommandList, &dispatchDesc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ static DXGI_FORMAT convertFormat(kinc_g5_render_target_format_t format) {
void kinc_memory_emergency();

static void render_target_init(kinc_g5_render_target_t *render_target, int width, int height, kinc_g5_render_target_format_t format, int depthBufferBits, int framebuffer_index) {
render_target->tex_width = render_target->width = width;
render_target->tex_height = render_target->height = height;
render_target->width = render_target->width = width;
render_target->height = render_target->height = height;
render_target->impl.stage = 0;
render_target->impl.stage_depth = -1;
render_target->impl.renderTargetReadback = NULL;
Expand All @@ -71,8 +71,8 @@ static void render_target_init(kinc_g5_render_target_t *render_target, int width
D3D12_RESOURCE_DESC texResourceDesc;
texResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texResourceDesc.Alignment = 0;
texResourceDesc.Width = render_target->tex_width;
texResourceDesc.Height = render_target->tex_height;
texResourceDesc.Width = render_target->width;
texResourceDesc.Height = render_target->height;
texResourceDesc.DepthOrArraySize = 1;
texResourceDesc.MipLevels = 1;
texResourceDesc.Format = dxgiFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ void kinc_g5_texture_init_from_image(kinc_g5_texture_t *texture, kinc_image_t *i
memset(&texture->impl, 0, sizeof(texture->impl));
texture->impl.stage = 0;
texture->impl.mipmap = true;
texture->tex_width = image->width;
texture->tex_height = image->height;
texture->width = image->width;
texture->height = image->height;
texture->_uploaded = false;
texture->format = image->format;

Expand All @@ -184,8 +184,8 @@ void kinc_g5_texture_init_from_image(kinc_g5_texture_t *texture, kinc_image_t *i
D3D12_RESOURCE_DESC resourceDescTex;
resourceDescTex.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDescTex.Alignment = 0;
resourceDescTex.Width = texture->tex_width;
resourceDescTex.Height = texture->tex_height;
resourceDescTex.Width = texture->width;
resourceDescTex.Height = texture->height;
resourceDescTex.DepthOrArraySize = 1;
resourceDescTex.MipLevels = 1;
resourceDescTex.Format = d3dformat;
Expand Down Expand Up @@ -250,8 +250,8 @@ void kinc_g5_texture_init_from_image(kinc_g5_texture_t *texture, kinc_image_t *i
BYTE *pixel;
texture->impl.uploadImage->lpVtbl->Map(texture->impl.uploadImage, 0, NULL, (void **)&pixel);
int pitch = kinc_g5_texture_stride(texture);
for (int y = 0; y < texture->tex_height; ++y) {
memcpy(&pixel[y * pitch], &((uint8_t *)image->data)[y * texture->tex_width * formatSize], texture->tex_width * formatSize);
for (int y = 0; y < texture->height; ++y) {
memcpy(&pixel[y * pitch], &((uint8_t *)image->data)[y * texture->width * formatSize], texture->width * formatSize);
}
texture->impl.uploadImage->lpVtbl->Unmap(texture->impl.uploadImage, 0, NULL);

Expand Down Expand Up @@ -283,8 +283,8 @@ void create_texture(struct kinc_g5_texture *texture, int width, int height, kinc
memset(&texture->impl, 0, sizeof(texture->impl));
texture->impl.stage = 0;
texture->impl.mipmap = true;
texture->tex_width = width;
texture->tex_height = height;
texture->width = width;
texture->height = height;

DXGI_FORMAT d3dformat = convertImageFormat(format);

Expand All @@ -298,8 +298,8 @@ void create_texture(struct kinc_g5_texture *texture, int width, int height, kinc
D3D12_RESOURCE_DESC resourceDescTex;
resourceDescTex.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDescTex.Alignment = 0;
resourceDescTex.Width = texture->tex_width;
resourceDescTex.Height = texture->tex_height;
resourceDescTex.Width = texture->width;
resourceDescTex.Height = texture->height;
resourceDescTex.DepthOrArraySize = 1;
resourceDescTex.MipLevels = 1;
resourceDescTex.Format = d3dformat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void kinc_g5_command_list_scissor(kinc_g5_command_list_t *list, int x, int y, in
int target_w = -1;
int target_h = -1;
if (lastRenderTargets[0] != NULL) {
target_w = lastRenderTargets[0]->tex_width;
target_h = lastRenderTargets[0]->tex_height;
target_w = lastRenderTargets[0]->width;
target_h = lastRenderTargets[0]->height;
}
else {
target_w = kinc_window_width();
Expand All @@ -115,8 +115,8 @@ void kinc_g5_command_list_disable_scissor(kinc_g5_command_list_t *list) {
scissor.x = 0;
scissor.y = 0;
if (lastRenderTargets[0] != NULL) {
scissor.width = lastRenderTargets[0]->tex_width;
scissor.height = lastRenderTargets[0]->tex_height;
scissor.width = lastRenderTargets[0]->width;
scissor.height = lastRenderTargets[0]->height;
}
else {
scissor.width = kinc_window_width();
Expand Down Expand Up @@ -167,8 +167,8 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
id<MTLDevice> device = getMetalDevice();
MTLTextureDescriptor *descriptor = [MTLTextureDescriptor new];
descriptor.textureType = MTLTextureType2D;
descriptor.width = render_target->tex_width;
descriptor.height = render_target->tex_height;
descriptor.width = render_target->width;
descriptor.height = render_target->height;
descriptor.depth = 1;
descriptor.pixelFormat = [(__bridge id<MTLTexture>)render_target->impl._tex pixelFormat];
descriptor.arrayLength = 1;
Expand All @@ -186,7 +186,7 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(render_target->tex_width, render_target->tex_height, 1)
sourceSize:MTLSizeMake(render_target->width, render_target->height, 1)
toTexture:(__bridge id<MTLTexture>)render_target->impl._texReadback
destinationSlice:0
destinationLevel:0
Expand All @@ -198,8 +198,8 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
// Read buffer
id<MTLTexture> tex = (__bridge id<MTLTexture>)render_target->impl._texReadback;
int formatByteSize = formatSize([(__bridge id<MTLTexture>)render_target->impl._tex pixelFormat]);
MTLRegion region = MTLRegionMake2D(0, 0, render_target->tex_width, render_target->tex_height);
[tex getBytes:data bytesPerRow:formatByteSize * render_target->tex_width fromRegion:region mipmapLevel:0];
MTLRegion region = MTLRegionMake2D(0, 0, render_target->width, render_target->height);
[tex getBytes:data bytesPerRow:formatByteSize * render_target->width fromRegion:region mipmapLevel:0];
}

void kinc_g5_command_list_execute(kinc_g5_command_list_t *list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ void kinc_raytrace_dispatch_rays(kinc_g5_command_list_t *command_list) {
dispatch_semaphore_signal(sem);
}];

NSUInteger width = output->tex_width;
NSUInteger height = output->tex_height;
NSUInteger width = output->width;
NSUInteger height = output->height;
MTLSize threads_per_threadgroup = MTLSizeMake(8, 8, 1);
MTLSize threadgroups = MTLSizeMake((width + threads_per_threadgroup.width - 1) / threads_per_threadgroup.width,
(height + threads_per_threadgroup.height - 1) / threads_per_threadgroup.height, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static MTLPixelFormat convert_format(kinc_g5_render_target_format_t format) {
static void render_target_init(kinc_g5_render_target_t *target, int width, int height, kinc_g5_render_target_format_t format, int depthBufferBits, int framebuffer_index) {
memset(target, 0, sizeof(kinc_g5_render_target_t));

target->tex_width = width;
target->tex_height = height;
target->width = width;
target->height = height;

target->framebuffer_index = framebuffer_index;

Expand Down Expand Up @@ -67,6 +67,9 @@ static void render_target_init(kinc_g5_render_target_t *target, int width, int h

void kinc_g5_render_target_init(kinc_g5_render_target_t *target, int width, int height, kinc_g5_render_target_format_t format, int depthBufferBits) {
render_target_init(target, width, height, format, depthBufferBits, -1);
target->width = target->width = width;
target->height = target->height = height;
target->state = KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}

static int framebuffer_count = 0;
Expand Down
42 changes: 21 additions & 21 deletions armorcore/sources/backends/metal/kinc/backend/graphics5/texture.m.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,34 @@ static void create(kinc_g5_texture_t *texture, int width, int height, int format
}

void kinc_g5_texture_init(kinc_g5_texture_t *texture, int width, int height, kinc_image_format_t format) {
texture->tex_width = width;
texture->tex_height = height;
texture->width = width;
texture->height = height;
texture->format = format;
texture->impl.data = malloc(width * height * (format == KINC_IMAGE_FORMAT_GREY8 ? 1 : 4));
create(texture, width, height, format, true);
texture->_uploaded = true;
}

void kinc_g5_texture_init_from_image(kinc_g5_texture_t *texture, struct kinc_image *image) {
texture->tex_width = image->width;
texture->tex_height = image->height;
texture->width = image->width;
texture->height = image->height;
texture->format = image->format;
texture->_uploaded = false;
texture->format = image->format;
texture->impl.data = NULL;
create(texture, image->width, image->height, image->format, true);
id<MTLTexture> tex = (__bridge id<MTLTexture>)texture->impl._tex;
[tex replaceRegion:MTLRegionMake2D(0, 0, texture->tex_width, texture->tex_height)
[tex replaceRegion:MTLRegionMake2D(0, 0, texture->width, texture->height)
mipmapLevel:0
slice:0
withBytes:image->data
bytesPerRow:kinc_g5_texture_stride(texture)
bytesPerImage:kinc_g5_texture_stride(texture) * texture->tex_height];
bytesPerImage:kinc_g5_texture_stride(texture) * texture->height];
}

void kinc_g5_texture_init_non_sampled_access(kinc_g5_texture_t *texture, int width, int height, kinc_image_format_t format) {
texture->tex_width = width;
texture->tex_height = height;
texture->width = width;
texture->height = height;
texture->format = format;
texture->impl.data = malloc(width * height * (format == KINC_IMAGE_FORMAT_GREY8 ? 1 : 4));
create(texture, width, height, format, true);
Expand All @@ -125,19 +125,19 @@ id getMetalEncoder(void);
int kinc_g5_texture_stride(kinc_g5_texture_t *texture) {
switch (texture->format) {
case KINC_IMAGE_FORMAT_GREY8:
return texture->tex_width;
return texture->width;
case KINC_IMAGE_FORMAT_RGBA32:
case KINC_IMAGE_FORMAT_BGRA32:
case KINC_IMAGE_FORMAT_RGB24:
return texture->tex_width * 4;
return texture->width * 4;
case KINC_IMAGE_FORMAT_RGBA64:
return texture->tex_width * 8;
return texture->width * 8;
case KINC_IMAGE_FORMAT_RGBA128:
return texture->tex_width * 16;
return texture->width * 16;
case KINC_IMAGE_FORMAT_A16:
return texture->tex_width * 2;
return texture->width * 2;
case KINC_IMAGE_FORMAT_A32:
return texture->tex_width * 4;
return texture->width * 4;
}
}

Expand All @@ -147,12 +147,12 @@ uint8_t *kinc_g5_texture_lock(kinc_g5_texture_t *texture) {

void kinc_g5_texture_unlock(kinc_g5_texture_t *tex) {
id<MTLTexture> texture = (__bridge id<MTLTexture>)tex->impl._tex;
[texture replaceRegion:MTLRegionMake2D(0, 0, tex->tex_width, tex->tex_height)
[texture replaceRegion:MTLRegionMake2D(0, 0, tex->width, tex->height)
mipmapLevel:0
slice:0
withBytes:tex->impl.data
bytesPerRow:kinc_g5_texture_stride(tex)
bytesPerImage:kinc_g5_texture_stride(tex) * tex->tex_height];
bytesPerImage:kinc_g5_texture_stride(tex) * tex->height];
texture->_uploaded = false;
}

Expand All @@ -162,12 +162,12 @@ void kinc_g5_texture_set_mipmap(kinc_g5_texture_t *texture, kinc_image_t *mipmap
if (!texture->impl.has_mipmaps) {
id<MTLDevice> device = getMetalDevice();
MTLTextureDescriptor *descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:convert_image_format((kinc_image_format_t)texture->format)
width:texture->tex_width
height:texture->tex_height
width:texture->width
height:texture->height
mipmapped:YES];
descriptor.textureType = MTLTextureType2D;
descriptor.width = texture->tex_width;
descriptor.height = texture->tex_height;
descriptor.width = texture->width;
descriptor.height = texture->height;
descriptor.depth = 1;
descriptor.pixelFormat = convert_image_format((kinc_image_format_t)texture->format);
descriptor.arrayLength = 1;
Expand All @@ -184,7 +184,7 @@ void kinc_g5_texture_set_mipmap(kinc_g5_texture_t *texture, kinc_image_t *mipmap
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(texture->tex_width, texture->tex_height, 1)
sourceSize:MTLSizeMake(texture->width, texture->height, 1)
toTexture:(__bridge id<MTLTexture>)mipmaptex
destinationSlice:0
destinationLevel:0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ void kinc_g5_command_list_get_render_target_pixels(kinc_g5_command_list_t *list,
// Read buffer
void *p;
vkMapMemory(vk_ctx.device, render_target->impl.readbackMemory, 0, VK_WHOLE_SIZE, 0, (void **)&p);
memcpy(data, p, render_target->tex_width * render_target->tex_height * format_byteS_size);
memcpy(data, p, render_target->width * render_target->height * format_byteS_size);
vkUnmapMemory(vk_ctx.device, render_target->impl.readbackMemory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ void kinc_raytrace_dispatch_rays(kinc_g5_command_list_t *command_list) {

_vkCmdTraceRaysKHR = (void *)vkGetDeviceProcAddr(vk_ctx.device, "vkCmdTraceRaysKHR");
_vkCmdTraceRaysKHR(command_list->impl._buffer, &raygen_shader_sbt_entry, &miss_shader_sbt_entry, &hit_shader_sbt_entry, &callable_shader_sbt_entry,
output->tex_width, output->tex_height, 1);
output->width, output->height, 1);

vkCmdBeginRenderPass(command_list->impl._buffer, &currentRenderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ void setImageLayout(VkCommandBuffer _buffer, VkImage image, VkImageAspectFlags a
}

static void render_target_init(kinc_g5_render_target_t *target, int width, int height, kinc_g5_render_target_format_t format, int depthBufferBits, int framebuffer_index) {
target->framebuffer_index = framebuffer_index;
target->width = width;
target->height = height;
target->framebuffer_index = framebuffer_index;
target->tex_width = width;
target->tex_height = height;
target->impl.format = convert_format(format);
target->impl.depthBufferBits = depthBufferBits;
target->impl.stage = 0;
Expand Down Expand Up @@ -243,6 +241,9 @@ static void render_target_init(kinc_g5_render_target_t *target, int width, int h

void kinc_g5_render_target_init(kinc_g5_render_target_t *target, int width, int height, kinc_g5_render_target_format_t format, int depthBufferBits) {
render_target_init(target, width, height, format, depthBufferBits, -1);
target->width = width;
target->height = height;
target->state = KINC_INTERNAL_RENDER_TARGET_STATE_RENDER_TARGET;
}

static int framebuffer_count = 0;
Expand Down
Loading

0 comments on commit 4a6435b

Please sign in to comment.