Skip to content

Commit

Permalink
More general gradient matrix layout
Browse files Browse the repository at this point in the history
  • Loading branch information
poke1024 committed Aug 2, 2018
1 parent 5f42d98 commit 32c7d56
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/cpp/_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 21 additions & 3 deletions src/cpp/shader/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/lua/core/shader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 32c7d56

Please sign in to comment.