[wip, win32, canvas] porting new canvas renderer to opengl

This commit is contained in:
martinfouilleul 2023-06-27 18:53:09 +02:00
parent 21aa1bef68
commit b797f187dc
13 changed files with 722 additions and 3715 deletions

View File

@ -1,7 +1,10 @@
@echo off
setlocal EnableDelayedExpansion
if not exist bin mkdir bin
set glsl_shaders=src\glsl_shaders\common.glsl src\glsl_shaders\blit_vertex.glsl src\glsl_shaders\blit_fragment.glsl src\glsl_shaders\clear_counters.glsl src\glsl_shaders\tile.glsl src\glsl_shaders\sort.glsl src\glsl_shaders\draw.glsl
set glsl_shaders=src\glsl_shaders\common.glsl src\glsl_shaders\blit_vertex.glsl src\glsl_shaders\blit_fragment.glsl src\glsl_shaders\path_setup.glsl src\glsl_shaders\segment_setup.glsl src\glsl_shaders\backprop.glsl src\glsl_shaders\merge.glsl src\glsl_shaders\raster.glsl
call python3 scripts\embed_text.py %glsl_shaders% --prefix=glsl_ --output src\glsl_shaders.h

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,20 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) restrict readonly buffer pathQueueBufferSSBO
{
mg_gl_path_queue elements[];
} pathQueueBuffer;
layout(binding = 1) restrict writeonly buffer tileQueueBufferSSBO
{
mg_gl_tile_queue elements[];
} tileQueueBuffer;
void main()
{
}

View File

@ -1,15 +0,0 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) coherent restrict writeonly buffer tileCounterBufferSSBO {
uint elements[];
} tileCounterBuffer ;
void main()
{
uint tileIndex = gl_WorkGroupID.x;
tileCounterBuffer.elements[tileIndex] = 0u;
}

View File

