Created
September 3, 2020 23:39
-
-
Save 1bardesign/3a14bb7e2b64750809798dbf57132774 to your computer and use it in GitHub Desktop.
try to send a uniform to a shader without getting blocking errors when it's been optimised away
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
try to send a uniform to a shader | |
prints a warning once for the lifetime of the shader for missing uniforms | |
otherwise succeeds or fails silently | |
]] | |
do | |
local _warned = setmetatable({}, {__mode = "k"}) | |
local function _warn(s, u) | |
local already_warned = _warned[s] | |
if not already_warned then | |
_warned[s] = {} | |
return _warn(s, u) | |
end | |
if already_warned[u] then | |
return | |
end | |
already_warned[u] = true | |
print( | |
debug.traceback( | |
("warning: attempted to send uniform '%s'") | |
:format(u), | |
2 | |
) | |
) | |
end | |
function shader_try_send(shader, uniform, ...) | |
if shader:hasUniform(uniform) then | |
return shader:send(uniform, ...) | |
end | |
return _warn(shader, uniform) | |
end | |
end | |
--[[ | |
example; | |
one uniform is used, one is not | |
]] | |
local shader = love.graphics.newShader([[ | |
uniform float is_used; | |
uniform float not_used; | |
vec4 effect(vec4 c, Image t, vec2 uv, vec2 px) { | |
return vec4(is_used); | |
} | |
]]) | |
--[[ | |
sending both in a loop results in just one print | |
]] | |
for i = 1, 100 do | |
shader_try_send(shader, "is_used", 1.0) | |
shader_try_send(shader, "not_used", 1.0) | |
end | |
--[[ | |
example output: | |
warning: attempted to send uniform 'not_used' | |
stack traceback: | |
main.lua:55: in main chunk | |
[C]: in function 'require' | |
[string "boot.lua"]:570: in function <[string "boot.lua"]:380> | |
[C]: in function 'xpcall' | |
[string "boot.lua"]:787: in function <[string "boot.lua"]:780> | |
[C]: in function 'xpcall' | |
[string "boot.lua"]:802: in function <[string "boot.lua"]:769> | |
]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment