Skip to content

Commit

Permalink
D3D: nanovg supports setting custom stencil
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Feb 20, 2024
1 parent cc9bf1e commit 23048cf
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 188 deletions.
49 changes: 39 additions & 10 deletions library/include/borealis/extern/nanovg/nanovg_d3d11.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ enum D3DNVGcallType {
D3DNVG_FILL,
D3DNVG_CONVEXFILL,
D3DNVG_STROKE,
D3DNVG_TRIANGLES
D3DNVG_TRIANGLES,
D3DNVG_CONVEXFILL_STENCIL,
D3DNVG_CONVEXFILL_STENCIL_CLEAR,
};

struct D3DNVGcall {
Expand Down Expand Up @@ -880,21 +882,17 @@ static int D3Dnvg__convertPaint(struct D3DNVGcontext* D3D, struct D3DNVGfragUnif
}
frag->type = NSVG_SHADER_FILLIMG;

#if NANOVG_GL_USE_UNIFORMBUFFER
if (tex->type == NVG_TEXTURE_RGBA)
{
frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0 : 1;
if (scissor->stencilFlag)
frag->texType = 3;
else
frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0 : 1;
}
else
{
frag->texType = 2;
}
#else
if (tex->type == NVG_TEXTURE_RGBA)
frag->texType = (tex->flags & NVG_IMAGE_PREMULTIPLIED) ? 0.0f : 1.0f;
else
frag->texType = 2.0f;
#endif
}
else
{
Expand Down Expand Up @@ -1044,6 +1042,28 @@ static void D3Dnvg__convexFill(struct D3DNVGcontext* D3D, struct D3DNVGcall* cal
}
}

static void D3Dnvg__convexFillStencil(struct D3DNVGcontext* D3D, struct D3DNVGcall* call)
{
D3D_API_2(D3D->pDeviceContext, OMSetDepthStencilState, D3D->pDepthStencilDrawShapes, 0);
D3D_API_3(D3D->pDeviceContext, OMSetBlendState, D3D->pBSNoWrite, NULL, 0xFFFFFFFF);

D3Dnvg__convexFill(D3D, call);

D3D_API_3(D3D->pDeviceContext, OMSetBlendState, D3D->pBSBlend, NULL, 0xFFFFFFFF);
D3D_API_2(D3D->pDeviceContext, OMSetDepthStencilState, D3D->pDepthStencilDrawAA, 1);
}

static void D3Dnvg__convexFillStencilClear(struct D3DNVGcontext* D3D, struct D3DNVGcall* call)
{
D3D_API_2(D3D->pDeviceContext, OMSetDepthStencilState, D3D->pDepthStencilFill, 0);
D3D_API_3(D3D->pDeviceContext, OMSetBlendState, D3D->pBSNoWrite, NULL, 0xFFFFFFFF);

D3Dnvg__convexFill(D3D, call);

D3D_API_3(D3D->pDeviceContext, OMSetBlendState, D3D->pBSBlend, NULL, 0xFFFFFFFF);
D3D_API_2(D3D->pDeviceContext, OMSetDepthStencilState, D3D->pDepthStencilDefault, 0);
}

static void D3Dnvg__stroke(struct D3DNVGcontext* D3D, struct D3DNVGcall* call)
{
struct D3DNVGpath* paths = &D3D->paths[call->pathOffset];
Expand Down Expand Up @@ -1151,6 +1171,10 @@ static void D3Dnvg__renderFlush(void* uptr)
D3Dnvg__stroke(D3D, call);
else if (call->type == D3DNVG_TRIANGLES)
D3Dnvg__triangles(D3D, call);
else if (call->type == D3DNVG_CONVEXFILL_STENCIL)
D3Dnvg__convexFillStencil(D3D, call);
else if (call->type == D3DNVG_CONVEXFILL_STENCIL_CLEAR)
D3Dnvg__convexFillStencilClear(D3D, call);
}
}