@ -1,14 +1,74 @@
layout(std430) buffer;
struct vertex {
vec4 cubic;
vec2 pos;
int shapeIndex;
// command
#define MG_GL_FILL 0
#define MG_GL_STROKE 1
// element kind
#define MG_GL_LINE 1
#define MG_GL_QUADRATIC 2
#define MG_GL_CUBIC 3
// curve config
#define MG_GL_BL 1 /* curve on bottom left */
#define MG_GL_BR 2 /* curve on bottom right */
#define MG_GL_TL 3 /* curve on top left */
#define MG_GL_TR 4 /* curve on top right */
// Operations
#define MG_GL_OP_START 0
#define MG_GL_OP_SEGMENT 1
struct mg_gl_path
{
int cmd;
float uvTransform[9];
vec4 color;
vec4 box;
vec4 clip;
};
struct shape {
vec4 color;
vec4 clip;
float uvTransform[6];
struct mg_gl_path_elt
{
int pathIndex;
int localEltIndex;
int kind;
vec2 p[4];
};
struct mg_gl_segment
{
int kind;
int pathIndex;
int config; //TODO pack these
int windingIncrement;
vec4 box;
float hullMatrix[9];
float implicitMatrix[9];
float sign;
vec2 hullVertex;
int debugID;
};
struct mg_gl_path_queue
{
vec4 area;
int tileQueues;
};
struct mg_gl_tile_op
{
int kind;
int index;
int next;
bool crossRight;
int windingOffset;
};
struct mg_gl_tile_queue
{
int windingOffset;
int first;
int last;
};

View File

@ -1,242 +0,0 @@
#extension GL_ARB_gpu_shader_int64 : require
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
precision mediump float;
//precision mediump image2D;
layout(binding = 0) restrict readonly buffer vertexBufferSSBO {
vertex elements[];
} vertexBuffer ;
layout(binding = 1) restrict readonly buffer shapeBufferSSBO {
shape elements[];
} shapeBuffer ;
layout(binding = 2) restrict readonly buffer indexBufferSSBO {
uint elements[];
} indexBuffer ;
layout(binding = 3) restrict readonly buffer tileCounterBufferSSBO {
uint elements[];
} tileCounterBuffer ;
layout(binding = 4) restrict readonly buffer tileArrayBufferSSBO {
uint elements[];
} tileArrayBuffer ;
layout(location = 0) uniform uint indexCount;
layout(location = 1) uniform uvec2 tileCount;
layout(location = 2) uniform uint tileSize;
layout(location = 3) uniform uint tileArraySize;
layout(location = 4) uniform vec2 scaling;
layout(location = 5) uniform uint useTexture;
layout(rgba8, binding = 0) uniform restrict writeonly image2D outTexture;
layout(binding = 1) uniform sampler2D srcTexture;
bool is_top_left(ivec2 a, ivec2 b)
{
return( (a.y == b.y && b.x < a.x)
||(b.y < a.y));
}
//////////////////////////////////////////////////////////////////////////////
//TODO: we should do these computations on 64bits, because otherwise
// we might overflow for values > 2048.
// Unfortunately this is costly.
// Another way is to precompute triangle edges (b - a) in full precision
// once to avoid doing it all the time...
//////////////////////////////////////////////////////////////////////////////
int orient2d(ivec2 a, ivec2 b, ivec2 p)
{
return((b.x-a.x)*(p.y-a.y) - (b.y-a.y)*(p.x-a.x));
}
int is_clockwise(ivec2 p0, ivec2 p1, ivec2 p2)
{
return((p1 - p0).x*(p2 - p0).y - (p1 - p0).y*(p2 - p0).x);
}
void main()
{
ivec2 pixelCoord = ivec2(gl_WorkGroupID.xy*uvec2(16, 16) + gl_LocalInvocationID.xy);
uvec2 tileCoord = uvec2(pixelCoord) / tileSize;
uint tileIndex = tileCoord.y * tileCount.x + tileCoord.x;
uint tileCounter = min(tileCounterBuffer.elements[tileIndex], tileArraySize);
const float subPixelFactor = 16.;
ivec2 centerPoint = ivec2((vec2(pixelCoord) + vec2(0.5, 0.5)) * subPixelFactor);
//*
const int sampleCount = 8;
ivec2 samplePoints[sampleCount] = ivec2[sampleCount](centerPoint + ivec2(1, 3),
centerPoint + ivec2(-1, -3),
centerPoint + ivec2(5, -1),
centerPoint + ivec2(-3, 5),
centerPoint + ivec2(-5, -5),
centerPoint + ivec2(-7, 1),
centerPoint + ivec2(3, -7),
centerPoint + ivec2(7, 7));
/*/
const int sampleCount = 4;
ivec2 samplePoints[sampleCount] = ivec2[sampleCount](centerPoint + ivec2(-2, 6),
centerPoint + ivec2(6, 2),
centerPoint + ivec2(-6, -2),
centerPoint + ivec2(2, -6));
//*/
//DEBUG
/*
{
vec4 fragColor = vec4(0);
if( pixelCoord.x % 16 == 0
||pixelCoord.y % 16 == 0)
{
fragColor = vec4(0, 0, 0, 1);
}
else if(tileCounterBuffer.elements[tileIndex] == 0xffffu)
{
fragColor = vec4(1, 0, 1, 1);
}
else if(tileCounter != 0u)
{
fragColor = vec4(0, 1, 0, 1);
}
else
{
fragColor = vec4(1, 0, 0, 1);
}
imageStore(outTexture, pixelCoord, fragColor);
return;
}
//*/
//----
vec4 sampleColor[sampleCount];
vec4 currentColor[sampleCount];
int currentShapeIndex[sampleCount];
int flipCount[sampleCount];
for(int i=0; i<sampleCount; i++)
{
currentShapeIndex[i] = -1;
flipCount[i] = 0;
sampleColor[i] = vec4(0, 0, 0, 0);
currentColor[i] = vec4(0, 0, 0, 0);
}
for(uint tileArrayIndex=0u; tileArrayIndex < tileCounter; tileArrayIndex++)
{
uint triangleIndex = tileArrayBuffer.elements[tileArraySize * tileIndex + tileArrayIndex];
uint i0 = indexBuffer.elements[triangleIndex];
uint i1 = indexBuffer.elements[triangleIndex+1u];
uint i2 = indexBuffer.elements[triangleIndex+2u];
ivec2 p0 = ivec2((vertexBuffer.elements[i0].pos * scaling) * subPixelFactor);
ivec2 p1 = ivec2((vertexBuffer.elements[i1].pos * scaling) * subPixelFactor);
ivec2 p2 = ivec2((vertexBuffer.elements[i2].pos * scaling) * subPixelFactor);
int shapeIndex = vertexBuffer.elements[i0].shapeIndex;
vec4 color = shapeBuffer.elements[shapeIndex].color;
color.rgb *= color.a;
ivec4 clip = ivec4(round((shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling) + vec4(0.5, 0.5, 0.5, 0.5)) * subPixelFactor));
mat3 uvTransform = mat3(shapeBuffer.elements[shapeIndex].uvTransform[0],
shapeBuffer.elements[shapeIndex].uvTransform[3],
0.,
shapeBuffer.elements[shapeIndex].uvTransform[1],
shapeBuffer.elements[shapeIndex].uvTransform[4],
0.,
shapeBuffer.elements[shapeIndex].uvTransform[2],
shapeBuffer.elements[shapeIndex].uvTransform[5],
1.);
//NOTE(martin): reorder triangle counter-clockwise and compute bias for each edge
int cw = is_clockwise(p0, p1, p2);
if(cw < 0)
{
uint tmpIndex = i1;
i1 = i2;
i2 = tmpIndex;
ivec2 tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;
}
vec4 cubic0 = vertexBuffer.elements[i0].cubic;
vec4 cubic1 = vertexBuffer.elements[i1].cubic;
vec4 cubic2 = vertexBuffer.elements[i2].cubic;
int bias0 = is_top_left(p1, p2) ? 0 : -1;
int bias1 = is_top_left(p2, p0) ? 0 : -1;
int bias2 = is_top_left(p0, p1) ? 0 : -1;
for(int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
{
ivec2 samplePoint = samplePoints[sampleIndex];
if( samplePoint.x < clip.x
|| samplePoint.x > clip.z
|| samplePoint.y < clip.y
|| samplePoint.y > clip.w)
{
continue;
}
int w0 = orient2d(p1, p2, samplePoint);
int w1 = orient2d(p2, p0, samplePoint);
int w2 = orient2d(p0, p1, samplePoint);
if((w0+bias0) >= 0 && (w1+bias1) >= 0 && (w2+bias2) >= 0)
{
vec4 cubic = (cubic0*float(w0) + cubic1*float(w1) + cubic2*float(w2))/(float(w0)+float(w1)+float(w2));
float eps = 0.0001;
if(cubic.w*(cubic.x*cubic.x*cubic.x - cubic.y*cubic.z) <= eps)
{
if(shapeIndex == currentShapeIndex[sampleIndex])
{
flipCount[sampleIndex]++;
}
else
{
if((flipCount[sampleIndex] & 0x01) != 0)
{
sampleColor[sampleIndex] = currentColor[sampleIndex];
}
vec4 nextColor = color;
if(useTexture)
{
vec3 sampleFP = vec3(vec2(samplePoint).xy/(subPixelFactor*2.), 1);
vec2 uv = (uvTransform * sampleFP).xy;
vec4 texColor = texture(srcTexture, uv);
texColor.rgb *= texColor.a;
nextColor *= texColor;
}
currentColor[sampleIndex] = sampleColor[sampleIndex]*(1.-nextColor.a) + nextColor;
currentShapeIndex[sampleIndex] = shapeIndex;
flipCount[sampleIndex] = 1;
}
}
}
}
}
vec4 pixelColor = vec4(0);
for(int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
{
if((flipCount[sampleIndex] & 0x01) != 0)
{
sampleColor[sampleIndex] = currentColor[sampleIndex];
}
pixelColor += sampleColor[sampleIndex];
}
imageStore(outTexture, pixelCoord, pixelColor/float(sampleCount));
}

View File

@ -0,0 +1,41 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) restrict readonly buffer pathBufferSSBO
{
mg_gl_path elements[];
} pathBuffer;
layout(binding = 1) restrict readonly buffer pathQueueBufferSSBO
{
mg_gl_path_queue elements[];
} pathQueueBuffer;
layout(binding = 2) restrict readonly buffer tileQueueBufferSSBO
{
mg_gl_tile_queue elements[];
} tileQueueBuffer;
layout(binding = 3) restrict readonly buffer tileOpBufferSSBO
{
mg_gl_tile_op elements[];
} tileOpBuffer;
layout(binding = 4) restrict readonly buffer tileOpCountBufferSSBO
{
int elements[];
} tileOpCountBuffer;
layout(binding = 5) restrict readonly buffer screenTilesBufferSSBO
{
mg_gl_tile_queue elements[];
} screenTilesBuffer;
void main()
{
}

View File

@ -0,0 +1,30 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) restrict readonly buffer pathBufferSSBO
{
mg_gl_path elements[];
} pathBuffer;
layout(binding = 1) restrict writeonly buffer pathQueueBufferSSBO
{
mg_gl_path_queue elements[];
} pathQueueBuffer;
layout(binding = 2) restrict writeonly buffer tileQueueBufferSSBO
{
mg_gl_tile_queue elements[];
} tileQueueBuffer;
layout(binding = 3) restrict writeonly buffer tileQueueCountBufferSSBO
{
int elements[];
} tileQueueCountBuffer;
void main()
{
}

View File

@ -0,0 +1,31 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) restrict readonly buffer screenTilesBufferSSBO
{
mg_gl_tile_queue elements[];
} screenTilesBuffer;
layout(binding = 1) restrict readonly buffer tileOpBufferSSBO
{
mg_gl_tile_op elements[];
} tileOpBuffer;
layout(binding = 2) restrict readonly buffer pathBufferSSBO
{
mg_gl_path elements[];
} pathBuffer;
layout(binding = 3) restrict readonly buffer segmentBufferSSBO
{
mg_gl_segment elements[];
} segmentBuffer;
void main()
{
}

View File

@ -0,0 +1,46 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(std430) buffer;
layout(binding = 0) restrict readonly buffer elementBufferSSBO
{
mg_gl_path_elt elements[];
} elementBuffer;
layout(binding = 1) restrict buffer segmentCountBufferSSBO
{
int elements[];
} segmentCountBuffer;
layout(binding = 2) restrict writeonly buffer segmentBufferSSBO
{
mg_gl_segment elements[];
} segmentBuffer;
layout(binding = 3) restrict readonly buffer pathQueueBufferSSBO
{
mg_gl_path_queue elements[];
} pathQueueBuffer;
layout(binding = 4) restrict readonly buffer tileQueueBufferSSBO
{
mg_gl_tile_queue elements[];
} tileQueueBuffer;
layout(binding = 5) restrict readonly buffer tileOpBufferSSBO
{
mg_gl_tile_op elements[];
} tileOpBuffer;
layout(binding = 6) restrict readonly buffer tileOpCountBufferSSBO
{
int elements[];
} tileOpCountBuffer;
void main()
{
}

View File

@ -1,61 +0,0 @@
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(binding = 0) restrict readonly buffer vertexBufferSSBO {
vertex elements[];
} vertexBuffer ;
layout(binding = 1) restrict readonly buffer shapeBufferSSBO {
shape elements[];
} shapeBuffer ;
layout(binding = 2) restrict readonly buffer indexBufferSSBO {
uint elements[];
} indexBuffer ;
layout(binding = 3) coherent readonly restrict buffer tileCounterBufferSSBO {
uint elements[];
} tileCounterBuffer ;
layout(binding = 4) coherent restrict buffer tileArrayBufferSSBO {
uint elements[];
} tileArrayBuffer ;
layout(location = 0) uniform uint indexCount;
layout(location = 1) uniform uvec2 tileCount;
layout(location = 2) uniform uint tileSize;
layout(location = 3) uniform uint tileArraySize;
int get_shape_index(uint tileArrayOffset, uint tileArrayIndex)
{
uint triangleIndex = tileArrayBuffer.elements[tileArrayOffset + tileArrayIndex];
uint i0 = indexBuffer.elements[triangleIndex];
int shapeIndex = vertexBuffer.elements[i0].shapeIndex;
return(shapeIndex);
}
void main()
{
uint tileIndex = gl_WorkGroupID.x;
uint tileArrayOffset = tileArraySize * tileIndex;
uint tileArrayCount = min(tileCounterBuffer.elements[tileIndex], tileArraySize);
for(uint tileArrayIndex=1u; tileArrayIndex < tileArrayCount; tileArrayIndex++)
{
for(uint sortIndex = tileArrayIndex; sortIndex > 0u; sortIndex--)
{
int shapeIndex = get_shape_index(tileArrayOffset, sortIndex);
int prevShapeIndex = get_shape_index(tileArrayOffset, sortIndex-1u);
if(shapeIndex >= prevShapeIndex)
{
break;
}
uint tmp = tileArrayBuffer.elements[tileArrayOffset + sortIndex];
tileArrayBuffer.elements[tileArrayOffset + sortIndex] = tileArrayBuffer.elements[tileArrayOffset + sortIndex - 1u];
tileArrayBuffer.elements[tileArrayOffset + sortIndex - 1u] = tmp;
}
}
}

