Fix #34679: cycles image texture alpha fringes. New rule is now that color output
will not give straight RGB values if the alpha output is used, so that mixing with a transparent BSDF gives the correct result.
This commit is contained in:
@@ -48,6 +48,14 @@ color color_scene_linear_to_srgb(color c)
|
|||||||
color_scene_linear_to_srgb(c[2]));
|
color_scene_linear_to_srgb(c[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
color color_unpremultiply(color c, float alpha)
|
||||||
|
{
|
||||||
|
if(alpha != 1.0 && alpha != 0.0)
|
||||||
|
return c/alpha;
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
/* Color Operations */
|
/* Color Operations */
|
||||||
|
|
||||||
color rgb_to_hsv(color rgb)
|
color rgb_to_hsv(color rgb)
|
||||||
|
@@ -66,6 +66,9 @@ shader node_environment_texture(
|
|||||||
/* todo: use environment for better texture filtering of equirectangular */
|
/* todo: use environment for better texture filtering of equirectangular */
|
||||||
Color = (color)texture(filename, p[0], 1.0 - p[1], "wrap", "periodic", "alpha", Alpha);
|
Color = (color)texture(filename, p[0], 1.0 - p[1], "wrap", "periodic", "alpha", Alpha);
|
||||||
|
|
||||||
|
if (isconnected(Alpha))
|
||||||
|
Color = color_unpremultiply(Color, Alpha);
|
||||||
|
|
||||||
if (color_space == "sRGB")
|
if (color_space == "sRGB")
|
||||||
Color = color_srgb_to_scene_linear(Color);
|
Color = color_srgb_to_scene_linear(Color);
|
||||||
}
|
}
|
||||||
|
@@ -19,10 +19,13 @@
|
|||||||
#include "stdosl.h"
|
#include "stdosl.h"
|
||||||
#include "node_color.h"
|
#include "node_color.h"
|
||||||
|
|
||||||
color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha)
|
color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha, int use_alpha)
|
||||||
{
|
{
|
||||||
color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "alpha", Alpha);
|
color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "alpha", Alpha);
|
||||||
|
|
||||||
|
if (use_alpha)
|
||||||
|
rgb = color_unpremultiply(rgb, Alpha);
|
||||||
|
|
||||||
if (color_space == "sRGB")
|
if (color_space == "sRGB")
|
||||||
rgb = color_srgb_to_scene_linear(rgb);
|
rgb = color_srgb_to_scene_linear(rgb);
|
||||||
|
|
||||||
@@ -44,9 +47,11 @@ shader node_image_texture(
|
|||||||
|
|
||||||
if (use_mapping)
|
if (use_mapping)
|
||||||
p = transform(mapping, p);
|
p = transform(mapping, p);
|
||||||
|
|
||||||
|
int use_alpha = isconnected(Alpha);
|
||||||
|
|
||||||
if (projection == "Flat") {
|
if (projection == "Flat") {
|
||||||
Color = image_texture_lookup(filename, color_space, p[0], p[1], Alpha);
|
Color = image_texture_lookup(filename, color_space, p[0], p[1], Alpha, use_alpha);
|
||||||
}
|
}
|
||||||
else if (projection == "Box") {
|
else if (projection == "Box") {
|
||||||
/* object space normal */
|
/* object space normal */
|
||||||
@@ -111,15 +116,15 @@ shader node_image_texture(
|
|||||||
float tmp_alpha;
|
float tmp_alpha;
|
||||||
|
|
||||||
if (weight[0] > 0.0) {
|
if (weight[0] > 0.0) {
|
||||||
Color += weight[0] * image_texture_lookup(filename, color_space, p[1], p[2], tmp_alpha);
|
Color += weight[0] * image_texture_lookup(filename, color_space, p[1], p[2], tmp_alpha, use_alpha);
|
||||||
Alpha += weight[0] * tmp_alpha;
|
Alpha += weight[0] * tmp_alpha;
|
||||||
}
|
}
|
||||||
if (weight[1] > 0.0) {
|
if (weight[1] > 0.0) {
|
||||||
Color += weight[1] * image_texture_lookup(filename, color_space, p[0], p[2], tmp_alpha);
|
Color += weight[1] * image_texture_lookup(filename, color_space, p[0], p[2], tmp_alpha, use_alpha);
|
||||||
Alpha += weight[1] * tmp_alpha;
|
Alpha += weight[1] * tmp_alpha;
|
||||||
}
|
}
|
||||||
if (weight[2] > 0.0) {
|
if (weight[2] > 0.0) {
|
||||||
Color += weight[2] * image_texture_lookup(filename, color_space, p[1], p[0], tmp_alpha);
|
Color += weight[2] * image_texture_lookup(filename, color_space, p[1], p[0], tmp_alpha, use_alpha);
|
||||||
Alpha += weight[2] * tmp_alpha;
|
Alpha += weight[2] * tmp_alpha;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -50,7 +50,7 @@ __device_inline float svm_image_texture_frac(float x, int *ix)
|
|||||||
return x - (float)i;
|
return x - (float)i;
|
||||||
}
|
}
|
||||||
|
|
||||||
__device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb)
|
__device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
|
||||||
{
|
{
|
||||||
/* first slots are used by float textures, which are not supported here */
|
/* first slots are used by float textures, which are not supported here */
|
||||||
if(id < TEX_NUM_FLOAT_IMAGES)
|
if(id < TEX_NUM_FLOAT_IMAGES)
|
||||||
@@ -88,6 +88,13 @@ __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, u
|
|||||||
r += ty*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + niy*width);
|
r += ty*(1.0f - tx)*svm_image_texture_read(kg, offset + ix + niy*width);
|
||||||
r += ty*tx*svm_image_texture_read(kg, offset + nix + niy*width);
|
r += ty*tx*svm_image_texture_read(kg, offset + nix + niy*width);
|
||||||
|
|
||||||
|
if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
|
||||||
|
float invw = 1.0f/r.w;
|
||||||
|
r.x *= invw;
|
||||||
|
r.y *= invw;
|
||||||
|
r.z *= invw;
|
||||||
|
}
|
||||||
|
|
||||||
if(srgb) {
|
if(srgb) {
|
||||||
r.x = color_srgb_to_scene_linear(r.x);
|
r.x = color_srgb_to_scene_linear(r.x);
|
||||||
r.y = color_srgb_to_scene_linear(r.y);
|
r.y = color_srgb_to_scene_linear(r.y);
|
||||||
@@ -99,7 +106,7 @@ __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, u
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
__device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb)
|
__device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, uint srgb, uint use_alpha)
|
||||||
{
|
{
|
||||||
float4 r;
|
float4 r;
|
||||||
|
|
||||||
@@ -222,6 +229,13 @@ __device float4 svm_image_texture(KernelGlobals *kg, int id, float x, float y, u
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if(use_alpha && r.w != 1.0f && r.w != 0.0f) {
|
||||||
|
float invw = 1.0f/r.w;
|
||||||
|
r.x *= invw;
|
||||||
|
r.y *= invw;
|
||||||
|
r.z *= invw;
|
||||||
|
}
|
||||||
|
|
||||||
if(srgb) {
|
if(srgb) {
|
||||||
r.x = color_srgb_to_scene_linear(r.x);
|
r.x = color_srgb_to_scene_linear(r.x);
|
||||||
r.y = color_srgb_to_scene_linear(r.y);
|
r.y = color_srgb_to_scene_linear(r.y);
|
||||||
@@ -241,7 +255,8 @@ __device void svm_node_tex_image(KernelGlobals *kg, ShaderData *sd, float *stack
|
|||||||
decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
|
decode_node_uchar4(node.z, &co_offset, &out_offset, &alpha_offset, &srgb);
|
||||||
|
|
||||||
float3 co = stack_load_float3(stack, co_offset);
|
float3 co = stack_load_float3(stack, co_offset);
|
||||||
float4 f = svm_image_texture(kg, id, co.x, co.y, srgb);
|
uint use_alpha = stack_valid(alpha_offset);
|
||||||
|
float4 f = svm_image_texture(kg, id, co.x, co.y, srgb, use_alpha);
|
||||||
|
|
||||||
if(stack_valid(out_offset))
|
if(stack_valid(out_offset))
|
||||||
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
||||||
@@ -322,13 +337,14 @@ __device void svm_node_tex_image_box(KernelGlobals *kg, ShaderData *sd, float *s
|
|||||||
uint id = node.y;
|
uint id = node.y;
|
||||||
|
|
||||||
float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
float4 f = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
uint use_alpha = stack_valid(alpha_offset);
|
||||||
|
|
||||||
if(weight.x > 0.0f)
|
if(weight.x > 0.0f)
|
||||||
f += weight.x*svm_image_texture(kg, id, co.y, co.z, srgb);
|
f += weight.x*svm_image_texture(kg, id, co.y, co.z, srgb, use_alpha);
|
||||||
if(weight.y > 0.0f)
|
if(weight.y > 0.0f)
|
||||||
f += weight.y*svm_image_texture(kg, id, co.x, co.z, srgb);
|
f += weight.y*svm_image_texture(kg, id, co.x, co.z, srgb, use_alpha);
|
||||||
if(weight.z > 0.0f)
|
if(weight.z > 0.0f)
|
||||||
f += weight.z*svm_image_texture(kg, id, co.y, co.x, srgb);
|
f += weight.z*svm_image_texture(kg, id, co.y, co.x, srgb, use_alpha);
|
||||||
|
|
||||||
if(stack_valid(out_offset))
|
if(stack_valid(out_offset))
|
||||||
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
||||||
@@ -355,7 +371,8 @@ __device void svm_node_tex_environment(KernelGlobals *kg, ShaderData *sd, float
|
|||||||
else
|
else
|
||||||
uv = direction_to_mirrorball(co);
|
uv = direction_to_mirrorball(co);
|
||||||
|
|
||||||
float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb);
|
uint use_alpha = stack_valid(alpha_offset);
|
||||||
|
float4 f = svm_image_texture(kg, id, uv.x, uv.y, srgb, use_alpha);
|
||||||
|
|
||||||
if(stack_valid(out_offset))
|
if(stack_valid(out_offset))
|
||||||
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
stack_store_float3(stack, out_offset, make_float3(f.x, f.y, f.z));
|
||||||
|
Reference in New Issue
Block a user