Skip to content

Commit

Permalink
fixes for premultiplied transparency on image quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp committed Nov 20, 2023
1 parent 2cd38b7 commit 783a333
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ extern const ShaderReplacementRule
extern const ShaderReplacementRule
TEXTURE_ORIGIN_LOWERLEFT; // sample textures with (0,0) in the lower left, which is the usual openGL rule
extern const ShaderReplacementRule TEXTURE_SET_TRANSPARENCY; // apply a transparency uniform to the texture
extern const ShaderReplacementRule TEXTURE_PREMULTIPLY_OUT; // premultiply color samples
extern const ShaderReplacementRule TEXTURE_SHADE_COLOR; // sample a color from a texture and use it for shading
extern const ShaderReplacementRule
TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED; // apply a transparency uniform to the texture
extern const ShaderReplacementRule TEXTURE_PREMULTIPLY_OUT; // premultiply color samples
extern const ShaderReplacementRule TEXTURE_SHADE_COLOR; // sample a color from a texture and use it for shading
extern const ShaderReplacementRule
TEXTURE_SHADE_COLORALPHA; // sample a coloralpha from a texture and use it for shading
extern const ShaderReplacementRule TEXTURE_PROPAGATE_VALUE; // sample a scalar from a texture and use it for shading
Expand Down
31 changes: 23 additions & 8 deletions src/color_image_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,17 @@ std::string ColorImageQuantity::niceName() { return name + " (color image)"; }
void ColorImageQuantity::prepareFullscreen() {

// Create the sourceProgram
fullscreenProgram = render::engine->requestShader(
"TEXTURE_DRAW_PLAIN", {getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY", "INVERSE_TONEMAP"},
render::ShaderReplacementDefaults::Process);
// clang-format off
fullscreenProgram =
render::engine->requestShader("TEXTURE_DRAW_PLAIN",
{
getImageOriginRule(imageOrigin),
getIsPremultiplied() ? "TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED" : "TEXTURE_SET_TRANSPARENCY",
"INVERSE_TONEMAP",
getIsPremultiplied() ? "" : "TEXTURE_PREMULTIPLY_OUT"
},
render::ShaderReplacementDefaults::Process);
// clang-format on
fullscreenProgram->setAttribute("a_position", render::engine->screenTrianglesCoords());
// TODO throughout polyscope we discard the shared pointer when adding textures/attributes to programs... should we
// just track the shared pointer?
Expand All @@ -53,11 +61,18 @@ void ColorImageQuantity::prepareFullscreen() {
void ColorImageQuantity::prepareBillboard() {

// Create the sourceProgram
billboardProgram = render::engine->requestShader("TEXTURE_DRAW_PLAIN",
{getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY",
"TEXTURE_BILLBOARD_FROM_UNIFORMS", "INVERSE_TONEMAP",
getIsPremultiplied() ? "" : "TEXTURE_PREMULTIPLY_OUT"},
render::ShaderReplacementDefaults::Process);
// clang-format off
billboardProgram =
render::engine->requestShader("TEXTURE_DRAW_PLAIN",
{
getImageOriginRule(imageOrigin),
getIsPremultiplied() ? "TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED" : "TEXTURE_SET_TRANSPARENCY",
"TEXTURE_BILLBOARD_FROM_UNIFORMS",
"INVERSE_TONEMAP",
getIsPremultiplied() ? "" : "TEXTURE_PREMULTIPLY_OUT"
},
render::ShaderReplacementDefaults::Process);
// clang-format on
billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords());
billboardProgram->setTextureFromBuffer("t_image", colors.getRenderTextureBuffer().get());
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/managed_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ template<> ManagedBufferMap<glm::uvec4>& ManagedBufferMap<glm::uve

std::string typeName(ManagedBufferType type) {
switch (type) {
// clang-format off
// clang-format off
case ManagedBufferType::Float : return "Float";
case ManagedBufferType::Double : return "Double";
case ManagedBufferType::Vec2 : return "Vec2";
Expand Down
1 change: 1 addition & 0 deletions src/render/mock_opengl/mock_gl_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ void MockGLEngine::populateDefaultShadersAndRules() {
registerShaderRule("TEXTURE_ORIGIN_UPPERLEFT", TEXTURE_ORIGIN_UPPERLEFT);
registerShaderRule("TEXTURE_ORIGIN_LOWERLEFT", TEXTURE_ORIGIN_LOWERLEFT);
registerShaderRule("TEXTURE_SET_TRANSPARENCY", TEXTURE_SET_TRANSPARENCY);
registerShaderRule("TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED", TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED);
registerShaderRule("TEXTURE_PREMULTIPLY_OUT", TEXTURE_PREMULTIPLY_OUT);
registerShaderRule("TEXTURE_SHADE_COLOR", TEXTURE_SHADE_COLOR);
registerShaderRule("TEXTURE_SHADE_COLORALPHA", TEXTURE_SHADE_COLORALPHA);
Expand Down
1 change: 1 addition & 0 deletions src/render/opengl/gl_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2730,6 +2730,7 @@ void GLEngine::populateDefaultShadersAndRules() {
registerShaderRule("TEXTURE_ORIGIN_UPPERLEFT", TEXTURE_ORIGIN_UPPERLEFT);
registerShaderRule("TEXTURE_ORIGIN_LOWERLEFT", TEXTURE_ORIGIN_LOWERLEFT);
registerShaderRule("TEXTURE_SET_TRANSPARENCY", TEXTURE_SET_TRANSPARENCY);
registerShaderRule("TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED", TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED);
registerShaderRule("TEXTURE_PREMULTIPLY_OUT", TEXTURE_PREMULTIPLY_OUT);
registerShaderRule("TEXTURE_SHADE_COLOR", TEXTURE_SHADE_COLOR);
registerShaderRule("TEXTURE_SHADE_COLORALPHA", TEXTURE_SHADE_COLORALPHA);
Expand Down
17 changes: 17 additions & 0 deletions src/render/opengl/shaders/texture_draw_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,23 @@ const ShaderReplacementRule TEXTURE_SET_TRANSPARENCY(
/* textures */ {}
);

const ShaderReplacementRule TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED(
/* rule name */ "TEXTURE_SET_TRANSPARENCY_PREMULTIPLIED",
{ /* replacement sources */
{"FRAG_DECLARATIONS", R"(
uniform float u_transparency;
)" },
{"TEXTURE_OUT_ADJUST", R"(
textureOut *= u_transparency;
)"}
},
/* uniforms */ {
{"u_transparency", RenderDataType::Float},
},
/* attributes */ {},
/* textures */ {}
);

// remember to use this rule _after_ TEXTURE_SET_TRANSPARENCY
const ShaderReplacementRule TEXTURE_PREMULTIPLY_OUT(
/* rule name */ "TEXTURE_PREMULTIPLY_OUT",
Expand Down
10 changes: 5 additions & 5 deletions src/scalar_image_quantity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ void ScalarImageQuantity::prepareFullscreen() {
void ScalarImageQuantity::prepareBillboard() {

// Create the sourceProgram
billboardProgram =
render::engine->requestShader("SCALAR_TEXTURE_COLORMAP",
this->addScalarRules({getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY",
"TEXTURE_BILLBOARD_FROM_UNIFORMS"}),
render::ShaderReplacementDefaults::Process);
billboardProgram = render::engine->requestShader(
"SCALAR_TEXTURE_COLORMAP",
this->addScalarRules({getImageOriginRule(imageOrigin), "TEXTURE_SET_TRANSPARENCY", "TEXTURE_PREMULTIPLY_OUT",
"TEXTURE_BILLBOARD_FROM_UNIFORMS"}),
render::ShaderReplacementDefaults::Process);
billboardProgram->setAttribute("a_position", render::engine->screenTrianglesCoords());
billboardProgram->setTextureFromBuffer("t_scalar", values.getRenderTextureBuffer().get());
billboardProgram->setTextureFromColormap("t_colormap", this->cMap.get());
Expand Down

0 comments on commit 783a333

Please sign in to comment.