Expand Down Expand Up @@ -1269,7 +1293,12 @@ static void D3Dnvg__renderFill(void* uptr, struct NVGpaint* paint, NVGcompositeO

if (npaths == 1 && paths[0].convex)
{
call->type = D3DNVG_CONVEXFILL;
if (scissor->stencilFlag == NVG_STENCIL_DEFAULT)
call->type = D3DNVG_CONVEXFILL;
else if (scissor->stencilFlag == NVG_STENCIL_ENABLE)
call->type = D3DNVG_CONVEXFILL_STENCIL;
else if (scissor->stencilFlag == NVG_STENCIL_CLEAR)
call->type = D3DNVG_CONVEXFILL_STENCIL_CLEAR;
call->triangleCount = 0; // Bounding box fill quad not needed for convex fill
}

Expand Down
177 changes: 96 additions & 81 deletions library/include/borealis/extern/nanovg/nvg_shader/D3D11PixelShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ else
add r0.yz, r0.yyzy, cb0[8].xxyx
div r0.yz, r0.yyzy, cb0[10].xxyx
sample_indexable(texture2d)(float,float,float,float) r1.xyzw, r0.yzyy, t0.xyzw, s0
ieq r0.yz, cb0[16].xxxx, l(0, 1, 2, 0)
ieq r0.yzw, cb0[16].xxxx, l(0, 1, 2, 3)
mul r2.xyz, r1.wwww, r1.xyzx
movc r1.xyz, r0.yyyy, r2.xyzx, r1.xyzx
movc r1.yzw, r0.zzzz, r1.xxxx, r1.yyzw
eq r0.y, r1.w, l(0.000000)
and r0.y, r0.y, r0.w
discard_nz r0.y
mul r1.xyzw, r1.xyzw, cb0[13].xyzw
mul o0.xyzw, r0.xxxx, r1.xyzw
else
Expand All @@ -112,20 +115,20 @@ else
endif
endif
ret
// Approximately 54 instruction slots used
// Approximately 57 instruction slots used
#endif

const BYTE g_D3D11PixelShader_Main[] =
{
68, 88, 66, 67, 78, 73,
165, 189, 231, 111, 49, 210,
111, 23, 174, 9, 214, 35,
203, 76, 1, 0, 0, 0,
244, 11, 0, 0, 5, 0,
68, 88, 66, 67, 56, 135,
171, 203, 207, 169, 4, 112,
255, 73, 81, 40, 58, 182,
14, 40, 1, 0, 0, 0,
56, 12, 0, 0, 5, 0,
0, 0, 52, 0, 0, 0,
16, 4, 0, 0, 128, 4,
0, 0, 180, 4, 0, 0,
88, 11, 0, 0, 82, 68,
156, 11, 0, 0, 82, 68,
69, 70, 212, 3, 0, 0,
1, 0, 0, 0, 192, 0,
0, 0, 3, 0, 0, 0,
Expand Down Expand Up @@ -318,8 +321,8 @@ const BYTE g_D3D11PixelShader_Main[] =
15, 0, 0, 0, 83, 86,
95, 84, 65, 82, 71, 69,
84, 0, 171, 171, 83, 72,
69, 88, 156, 6, 0, 0,
80, 0, 0, 0, 167, 1,
69, 88, 224, 6, 0, 0,
80, 0, 0, 0, 184, 1,
0, 0, 106, 8, 0, 1,
89, 0, 0, 4, 70, 142,
32, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -509,13 +512,13 @@ const BYTE g_D3D11PixelShader_Main[] =
0, 0, 70, 126, 16, 0,
0, 0, 0, 0, 0, 96,
16, 0, 0, 0, 0, 0,
32, 0, 0, 11, 98, 0,
32, 0, 0, 11, 226, 0,
16, 0, 0, 0, 0, 0,
6, 128, 32, 0, 0, 0,
0, 0, 16, 0, 0, 0,
2, 64, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0,
2, 0, 0, 0, 0, 0,
2, 0, 0, 0, 3, 0,
0, 0, 56, 0, 0, 7,
114, 0, 16, 0, 2, 0,
0, 0, 246, 15, 16, 0,
Expand All @@ -533,89 +536,101 @@ const BYTE g_D3D11PixelShader_Main[] =
0, 0, 6, 0, 16, 0,
1, 0, 0, 0, 86, 14,
16, 0, 1, 0, 0, 0,
56, 0, 0, 8, 242, 0,
16, 0, 1, 0, 0, 0,
70, 14, 16, 0, 1, 0,
0, 0, 70, 142, 32, 0,
0, 0, 0, 0, 13, 0,
0, 0, 56, 0, 0, 7,
242, 32, 16, 0, 0, 0,
0, 0, 6, 0, 16, 0,
0, 0, 0, 0, 70, 14,
16, 0, 1, 0, 0, 0,
18, 0, 0, 1, 32, 0,
0, 8, 34, 0, 16, 0,
0, 0, 0, 0, 26, 128,
32, 0, 0, 0, 0, 0,
16, 0, 0, 0, 1, 64,
0, 0, 2, 0, 0, 0,
31, 0, 4, 3, 26, 0,
24, 0, 0, 7, 34, 0,
16, 0, 0, 0, 0, 0,
54, 0, 0, 8, 242, 32,
58, 0, 16, 0, 1, 0,
0, 0, 1, 64, 0, 0,
0, 0, 0, 0, 1, 0,
0, 7, 34, 0, 16, 0,
0, 0, 0, 0, 26, 0,
16, 0, 0, 0, 0, 0,
2, 64, 0, 0, 0, 0,
128, 63, 0, 0, 128, 63,
0, 0, 128, 63, 0, 0,
128, 63, 18, 0, 0, 1,
69, 0, 0, 139, 194, 0,
0, 128, 67, 85, 21, 0,
58, 0, 16, 0, 0, 0,
0, 0, 13, 0, 4, 3,
26, 0, 16, 0, 0, 0,
0, 0, 56, 0, 0, 8,
242, 0, 16, 0, 1, 0,
0, 0, 70, 16, 16, 0,
1, 0, 0, 0, 70, 126,
0, 0, 70, 14, 16, 0,
1, 0, 0, 0, 70, 142,
32, 0, 0, 0, 0, 0,
13, 0, 0, 0, 56, 0,
0, 7, 242, 32, 16, 0,
0, 0, 0, 0, 6, 0,
16, 0, 0, 0, 0, 0,
0, 96, 16, 0, 0, 0,
0, 0, 32, 0, 0, 11,
98, 0, 16, 0, 0, 0,
0, 0, 6, 128, 32, 0,
0, 0, 0, 0, 16, 0,
70, 14, 16, 0, 1, 0,
0, 0, 18, 0, 0, 1,
32, 0, 0, 8, 34, 0,
16, 0, 0, 0, 0, 0,
26, 128, 32, 0, 0, 0,
0, 0, 16, 0, 0, 0,
1, 64, 0, 0, 2, 0,
0, 0, 31, 0, 4, 3,
26, 0, 16, 0, 0, 0,
0, 0, 54, 0, 0, 8,
242, 32, 16, 0, 0, 0,
0, 0, 2, 64, 0, 0,
0, 0, 0, 0, 1, 0,
0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 56, 0,
0, 7, 114, 0, 16, 0,
2, 0, 0, 0, 246, 15,
0, 0, 128, 63, 0, 0,
128, 63, 0, 0, 128, 63,
0, 0, 128, 63, 18, 0,
0, 1, 69, 0, 0, 139,
194, 0, 0, 128, 67, 85,
21, 0, 242, 0, 16, 0,
1, 0, 0, 0, 70, 16,
16, 0, 1, 0, 0, 0,
70, 2, 16, 0, 1, 0,
0, 0, 55, 0, 0, 9,
114, 0, 16, 0, 1, 0,
0, 0, 86, 5, 16, 0,
0, 0, 0, 0, 70, 2,
70, 126, 16, 0, 0, 0,
0, 0, 0, 96, 16, 0,
0, 0, 0, 0, 32, 0,
0, 11, 98, 0, 16, 0,
0, 0, 0, 0, 6, 128,
32, 0, 0, 0, 0, 0,
16, 0, 0, 0, 2, 64,
0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 2, 0,
0, 0, 0, 0, 0, 0,
56, 0, 0, 7, 114, 0,
16, 0, 2, 0, 0, 0,
70, 2, 16, 0, 1, 0,
0, 0, 55, 0, 0, 9,
226, 0, 16, 0, 1, 0,
0, 0, 166, 10, 16, 0,
246, 15, 16, 0, 1, 0,
0, 0, 70, 2, 16, 0,
1, 0, 0, 0, 55, 0,
0, 9, 114, 0, 16, 0,
1, 0, 0, 0, 86, 5,
16, 0, 0, 0, 0, 0,
70, 2, 16, 0, 2, 0,
0, 0, 70, 2, 16, 0,
1, 0, 0, 0, 55, 0,
0, 9, 226, 0, 16, 0,
1, 0, 0, 0, 166, 10,
16, 0, 0, 0, 0, 0,
6, 0, 16, 0, 1, 0,
0, 0, 86, 14, 16, 0,
1, 0, 0, 0, 56, 0,
0, 7, 242, 0, 16, 0,
0, 0, 0, 0, 6, 0,
16, 0, 1, 0, 0, 0,
86, 14, 16, 0, 1, 0,
0, 0, 56, 0, 0, 7,
242, 0, 16, 0, 0, 0,
0, 0, 6, 0, 16, 0,
0, 0, 0, 0, 70, 14,
16, 0, 1, 0, 0, 0,
56, 0, 0, 8, 242, 32,
16, 0, 0, 0, 0, 0,
70, 14, 16, 0, 0, 0,
0, 0, 70, 142, 32, 0,
0, 0, 0, 0, 13, 0,
0, 0, 21, 0, 0, 1,
21, 0, 0, 1, 21, 0,
0, 1, 62, 0, 0, 1,
83, 84, 65, 84, 148, 0,
0, 0, 54, 0, 0, 0,
3, 0, 0, 0, 0, 0,
70, 14, 16, 0, 1, 0,
0, 0, 56, 0, 0, 8,
242, 32, 16, 0, 0, 0,
0, 0, 70, 14, 16, 0,
0, 0, 0, 0, 70, 142,
32, 0, 0, 0, 0, 0,
13, 0, 0, 0, 21, 0,
0, 1, 21, 0, 0, 1,
21, 0, 0, 1, 62, 0,
0, 1, 83, 84, 65, 84,
148, 0, 0, 0, 57, 0,
0, 0, 3, 0, 0, 0,
33, 0, 0, 0, 4, 0,
0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 2, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0,
0, 0, 34, 0, 0, 0,
4, 0, 0, 0, 1, 0,
0, 0, 5, 0, 0, 0,
2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 0,
0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 4, 0,
0, 0, 1, 0, 0, 0,
4, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
Expand All @@ -626,5 +641,5 @@ const BYTE g_D3D11PixelShader_Main[] =
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
0, 0
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ float4 D3D11PixelShader_Main(PS_INPUT input) : SV_TARGET
float4 color = g_texture.Sample(g_sampler, pt);
if (texType == 1) color = float4(color.xyz*color.w,color.w);
if (texType == 2) color = float4(color.x, color.x, color.x, color.x);
if (texType == 3 && color.w == 0.0) discard;

// Apply color tint and alpha.
color *= innerCol;
Expand Down
Loading

0 comments on commit 23048cf

Please sign in to comment.