Shader code:
Code: Select all
uniform ArrayImage arrayCanvas;
vec4 effect(vec4 color, Image tex, vec2 uv, vec2 fc) {
float layer = floor(Texel(tex, uv).r * 255.0 + .5);
return Texel(arrayCanvas, vec3(uv * 16.0, layer));
}
The image shows all 256 layers of an ArrayImage, and each layer is showing its index. The red background is the Canvas from which the layer index is read.
The layers are shown as expected, the layer indices have no discontinuities or rounding errors.
The code that generated the layer selection canvas:
Code: Select all
local layerSelect = lg.newCanvas(512, 512, { format = 'r8' })
lg.setCanvas(layerSelect)
for i = 0, 255 do
local col = i / 255
lg.setColor(col, col, col, 1.0)
local x, y = i % 16, math.floor(i / 16)
lg.rectangle('fill', x * 32, y * 32, 32, 32)
end
Code: Select all
for i = 0, 255 do
local col = love.math.linearToGamma(i / 255) -- this line is the relevant change
lg.setColor(col, col, col, 1.0)
local x, y = i % 16, math.floor(i / 16)
lg.rectangle('fill', x * 32, y * 32, 32, 32)
end
For example: layers 2 and 4 appear twice instead of 3 and 5. More severe errors with higher numbers. I tried rounding, floor, ceil, in the shader to no avail. I also tried unGammaCorrectColor in the shader, which gave me even weirder distribution of values in any case.
Disabling gamma correction while the index canvas is generated is not possible; it is enabled in conf.lua and can't be changed at runtime. A way to selectively disable gamma correction for some Canvases does not seem to exist. The index canvas is highly dynamic and can't be precomputed in my use case.
Any ideas how to solve this problem?