This commit is contained in:
luboslenco
2025-07-12 15:20:29 +02:00
parent af075de9bc
commit 4f68bfc9ef
8 changed files with 48 additions and 81 deletions
+5 -9
View File
@@ -384,14 +384,14 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t *depth
gpu_scissor(0, 0, target->width, target->height);
if (flags & GPU_CLEAR_COLOR) {
float clearColor[] = {((color & 0x00ff0000) >> 16) / 255.0f,
((color & 0x0000ff00) >> 8) / 255.0f,
(color & 0x000000ff) / 255.0f,
((color & 0xff000000) >> 24) / 255.0f};
float clear_color[] = {((color & 0x00ff0000) >> 16) / 255.0f,
((color & 0x0000ff00) >> 8) / 255.0f,
(color & 0x000000ff) / 255.0f,
((color & 0xff000000) >> 24) / 255.0f};
D3D12_CPU_DESCRIPTOR_HANDLE handle;
target->impl.rtv_descriptor_heap->lpVtbl->GetCPUDescriptorHandleForHeapStart(target->impl.rtv_descriptor_heap, &handle);
command_list->lpVtbl->ClearRenderTargetView(command_list, handle, clearColor, 0, NULL);
command_list->lpVtbl->ClearRenderTargetView(command_list, handle, clear_color, 0, NULL);
}
if (flags & GPU_CLEAR_DEPTH && depth_buffer != NULL) {
D3D12_CPU_DESCRIPTOR_HANDLE handle;
@@ -672,10 +672,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) {
current_textures[unit] = texture;
}
void gpu_pipeline_init(gpu_pipeline_t *pipe) {
gpu_internal_pipeline_init(pipe);
}
void gpu_pipeline_destroy(gpu_pipeline_t *pipe) {
if (pipe->impl.pso != NULL) {
pipe->impl.pso->lpVtbl->Release(pipe->impl.pso);
-4
View File
@@ -1,10 +1,6 @@
#pragma once
struct gpu_shader;
typedef struct {
struct gpu_shader *vertex_shader;
struct gpu_shader *fragment_shader;
void *_pipeline;
void *_depth;
} gpu_pipeline_impl_t;
+30 -35
View File
@@ -395,11 +395,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) {
[command_encoder useResource:tex usage:MTLResourceUsageRead stages:MTLRenderStageVertex|MTLRenderStageFragment];
}
void gpu_pipeline_init(gpu_pipeline_t *pipeline) {
memset(&pipeline->impl, 0, sizeof(pipeline->impl));
gpu_internal_pipeline_init(pipeline);
}
void gpu_pipeline_destroy(gpu_pipeline_t *pipeline) {
id<MTLRenderPipelineState> pipe = (__bridge_transfer id<MTLRenderPipelineState>)pipeline->impl._pipeline;
pipe = nil;
@@ -424,77 +419,77 @@ void gpu_pipeline_compile(gpu_pipeline_t *pipeline) {
pipeline->fragment_shader->impl.mtl_function = (__bridge_retained void *)[library newFunctionWithName:[NSString stringWithCString:pipeline->fragment_shader->impl.name encoding:NSUTF8StringEncoding]];
assert(pipeline->fragment_shader->impl.mtl_function);
MTLRenderPipelineDescriptor *renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init];
renderPipelineDesc.vertexFunction = (__bridge id<MTLFunction>)pipeline->vertex_shader->impl.mtl_function;
renderPipelineDesc.fragmentFunction = (__bridge id<MTLFunction>)pipeline->fragment_shader->impl.mtl_function;
MTLRenderPipelineDescriptor *render_pipeline_desc = [[MTLRenderPipelineDescriptor alloc] init];
render_pipeline_desc.vertexFunction = (__bridge id<MTLFunction>)pipeline->vertex_shader->impl.mtl_function;
render_pipeline_desc.fragmentFunction = (__bridge id<MTLFunction>)pipeline->fragment_shader->impl.mtl_function;
for (int i = 0; i < pipeline->color_attachment_count; ++i) {
renderPipelineDesc.colorAttachments[i].pixelFormat = convert_texture_format(pipeline->color_attachment[i]);
renderPipelineDesc.colorAttachments[i].blendingEnabled =
render_pipeline_desc.colorAttachments[i].pixelFormat = convert_texture_format(pipeline->color_attachment[i]);
render_pipeline_desc.colorAttachments[i].blendingEnabled =
pipeline->blend_source != GPU_BLEND_ONE || pipeline->blend_destination != GPU_BLEND_ZERO ||
pipeline->alpha_blend_source != GPU_BLEND_ONE || pipeline->alpha_blend_destination != GPU_BLEND_ZERO;
renderPipelineDesc.colorAttachments[i].sourceRGBBlendFactor = convert_blending_factor(pipeline->blend_source);
renderPipelineDesc.colorAttachments[i].destinationRGBBlendFactor = convert_blending_factor(pipeline->blend_destination);
renderPipelineDesc.colorAttachments[i].rgbBlendOperation = MTLBlendOperationAdd;
renderPipelineDesc.colorAttachments[i].sourceAlphaBlendFactor = convert_blending_factor(pipeline->alpha_blend_source);
renderPipelineDesc.colorAttachments[i].destinationAlphaBlendFactor = convert_blending_factor(pipeline->alpha_blend_destination);
renderPipelineDesc.colorAttachments[i].alphaBlendOperation = MTLBlendOperationAdd;
renderPipelineDesc.colorAttachments[i].writeMask =
render_pipeline_desc.colorAttachments[i].sourceRGBBlendFactor = convert_blending_factor(pipeline->blend_source);
render_pipeline_desc.colorAttachments[i].destinationRGBBlendFactor = convert_blending_factor(pipeline->blend_destination);
render_pipeline_desc.colorAttachments[i].rgbBlendOperation = MTLBlendOperationAdd;
render_pipeline_desc.colorAttachments[i].sourceAlphaBlendFactor = convert_blending_factor(pipeline->alpha_blend_source);
render_pipeline_desc.colorAttachments[i].destinationAlphaBlendFactor = convert_blending_factor(pipeline->alpha_blend_destination);
render_pipeline_desc.colorAttachments[i].alphaBlendOperation = MTLBlendOperationAdd;
render_pipeline_desc.colorAttachments[i].writeMask =
(pipeline->color_write_mask_red[i] ? MTLColorWriteMaskRed : 0) |
(pipeline->color_write_mask_green[i] ? MTLColorWriteMaskGreen : 0) |
(pipeline->color_write_mask_blue[i] ? MTLColorWriteMaskBlue : 0) |
(pipeline->color_write_mask_alpha[i] ? MTLColorWriteMaskAlpha : 0);
}
renderPipelineDesc.depthAttachmentPixelFormat = pipeline->depth_attachment_bits > 0 ? MTLPixelFormatDepth32Float : MTLPixelFormatInvalid;
render_pipeline_desc.depthAttachmentPixelFormat = pipeline->depth_attachment_bits > 0 ? MTLPixelFormatDepth32Float : MTLPixelFormatInvalid;
float offset = 0;
MTLVertexDescriptor *vertexDescriptor = [[MTLVertexDescriptor alloc] init];
MTLVertexDescriptor *vertex_descriptor = [[MTLVertexDescriptor alloc] init];
for (int i = 0; i < pipeline->input_layout->size; ++i) {
vertexDescriptor.attributes[i].bufferIndex = 0;
vertexDescriptor.attributes[i].offset = offset;
vertex_descriptor.attributes[i].bufferIndex = 0;
vertex_descriptor.attributes[i].offset = offset;
offset += gpu_vertex_data_size(pipeline->input_layout->elements[i].data);
switch (pipeline->input_layout->elements[i].data) {
case GPU_VERTEX_DATA_F32_1X:
vertexDescriptor.attributes[i].format = MTLVertexFormatFloat;
vertex_descriptor.attributes[i].format = MTLVertexFormatFloat;
break;
case GPU_VERTEX_DATA_F32_2X:
vertexDescriptor.attributes[i].format = MTLVertexFormatFloat2;
vertex_descriptor.attributes[i].format = MTLVertexFormatFloat2;
break;
case GPU_VERTEX_DATA_F32_3X:
vertexDescriptor.attributes[i].format = MTLVertexFormatFloat3;
vertex_descriptor.attributes[i].format = MTLVertexFormatFloat3;
break;
case GPU_VERTEX_DATA_F32_4X:
vertexDescriptor.attributes[i].format = MTLVertexFormatFloat4;
vertex_descriptor.attributes[i].format = MTLVertexFormatFloat4;
break;
case GPU_VERTEX_DATA_I16_2X_NORM:
vertexDescriptor.attributes[i].format = MTLVertexFormatShort2Normalized;
vertex_descriptor.attributes[i].format = MTLVertexFormatShort2Normalized;
break;
case GPU_VERTEX_DATA_I16_4X_NORM:
vertexDescriptor.attributes[i].format = MTLVertexFormatShort4Normalized;
vertex_descriptor.attributes[i].format = MTLVertexFormatShort4Normalized;
break;
}
}
vertexDescriptor.layouts[0].stride = offset;
vertexDescriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
vertex_descriptor.layouts[0].stride = offset;
vertex_descriptor.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
renderPipelineDesc.vertexDescriptor = vertexDescriptor;
render_pipeline_desc.vertexDescriptor = vertex_descriptor;
NSError *errors = nil;
MTLRenderPipelineReflection *reflection = nil;
pipeline->impl._pipeline = (__bridge_retained void *)[
device newRenderPipelineStateWithDescriptor:renderPipelineDesc
device newRenderPipelineStateWithDescriptor:render_pipeline_desc
options:MTLPipelineOptionBufferTypeInfo
reflection:&reflection
error:&errors];
MTLDepthStencilDescriptor *depthStencilDescriptor = [MTLDepthStencilDescriptor new];
depthStencilDescriptor.depthCompareFunction = convert_compare_mode(pipeline->depth_mode);
depthStencilDescriptor.depthWriteEnabled = pipeline->depth_write;
pipeline->impl._depth = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depthStencilDescriptor];
MTLDepthStencilDescriptor *depth_descriptor = [MTLDepthStencilDescriptor new];
depth_descriptor.depthCompareFunction = convert_compare_mode(pipeline->depth_mode);
depth_descriptor.depthWriteEnabled = pipeline->depth_write;
pipeline->impl._depth = (__bridge_retained void *)[device newDepthStencilStateWithDescriptor:depth_descriptor];
}
void gpu_shader_destroy(gpu_shader_t *shader) {
+8 -12
View File
@@ -1120,14 +1120,14 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * dept
int count = 0;
VkClearAttachment attachments[2];
if (flags & GPU_CLEAR_COLOR) {
VkClearColorValue clearColor = {0};
clearColor.float32[0] = ((color & 0x00ff0000) >> 16) / 255.0f;
clearColor.float32[1] = ((color & 0x0000ff00) >> 8) / 255.0f;
clearColor.float32[2] = (color & 0x000000ff) / 255.0f;
clearColor.float32[3] = ((color & 0xff000000) >> 24) / 255.0f;
VkClearColorValue clear_color = {0};
clear_color.float32[0] = ((color & 0x00ff0000) >> 16) / 255.0f;
clear_color.float32[1] = ((color & 0x0000ff00) >> 8) / 255.0f;
clear_color.float32[2] = (color & 0x000000ff) / 255.0f;
clear_color.float32[3] = ((color & 0xff000000) >> 24) / 255.0f;
attachments[count].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
attachments[count].colorAttachment = 0;
attachments[count].clearValue.color = clearColor;
attachments[count].clearValue.color = clear_color;
count++;
}
if (flags & GPU_CLEAR_DEPTH) {
@@ -1136,7 +1136,7 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * dept
attachments[count].clearValue.depthStencil.stencil = 0;
count++;
}
VkClearRect clearRect = {
VkClearRect clear_rect = {
.rect.offset.x = 0,
.rect.offset.y = 0,
.rect.extent.width = targets[0]->width,
@@ -1144,7 +1144,7 @@ void gpu_begin_internal(gpu_texture_t **targets, int count, gpu_texture_t * dept
.baseArrayLayer = 0,
.layerCount = 1,
};
vkCmdClearAttachments(command_buffer, count, attachments, 1, &clearRect);
vkCmdClearAttachments(command_buffer, count, attachments, 1, &clear_rect);
}
}
@@ -1421,10 +1421,6 @@ void gpu_set_texture(int unit, gpu_texture_t *texture) {
current_textures[unit] = texture;
}
void gpu_pipeline_init(gpu_pipeline_t *pipeline) {
gpu_internal_pipeline_init(pipeline);
}
void gpu_pipeline_destroy(gpu_pipeline_t *pipeline) {
vkDestroyPipeline(device, pipeline->impl.pipeline, NULL);
vkDestroyPipelineLayout(device, pipeline->impl.pipeline_layout, NULL);
-4
View File
@@ -147,10 +147,6 @@ void gpu_render_target_init(gpu_texture_t *target, int width, int height, gpu_te
void gpu_render_target_init_framebuffer(gpu_texture_t *target, int width, int height, gpu_texture_format_t format) {}
void gpu_pipeline_init(gpu_pipeline_t *pipe) {
gpu_internal_pipeline_init(pipe);
}
void gpu_pipeline_compile(gpu_pipeline_t *pipe) {
WGPUColorTargetState csDesc;
memset(&csDesc, 0, sizeof(csDesc));
+4 -15
View File
@@ -512,16 +512,14 @@ bool draw_font_get_baked_quad(draw_font_t *font, int size, draw_font_aligned_qua
void draw_string(const char *text, float x, float y) {
draw_font_image_t *img = draw_font_get_image_internal(draw_font, draw_font_size);
gpu_texture_t *tex = img->tex;
float xpos = x;
float ypos = y + img->baseline;
draw_font_aligned_quad_t q;
// gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline);
// gpu_set_vertex_buffer(&rect_vertex_buffer);
// gpu_set_index_buffer(&rect_index_buffer);
// gpu_set_texture(text_tex_unit, tex);
gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline);
gpu_set_vertex_buffer(&rect_vertex_buffer);
gpu_set_index_buffer(&rect_index_buffer);
gpu_set_texture(text_tex_unit, img->tex);
for (int i = 0; text[i] != 0; ) {
int l = 0;
@@ -530,17 +528,8 @@ void draw_string(const char *text, float x, float y) {
if (draw_font_get_baked_quad(draw_font, draw_font_size, &q, codepoint, xpos, ypos)) {
xpos += q.xadvance;
//
gpu_set_pipeline(draw_custom_pipeline != NULL ? draw_custom_pipeline : _draw_current != NULL ? &text_pipeline_rt : &text_pipeline);
gpu_set_vertex_buffer(&rect_vertex_buffer);
gpu_set_index_buffer(&rect_index_buffer);
gpu_set_texture(text_tex_unit, tex);
//
gpu_set_float4(text_pos_loc, q.x0 / vw(), q.y0 / vh(), (q.x1 - q.x0) / vw(), (q.y1 - q.y0) / vh());
gpu_set_float4(text_tex_loc, q.s0, q.t0, q.s1 - q.s0, q.t1 - q.t0);
gpu_set_matrix4(text_p_loc, draw_projection_matrix);
gpu_set_float4(text_col_loc, _draw_color_r(draw_color) / 255.0, _draw_color_g(draw_color) / 255.0, _draw_color_b(draw_color) / 255.0, _draw_color_a(draw_color) / 255.0);
gpu_draw();
+1 -1
View File
@@ -223,7 +223,7 @@ void gpu_vertex_structure_add(gpu_vertex_structure_t *structure, const char *nam
structure->size++;
}
void gpu_internal_pipeline_init(gpu_pipeline_t *pipe) {
void gpu_pipeline_init(gpu_pipeline_t *pipe) {
pipe->input_layout = NULL;
pipe->vertex_shader = NULL;
pipe->fragment_shader = NULL;
-1
View File
@@ -184,7 +184,6 @@ void *gpu_index_buffer_lock(gpu_buffer_t *buffer);
void gpu_index_buffer_unlock(gpu_buffer_t *buffer);
void gpu_pipeline_init(gpu_pipeline_t *pipeline);
void gpu_internal_pipeline_init(gpu_pipeline_t *pipeline);
void gpu_pipeline_destroy(gpu_pipeline_t *pipeline);
void gpu_pipeline_compile(gpu_pipeline_t *pipeline);
void gpu_shader_init(gpu_shader_t *shader, const void *source, size_t length, gpu_shader_type_t type);