Browse Source

Handle shader compilation error

luboslenco 1 week ago
parent
commit
b36002dd50
3 changed files with 13 additions and 8 deletions
  1. 5 7
      base/sources/backends/metal_gpu.m
  2. 4 0
      base/sources/iron.h
  3. 4 1
      base/sources/iron_gpu.c

+ 5 - 7
base/sources/backends/metal_gpu.m

@@ -321,13 +321,6 @@ void gpu_disable_scissor() {
 }
 
 void gpu_set_pipeline_internal(gpu_pipeline_t *pipeline) {
-	for (int i = 0; i < GPU_MAX_TEXTURES; ++i) {
-		current_textures[i] = NULL;
-	}
-	if (pipeline->impl.pipeline == NULL) {
-		return;
-	}
-	current_pipeline = pipeline;
 	id<MTLRenderPipelineState> pipe = (__bridge id<MTLRenderPipelineState>)pipeline->impl.pipeline;
 	[command_encoder setRenderPipelineState:pipe];
 	id<MTLDepthStencilState> depth_state = (__bridge id<MTLDepthStencilState>)pipeline->impl.depth;
@@ -423,6 +416,11 @@ void gpu_pipeline_destroy_internal(gpu_pipeline_t *pipeline) {
 }
 
 void gpu_pipeline_compile(gpu_pipeline_t *pipeline) {
+	if (pipeline->vertex_shader->impl.length == 0 || pipeline->fragment_shader->impl.length == 0) {
+		// Shader compilation error
+		return;
+	}
+
 	id<MTLDevice> device = get_metal_device();
 	NSError *error = nil;
 	id<MTLLibrary> library = [device newLibraryWithSource:[[NSString alloc] initWithBytes:pipeline->vertex_shader->impl.source length:pipeline->vertex_shader->impl.length encoding:NSUTF8StringEncoding] options:nil error:&error];

+ 4 - 0
base/sources/iron.h

@@ -1018,6 +1018,10 @@ void gpu_create_shaders_from_kong(char *kong, char **vs, char **fs, int *vs_size
 	if (kong_error) {
 		console_info("Warning: Shader compilation failed");
 		free(tokens.t);
+		#if defined(__APPLE__)
+		*vs = "";
+		*fs = "";
+		#endif
 		return;
 	}
 	allocate_globals();

+ 4 - 1
base/sources/iron_gpu.c

@@ -73,6 +73,9 @@ void gpu_begin(gpu_texture_t **targets, int count, gpu_texture_t *depth_buffer,
 }
 
 void gpu_draw() {
+	if (current_pipeline == NULL || current_pipeline->impl.pipeline == NULL) {
+		return;
+	}
 	gpu_constant_buffer_unlock(&constant_buffer);
 	gpu_set_constant_buffer(&constant_buffer, constant_buffer_index * GPU_CONSTANT_BUFFER_SIZE, GPU_CONSTANT_BUFFER_SIZE);
 	gpu_draw_internal();
@@ -294,13 +297,13 @@ void gpu_texture_destroy(gpu_texture_t *texture) {
 }
 
 void gpu_set_pipeline(gpu_pipeline_t *pipeline) {
+	current_pipeline = pipeline;
 	for (int i = 0; i < GPU_MAX_TEXTURES; ++i) {
 		current_textures[i] = NULL;
 	}
 	if (pipeline->impl.pipeline == NULL) {
 		return;
 	}
-	current_pipeline = pipeline;
 	gpu_set_pipeline_internal(pipeline);
 }