Browse Source

image-source: Use DrawNonlinearAlpha

Match previous OBS alpha behavior.

DrawNonlinearAlpha requires premultiplied alpha blend state.
jpark37 4 years ago
parent
commit
b911e9b0cc
1 changed files with 21 additions and 9 deletions
  1. 21 9
      plugins/image-source/image-source.c

+ 21 - 9
plugins/image-source/image-source.c

@@ -147,19 +147,31 @@ static void image_source_render(void *data, gs_effect_t *effect)
 	if (!context->if2.image.texture)
 		return;
 
-	const bool linear_srgb = gs_get_linear_srgb();
+	effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
+	gs_technique_t *tech =
+		gs_effect_get_technique(effect, "DrawNonlinearAlpha");
 
 	const bool previous = gs_framebuffer_srgb_enabled();
-	gs_enable_framebuffer_srgb(linear_srgb);
+	gs_enable_framebuffer_srgb(true);
+
+	gs_blend_state_push();
+	gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
 
 	gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
-	if (linear_srgb)
-		gs_effect_set_texture_srgb(param, context->if2.image.texture);
-	else
-		gs_effect_set_texture(param, context->if2.image.texture);
+	gs_effect_set_texture_srgb(param, context->if2.image.texture);
+
+	size_t passes = gs_technique_begin(tech);
+	for (size_t i = 0; i < passes; i++) {
+		gs_technique_begin_pass(tech, i);
+
+		gs_draw_sprite(context->if2.image.texture, 0,
+			       context->if2.image.cx, context->if2.image.cy);
+
+		gs_technique_end_pass(tech);
+	}
+	gs_technique_end(tech);
 
-	gs_draw_sprite(context->if2.image.texture, 0, context->if2.image.cx,
-		       context->if2.image.cy);
+	gs_blend_state_pop();
 
 	gs_enable_framebuffer_srgb(previous);
 }
@@ -298,7 +310,7 @@ static obs_missing_files_t *image_source_missingfiles(void *data)
 static struct obs_source_info image_source_info = {
 	.id = "image_source",
 	.type = OBS_SOURCE_TYPE_INPUT,
-	.output_flags = OBS_SOURCE_VIDEO,
+	.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
 	.get_name = image_source_get_name,
 	.create = image_source_create,
 	.destroy = image_source_destroy,