Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions plugins/visualizer/shaders/primaryShader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,21 @@ void main(){
vec2 wrapped = fract(texcoord) * uv_scale;
vec3 texcoord3 = vec3(wrapped, float(textureID));

// Only 3D geometry (coordinateFlag==1) is decoded to linear light: the output stage at the end re-encodes to sRGB
// only for coordinateFlag==1, so decoding 2D window-normalized geometry here too left it in linear values and drew a
// texture or colour in screen coordinates too dark (sRGB 128 displayed as 55).
if( textureFlag==0 ){//Color by interpolating the colors at vertices
color = fragmentColor;
color.rgb = color.rgb*colorBoost;
if( linearPipeline==1 ){
if( linearPipeline==1 && coordinateFlag==1 ){
color.rgb = srgbToLinear(clamp(color.rgb,0.0,1.0));
}
}else if( textureFlag==1 ){//Color by texture map
color = texture(textureSampler, texcoord3);
if(color.a<0.5 ){
discard;
}
if( linearPipeline==1 ){
if( linearPipeline==1 && coordinateFlag==1 ){
color.rgb = srgbToLinear(color.rgb);
}
}else if( textureFlag==2 ){//Color by interpolating the colors at vertices, and set the transparency according to the red channel of the texture map given by textureSampler
Expand All @@ -342,7 +345,7 @@ void main(){
if( color.a<0.5 ){
discard;
}
if( linearPipeline==1 ){
if( linearPipeline==1 && coordinateFlag==1 ){
color.rgb = srgbToLinear(clamp(color.rgb,0.0,1.0));
}
}else if( textureFlag==4 ){//Color by texture map, blending the texture's alpha rather than testing it
Expand Down
4 changes: 4 additions & 0 deletions plugins/visualizer/src/VisualizerCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,10 @@ std::vector<helios::RGBcolor> Visualizer::readOffscreenPixels() const {
try {
// Read pixels from the color attachment
std::vector<unsigned char> pixel_data(data_size);
// pixel_data holds tightly packed rows. At the default pack alignment of 4, glReadPixels pads each GL_RGB row to a
// multiple of 4 bytes, so for a width that is not a multiple of 4 it writes past the end of the buffer and every
// row after the first is read skewed.
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, static_cast<GLsizei>(Wframebuffer), static_cast<GLsizei>(Hframebuffer), GL_RGB, GL_UNSIGNED_BYTE, pixel_data.data());

error = glGetError();
Expand Down
78 changes: 78 additions & 0 deletions plugins/visualizer/tests/selfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,47 @@ DOCTEST_TEST_CASE("Visualizer binds a real texture to every sampler the primary
DOCTEST_CHECK_MESSAGE(VisualizerTestHelper::getNormalMapMaterialTableTexture(visualizer) != 0, "no normal-map material table texture exists, so the normalMapMaterialTable sampler is bound to texture 0 for any scene built without a Context");
}

DOCTEST_TEST_CASE("Visualizer headless JPEG capture at a width that is not a multiple of 4") {
// readOffscreenPixels() read the offscreen framebuffer as GL_RGB into a tightly packed buffer without setting
// GL_PACK_ALIGNMENT, so at the default alignment of 4 each row came back padded to a multiple of 4 bytes. For a
// width of 381 (1143 bytes per row) that wrote one byte per row past the end of the buffer, corrupting the heap,
// and shifted every row by one more byte than the last, so a uniform background came back as rotating color bands.
const uint width = 381;
const uint height = 64;
const std::string filename = "test_headless_odd_width.jpeg";

{
Visualizer visualizer(width, height, 0, false, true);
visualizer.disableMessages();
visualizer.hideWatermark();
visualizer.hideNavigationGizmo();
visualizer.disableColorbar();
visualizer.setBackgroundColor(make_RGBcolor(0.f, 0.f, 1.f));
visualizer.plotUpdate(true);
DOCTEST_REQUIRE_NOTHROW(visualizer.printWindow(filename.c_str(), "jpeg"));
}

uint read_width = 0;
uint read_height = 0;
std::vector<RGBcolor> pixels;
helios::readJPEG(filename, read_width, read_height, pixels);
DOCTEST_REQUIRE(read_width == width);
DOCTEST_REQUIRE(read_height == height);

// JPEG compression leaves a flat field within a few levels of its color.
int off_color = 0;
for (const RGBcolor &pixel: pixels) {
if (pixel.r > 0.1f || pixel.g > 0.1f || pixel.b < 0.9f) {
off_color++;
}
}
DOCTEST_CHECK_MESSAGE(off_color == 0, off_color << " of " << pixels.size() << " pixels of a uniform blue background were read back as another color");

if (std::filesystem::exists(filename)) {
std::filesystem::remove(filename);
}
}

DOCTEST_TEST_CASE("Visualizer shadows render in headless mode") {
// REGRESSION TEST: Visualizer::initialize() created the shadow-map framebuffer and
// depth texture only under `if (!headless)`, so in headless mode framebufferID and
Expand Down Expand Up @@ -7738,3 +7779,40 @@ TEST_CASE("Visualizer reports normal maps on primitives with collinear texture c
std::filesystem::remove(tilt_u);
std::filesystem::remove(texture);
}

DOCTEST_TEST_CASE("Visualizer draws a window-normalized texture in its own sRGB values") {
// The fragment shader decoded every textured or colored fragment from sRGB to linear light when the linear
// pipeline was on (the default), but re-encoded only 3D geometry (coordinateFlag==1). A textured rectangle in
// window-normalized coordinates therefore came out in linear values: a uniform sRGB 128 texture was drawn as 55.
const std::string texture_file = "test_window_texture_gray128.jpeg";
const std::string capture_file = "test_window_texture_capture.jpeg";
const uint width = 200;
const uint height = 160;

helios::writeJPEG(texture_file, 64, 64, std::vector<RGBcolor>(64 * 64, make_RGBcolor(128.f / 255.f, 128.f / 255.f, 128.f / 255.f)));

{
Visualizer visualizer(width, height, 0, false, true);
visualizer.disableMessages();
visualizer.hideWatermark();
visualizer.hideNavigationGizmo();
visualizer.disableColorbar();
visualizer.setBackgroundColor(make_RGBcolor(1.f, 0.f, 0.f));
visualizer.addRectangleByCenter(make_vec3(0.5f, 0.5f, 0.f), make_vec2(0.8f, 0.8f), make_SphericalCoord(0, 0), texture_file.c_str(), Visualizer::COORDINATES_WINDOW_NORMALIZED);
visualizer.plotUpdate(true);
DOCTEST_REQUIRE_NOTHROW(visualizer.printWindow(capture_file.c_str(), "jpeg"));
}

uint read_width = 0;
uint read_height = 0;
std::vector<RGBcolor> pixels;
helios::readJPEG(capture_file, read_width, read_height, pixels);
DOCTEST_REQUIRE(read_width == width);
DOCTEST_REQUIRE(read_height == height);

const RGBcolor center = pixels.at((height / 2) * width + width / 2);
DOCTEST_CHECK_MESSAGE(std::fabs(center.g * 255.f - 128.f) <= 3.f, "a uniform sRGB 128 texture in window coordinates was drawn as " << center.g * 255.f);

std::filesystem::remove(texture_file);
std::filesystem::remove(capture_file);
}
Loading