diff --git a/plugins/visualizer/shaders/primaryShader.frag b/plugins/visualizer/shaders/primaryShader.frag index aacaf229d..3e8215c17 100755 --- a/plugins/visualizer/shaders/primaryShader.frag +++ b/plugins/visualizer/shaders/primaryShader.frag @@ -322,10 +322,13 @@ 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 @@ -333,7 +336,7 @@ void main(){ 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 @@ -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 diff --git a/plugins/visualizer/src/VisualizerCore.cpp b/plugins/visualizer/src/VisualizerCore.cpp index 14a043c70..c0213e62e 100644 --- a/plugins/visualizer/src/VisualizerCore.cpp +++ b/plugins/visualizer/src/VisualizerCore.cpp @@ -1289,6 +1289,10 @@ std::vector Visualizer::readOffscreenPixels() const { try { // Read pixels from the color attachment std::vector 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(Wframebuffer), static_cast(Hframebuffer), GL_RGB, GL_UNSIGNED_BYTE, pixel_data.data()); error = glGetError(); diff --git a/plugins/visualizer/tests/selfTest.cpp b/plugins/visualizer/tests/selfTest.cpp index 238615d6f..f81db3514 100644 --- a/plugins/visualizer/tests/selfTest.cpp +++ b/plugins/visualizer/tests/selfTest.cpp @@ -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 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 @@ -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(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 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); +}