View File

@ -1,77 +0,0 @@
layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
precision mediump float;
layout(binding = 0) restrict readonly buffer vertexBufferSSBO {
vertex elements[];
} vertexBuffer ;
layout(binding = 1) restrict readonly buffer shapeBufferSSBO {
shape elements[];
} shapeBuffer ;
layout(binding = 2) restrict readonly buffer indexBufferSSBO {
uint elements[];
} indexBuffer ;
layout(binding = 3) coherent restrict buffer tileCounterBufferSSBO {
uint elements[];
} tileCounterBuffer ;
layout(binding = 4) coherent restrict writeonly buffer tileArrayBufferSSBO {
uint elements[];
} tileArrayBuffer ;
layout(location = 0) uniform uint indexCount;
layout(location = 1) uniform uvec2 tileCount;
layout(location = 2) uniform uint tileSize;
layout(location = 3) uniform uint tileArraySize;
layout(location = 4) uniform vec2 scaling;
void main()
{
uint triangleIndex = (gl_WorkGroupID.x*gl_WorkGroupSize.x + gl_LocalInvocationIndex) * 3u;
if(triangleIndex >= indexCount)
{
return;
}
uint i0 = indexBuffer.elements[triangleIndex];
uint i1 = indexBuffer.elements[triangleIndex+1u];
uint i2 = indexBuffer.elements[triangleIndex+2u];
vec2 p0 = vertexBuffer.elements[i0].pos * scaling;
vec2 p1 = vertexBuffer.elements[i1].pos * scaling;
vec2 p2 = vertexBuffer.elements[i2].pos * scaling;
int shapeIndex = vertexBuffer.elements[i0].shapeIndex;
vec4 clip = shapeBuffer.elements[shapeIndex].clip * vec4(scaling, scaling);
vec4 fbox = vec4(max(min(min(p0.x, p1.x), p2.x), clip.x),
max(min(min(p0.y, p1.y), p2.y), clip.y),
min(max(max(p0.x, p1.x), p2.x), clip.z),
min(max(max(p0.y, p1.y), p2.y), clip.w));
ivec4 box = ivec4(floor(fbox))/int(tileSize);
//NOTE(martin): it's importat to do the computation with signed int, so that we can have negative xMax/yMax
// otherwise all triangles on the left or below the x/y axis are attributed to tiles on row/column 0.
int xMin = max(0, box.x);
int yMin = max(0, box.y);
int xMax = min(box.z, int(tileCount.x) - 1);
int yMax = min(box.w, int(tileCount.y) - 1);
for(int y = yMin; y <= yMax; y++)
{
for(int x = xMin ; x <= xMax; x++)
{
uint tileIndex = uint(y)*tileCount.x + uint(x);
uint tileCounter = atomicAdd(tileCounterBuffer.elements[tileIndex], 1u);
if(tileCounter < tileArraySize)
{
tileArrayBuffer.elements[tileArraySize*tileIndex + tileCounter] = triangleIndex;
}
}
}
}

File diff suppressed because it is too large Load Diff