mirror of
https://github.com/love2d/love.git
synced 2026-08-15 07:41:11 +02:00
Converted code for Shader:send from Lua to C++
This commit is contained in:
@@ -1452,72 +1452,4 @@ void main() {
|
||||
if #lines == 1 then return message end
|
||||
return table_concat(lines, "\n")
|
||||
end
|
||||
|
||||
-- helper to transform a matrix from {{a,b,c}, {d,e,f}, ...} to
|
||||
-- {a, b, c, d, e, f, ...}
|
||||
local function flattenMatrices(mat, ...)
|
||||
if not mat then return end
|
||||
|
||||
local tonumber = tonumber
|
||||
|
||||
local ret,l = {}, 1
|
||||
ret.dimension = #mat
|
||||
|
||||
for i = 1,#mat do
|
||||
for k = 1,#mat[i] do
|
||||
ret[l], l = tonumber(mat[i][k]), l+1
|
||||
end
|
||||
end
|
||||
|
||||
return ret, flattenMatrices(...)
|
||||
end
|
||||
|
||||
-- automagic uniform setter
|
||||
local function shader_dispatch_send(self, name, value, ...)
|
||||
local valuetype = type(value)
|
||||
if valuetype == "number" or valuetype == "boolean" then -- scalar
|
||||
self:sendFloat(name, value, ...)
|
||||
elseif valuetype == "userdata" and value:typeOf("Image") then
|
||||
self:sendImage(name, value)
|
||||
elseif valuetype == "userdata" and value:typeOf("Canvas") then
|
||||
self:sendCanvas(name, value)
|
||||
elseif valuetype == "table" then -- vector or matrix
|
||||
valuetype = type(value[1])
|
||||
if valuetype == "number" or valuetype == "boolean" then
|
||||
self:sendFloat(name, value, ...)
|
||||
elseif valuetype == "table" then
|
||||
self:sendMatrix(name, flattenMatrices(value, ...))
|
||||
else
|
||||
error("Cannot send value (unsupported type: {"..valuetype.."}).")
|
||||
end
|
||||
else
|
||||
if valuetype == "userdata" and value.type then valuetype = value.type end
|
||||
error("Cannot send value (unsupported type: "..valuetype..").")
|
||||
end
|
||||
end
|
||||
|
||||
local function shader_send_protected(self, name, value, ...)
|
||||
local success, err = pcall(shader_dispatch_send, self, name, value, ...)
|
||||
if not success then
|
||||
if err and type(err) == "string" then
|
||||
err = err:gsub("^(.-):(%d+): ", "")
|
||||
end
|
||||
error(err, 2)
|
||||
end
|
||||
end
|
||||
|
||||
local newShader = love.graphics.newShader
|
||||
function love.graphics.newShader(vertexcode, pixelcode)
|
||||
love.graphics.newShader = newShader
|
||||
|
||||
local success, shader = pcall(love.graphics.newShader, vertexcode, pixelcode)
|
||||
if success then
|
||||
local meta = getmetatable(shader)
|
||||
meta.send = shader_send_protected
|
||||
meta.sendBoolean = meta.sendFloat
|
||||
return shader
|
||||
else
|
||||
return error(shader, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user