From 32c7d56e77a7e7ab2588b02f592d69e1b3049e24 Mon Sep 17 00:00:00 2001 From: Bernhard Liebl Date: Thu, 2 Aug 2018 20:42:39 +0200 Subject: [PATCH] More general gradient matrix layout --- src/cpp/_interface.h | 8 +++++++- src/cpp/shader/color.h | 24 +++++++++++++++++++++--- src/lua/core/shader.lua | 1 + src/lua/main.lua | 22 ++++++++++++---------- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/cpp/_interface.h b/src/cpp/_interface.h index 101c5f4..8e87287 100644 --- a/src/cpp/_interface.h +++ b/src/cpp/_interface.h @@ -161,9 +161,15 @@ typedef struct { int16_t numColors; } TovePaintColorAllocation; +typedef enum { + MATRIX_MAT3x3, + MATRIX_MAT3x4, +} ToveMatrixType; + typedef struct { int16_t numColors; // to be removed - ToveMatrix3x3 *matrix; + float *matrix; + ToveMatrixType matrixType; ToveVec4 *arguments; uint8_t *colorsTexture; int16_t colorsTextureRowBytes; diff --git a/src/cpp/shader/color.h b/src/cpp/shader/color.h index c72e0f0..a72a687 100644 --- a/src/cpp/shader/color.h +++ b/src/cpp/shader/color.h @@ -67,7 +67,7 @@ class AbstractPaintFeed { if (gradient.arguments) { if (gradient.matrix) { - float *m = &gradient.matrix[0][0]; + float *m = gradient.matrix; m[0] = 1; m[1] = 0; @@ -80,6 +80,12 @@ class AbstractPaintFeed { m[6] = 0; m[7] = 0; m[8] = 1; + + if (gradient.matrixType == MATRIX_MAT3x4) { + m[9] = 0; + m[10] = 0; + m[11] = 0; + } } if (gradient.arguments) { @@ -127,7 +133,7 @@ class AbstractPaintFeed { } assert(gradient.matrix != nullptr); - paint->getGradientMatrix(*gradient.matrix, scale); + paint->getGradientMatrix(*(ToveMatrix3x3*)gradient.matrix, scale); if (gradient.arguments) { float s = 0.5f / gradient.colorTextureHeight; @@ -193,7 +199,19 @@ class PaintFeedBase : public AbstractPaintFeed { } void bind(const ToveGradientData &data, int i) { - paintData.gradient.matrix = &data.matrix[i]; + int size; + switch (data.matrixType) { + case MATRIX_MAT3x3: + size = 3 * 3; + break; + case MATRIX_MAT3x4: + size = 3 * 4; + break; + default: + assert(false); + } + + paintData.gradient.matrix = data.matrix + i * size; paintData.gradient.arguments = &data.arguments[i]; paintData.gradient.colorsTexture = data.colorsTexture + 4 * i; paintData.gradient.colorsTextureRowBytes = data.colorsTextureRowBytes; diff --git a/src/lua/core/shader.lua b/src/lua/core/shader.lua index 7744bfc..f9c2614 100644 --- a/src/lua/core/shader.lua +++ b/src/lua/core/shader.lua @@ -286,6 +286,7 @@ local function newMeshFeedData(name, graphics, tess, usage, resolution) alloc.numPaints * ffi.sizeof("ToveVec4")) local gradientData = ffi.new("ToveGradientData") gradientData.matrix = matrixData:getPointer() + gradientData.matrixType = env.mat3.type gradientData.arguments = argumentsData:getPointer() gradientData.colorsTexture = imageData:getPointer() gradientData.colorsTextureRowBytes = imageData:getSize() / alloc.numColors diff --git a/src/lua/main.lua b/src/lua/main.lua index d65127b..6370956 100644 --- a/src/lua/main.lua +++ b/src/lua/main.lua @@ -71,10 +71,21 @@ tove.init = function(path) rgba16f = love.graphics.getCanvasFormats()["rgba16f"], mat3 = { glsl = "mat3", - size = ffi.sizeof("ToveMatrix3x3") + size = ffi.sizeof("ToveMatrix3x3"), + type = lib.MATRIX_MAT3x3 } } + -- work around crashing Parallels drivers with mat3 + do + local _, _, _, device = love.graphics.getRendererInfo() + if string.find(device, "Parallels") ~= nil then + env.mat3.glsl = "mat3x4" + env.mat3.size = ffi.sizeof("float[?]", 12) + env.mat3.type = lib.MATRIX_MAT3x4 + end + end + -- deepcopy: taken from http://lua-users.org/wiki/CopyTable function deepcopy(orig) local orig_type = type(orig) @@ -91,15 +102,6 @@ tove.init = function(path) return copy end - -- work around crashing Parallels drivers with mat3 - do - local _, _, _, device = love.graphics.getRendererInfo() - if string.find(device, "Parallels") ~= nil then - env.mat3.glsl = "mat3x4" - env.mat3.size = ffi.sizeof("float[?]", 12) - end - end - -- common attributes used by Command and Curve. local _attributes = { cp1x = 0,