This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
MTLPaints.m
904 lines (792 loc) · 36.3 KB
/
MTLPaints.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#ifndef HEADLESS
#include "MTLPaints.h"
#include "MTLClip.h"
#include "common.h"
#include "sun_java2d_SunGraphics2D.h"
#include "sun_java2d_pipe_BufferedPaints.h"
#import "MTLComposite.h"
#define RGBA_TO_V4(c) \
{ \
(((c) >> 16) & (0xFF))/255.0f, \
(((c) >> 8) & 0xFF)/255.0f, \
((c) & 0xFF)/255.0f, \
(((c) >> 24) & 0xFF)/255.0f \
}
static MTLRenderPipelineDescriptor * templateRenderPipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateTexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateAATexturePipelineDesc = nil;
static void initTemplatePipelineDescriptors() {
if (templateRenderPipelineDesc != nil && templateTexturePipelineDesc != nil)
return;
MTLVertexDescriptor *vertDesc = [[MTLVertexDescriptor new] autorelease];
vertDesc.attributes[VertexAttributePosition].format = MTLVertexFormatFloat2;
vertDesc.attributes[VertexAttributePosition].offset = 0;
vertDesc.attributes[VertexAttributePosition].bufferIndex = MeshVertexBuffer;
vertDesc.layouts[MeshVertexBuffer].stride = sizeof(struct Vertex);
vertDesc.layouts[MeshVertexBuffer].stepRate = 1;
vertDesc.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateRenderPipelineDesc = [[MTLRenderPipelineDescriptor new] autorelease];
templateRenderPipelineDesc.sampleCount = 1;
templateRenderPipelineDesc.vertexDescriptor = vertDesc;
templateRenderPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateRenderPipelineDesc.label = @"template_render";
templateTexturePipelineDesc = [[templateRenderPipelineDesc copy] autorelease];
templateTexturePipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateTexturePipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateTexturePipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateTexturePipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stride = sizeof(struct TxtVertex);
templateTexturePipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepRate = 1;
templateTexturePipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateTexturePipelineDesc.label = @"template_texture";
templateAATexturePipelineDesc = [[templateTexturePipelineDesc copy] autorelease];
templateAATexturePipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorOne;
templateAATexturePipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
templateAATexturePipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateAATexturePipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateAATexturePipelineDesc.label = @"template_aa_texture";
}
@implementation MTLPaint {
// TODO: remove paintState, split into heirarchy of Paint-objects (i.e. PaintColor, PaintGrad, e.t.c)
jint _paintState;
// color-mode
jint _color;
// lin-grad-mode
jdouble _p0;
jdouble _p1;
jdouble _p3;
jboolean _cyclic;
jint _pixel1;
jint _pixel2;
jboolean _useMask;
// texture paint
id<MTLTexture> _paintTexture;
struct AnchorData _anchor;
}
- (id)init {
self = [super init];
if (self) {
_paintState = sun_java2d_SunGraphics2D_PAINT_UNDEFINED;
}
return self;
}
- (BOOL)isEqual:(MTLPaint *)other {
if (self == other)
return YES;
if (_paintState == sun_java2d_SunGraphics2D_PAINT_UNDEFINED)
return _paintState == other->_paintState;
if (_paintState != other->_paintState)
return NO;
if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
return _p0 == other->_p0
&& _p1 == other->_p1
&& _p3 == other->_p3
&& _pixel1 == other->_pixel1
&& _pixel2 == other->_pixel2;
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
return _color == other->_color;
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
return _paintTexture == other->_paintTexture
&& _anchor.xParams[0] == other->_anchor.xParams[0]
&& _anchor.xParams[1] == other->_anchor.xParams[1]
&& _anchor.xParams[2] == other->_anchor.xParams[2]
&& _anchor.yParams[0] == other->_anchor.yParams[0]
&& _anchor.yParams[1] == other->_anchor.yParams[1]
&& _anchor.yParams[2] == other->_anchor.yParams[2];
}
J2dTraceLn1(J2D_TRACE_ERROR, "Unimplemented paint mode %d", _paintState);
return NO;
}
- (void)copyFrom:(MTLPaint *)other {
_paintState = other->_paintState;
if (other->_paintState == sun_java2d_SunGraphics2D_PAINT_UNDEFINED)
return;
if (other->_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
_p0 = other->_p0;
_p1 = other->_p1;
_p3 = other->_p3;
_pixel1 = other->_pixel1;
_pixel2 = other->_pixel2;
return;
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
_color = other->_color;
return;
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
_color = other->_color;
_paintTexture = other->_paintTexture;
_anchor = other->_anchor;
return;
}
J2dTraceLn1(J2D_TRACE_ERROR, "Unsupported paint mode %d", _paintState);
}
- (NSString *)getDescription {
if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
return [NSString stringWithFormat:@"[r=%d g=%d b=%d a=%d]", (_color >> 16) & (0xFF), (_color >> 8) & 0xFF, (_color) & 0xFF, (_color >> 24) & 0xFF];
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
return [NSString stringWithFormat:@"gradient"];
}
if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
return [NSString stringWithFormat:@"texture_paint"];
}
return @"unknown-paint";
}
- (jint)getColor {
return _color;
}
- (void)reset {
_paintState = sun_java2d_SunGraphics2D_PAINT_UNDEFINED;
_paintTexture = nil;
_anchor.xParams[0] = _anchor.xParams[1] = _anchor.xParams[2] = 0.0f;
_anchor.yParams[0] = _anchor.yParams[1] = _anchor.yParams[2] = 0.0f;
}
- (void)setColor:(jint)pixelColor {
_paintState = sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR;
_color = pixelColor;
}
- (void)setGradientUseMask:(jboolean)useMask
cyclic:(jboolean)cyclic
p0:(jdouble)p0
p1:(jdouble)p1
p3:(jdouble)p3
pixel1:(jint)pixel1
pixel2:(jint)pixel2
{
//TODO Resolve gradient distribution problem
//TODO Implement useMask
//TODO Implement cyclic
//fprintf(stderr,
// "MTLPaints_SetGradientPaint useMask=%d cyclic=%d "
// "p0=%f p1=%f p3=%f pix1=%d pix2=%d\n", useMask, cyclic,
// p0, p1, p3, pixel1, pixel2);
_paintState = sun_java2d_SunGraphics2D_PAINT_GRADIENT;
_useMask = useMask;
_pixel1 = pixel1;
_pixel2 = pixel2;
_p0 = p0;
_p1 = p1;
_p3 = p3;
_cyclic = cyclic;
}
- (void)setLinearGradient:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
numStops:(jint)numStops
p0:(jfloat)p0
p1:(jfloat)p1
p3:(jfloat)p3
fractions:(void *)fractions
pixels:(void *)pixels
{
J2dTraceLn(J2D_TRACE_ERROR, "setLinearGradient: UNIMPLEMENTED");
[self setColor:0];
}
- (void)setRadialGradient:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
numStops:(jint)numStops
m00:(jfloat)m00
m01:(jfloat)m01
m02:(jfloat)m02
m10:(jfloat)m10
m11:(jfloat)m11
m12:(jfloat)m12
focusX:(jfloat)focusX
fractions:(void *)fractions
pixels:(void *)pixels
{
J2dTraceLn(J2D_TRACE_ERROR, "setRadialGradient: UNIMPLEMENTED");
[self setColor:0];
}
- (void)setTexture:(jboolean)useMask
textureID:(id<MTLTexture>)textureID
filter:(jboolean)filter
xp0:(jdouble)xp0
xp1:(jdouble)xp1
xp3:(jdouble)xp3
yp0:(jdouble)yp0
yp1:(jdouble)yp1
yp3:(jdouble)yp3
{
_paintState = sun_java2d_SunGraphics2D_PAINT_TEXTURE;
_paintTexture = textureID;
_anchor.xParams[0] = xp0;
_anchor.xParams[1] = xp1;
_anchor.xParams[2] = xp3;
_anchor.yParams[0] = yp0;
_anchor.yParams[1] = yp1;
_anchor.yParams[2] = yp3;
}
static id<MTLSamplerState> samplerNearestClamp = nil;
static id<MTLSamplerState> samplerLinearClamp = nil;
static id<MTLSamplerState> samplerNearestRepeat = nil;
static id<MTLSamplerState> samplerLinearRepeat = nil;
void initSamplers(id<MTLDevice> device) {
// TODO: move this code into SamplerManager (need implement)
if (samplerNearestClamp != nil)
return;
MTLSamplerDescriptor *samplerDescriptor = [MTLSamplerDescriptor new];
samplerDescriptor.rAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDescriptor.sAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDescriptor.tAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
samplerNearestClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];
samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerLinearClamp = [device newSamplerStateWithDescriptor:samplerDescriptor];
samplerDescriptor.rAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.sAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.tAddressMode = MTLSamplerAddressModeRepeat;
samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
samplerNearestRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];
samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
samplerLinearRepeat = [device newSamplerStateWithDescriptor:samplerDescriptor];
}
static void setTxtUniforms(
id<MTLRenderCommandEncoder> encoder, int color, int mode, int interpolation, bool repeat, jfloat extraAlpha,
const SurfaceRasterFlags * srcFlags, const SurfaceRasterFlags * dstFlags
) {
struct TxtFrameUniforms uf = {RGBA_TO_V4(color), mode, srcFlags->isOpaque, dstFlags->isOpaque, extraAlpha};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
id<MTLSamplerState> sampler;
if (repeat) {
sampler = interpolation == INTERPOLATION_BILINEAR ? samplerLinearRepeat : samplerNearestRepeat;
} else {
sampler = interpolation == INTERPOLATION_BILINEAR ? samplerLinearClamp : samplerNearestClamp;
}
[encoder setFragmentSamplerState:sampler atIndex:0];
}
// TODO: need support hints for all shaders
// For the current paint mode:
// 1. Selects vertex+fragment shaders (and corresponding pipelineDesc) and set pipelineState
// 2. Set vertex and fragment buffers
- (void)setPipelineState:(id<MTLRenderCommandEncoder>)encoder
composite:(MTLComposite *)composite
isStencilUsed:(jboolean)isStencilUsed
isTexture:(jboolean)isTexture
interpolation:(int)interpolation
isAA:(jboolean)isAA
srcFlags:(const SurfaceRasterFlags *)srcFlags
dstFlags:(const SurfaceRasterFlags *)dstFlags
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
const bool stencil = isStencilUsed == JNI_TRUE;
id<MTLRenderPipelineState> pipelineState = nil;
if (isTexture) {
if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
pipelineState = [pipelineStateStorage getPipelineState:templateTexturePipelineDesc
vertexShaderId:@"vert_txt_tp"
fragmentShaderId:@"frag_txt_tp"
compositeRule:[composite getRule]
isAA:JNI_FALSE
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
[encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
[encoder setFragmentTexture:_paintTexture atIndex: 1];
setTxtUniforms(encoder, 0, 0, interpolation, YES, [composite getExtraAlpha], srcFlags, dstFlags);
} else if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
pipelineState = [pipelineStateStorage getPipelineState:templateTexturePipelineDesc
vertexShaderId:@"vert_txt_grad"
fragmentShaderId:@"frag_txt_grad"
compositeRule:[composite getRule]
isAA:JNI_FALSE
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
struct GradFrameUniforms uf = {
{_p0, _p1, _p3},
RGBA_TO_V4(_pixel1),
RGBA_TO_V4(_pixel2)};
[encoder setFragmentBytes: &uf length:sizeof(uf) atIndex:0];
} else {
if (isAA) {
pipelineState = [pipelineStateStorage
getPipelineState:templateAATexturePipelineDesc
vertexShaderId:@"vert_txt"
fragmentShaderId:@"aa_frag_txt"
compositeRule:[composite getRule]
isAA:JNI_FALSE
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
} else {
pipelineState =
[pipelineStateStorage getPipelineState:templateTexturePipelineDesc
vertexShaderId:@"vert_txt"
fragmentShaderId:@"frag_txt"
compositeRule:[composite getRule]
composite:composite
isAA:JNI_FALSE
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
}
setTxtUniforms(encoder, _color, _paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR ? 1 : 0, interpolation, NO, [composite getExtraAlpha], srcFlags, dstFlags);
}
} else {
if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
pipelineState = [pipelineStateStorage getPipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_col"
fragmentShaderId:@"frag_col"
compositeRule:[composite getRule]
isAA:isAA
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
struct FrameUniforms uf = {RGBA_TO_V4(_color)};
[encoder setVertexBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
} else if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
pipelineState = [pipelineStateStorage getPipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_grad"
fragmentShaderId:@"frag_grad"
compositeRule:[composite getRule]
isAA:isAA
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
struct GradFrameUniforms uf = {
{_p0, _p1, _p3},
RGBA_TO_V4(_pixel1),
RGBA_TO_V4(_pixel2)};
[encoder setFragmentBytes: &uf length:sizeof(uf) atIndex:0];
} else if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
pipelineState = [pipelineStateStorage getPipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_tp"
fragmentShaderId:@"frag_tp"
compositeRule:[composite getRule]
isAA:isAA
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
[encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
[encoder setFragmentTexture:_paintTexture atIndex: 0];
}
}
[encoder setRenderPipelineState:pipelineState];
}
// For the current paint mode: and for XOR composite - a separate method is added as fragment shader differ in some cases
// 1. Selects vertex+fragment shaders (and corresponding pipelineDesc) and set pipelineState
// 2. Set vertex and fragment buffers
- (void)setXorModePipelineState:(id<MTLRenderCommandEncoder>)encoder
composite:(MTLComposite *)composite
isStencilUsed:(jboolean)isStencilUsed
isTexture:(jboolean)isTexture
interpolation:(int)interpolation
srcFlags:(const SurfaceRasterFlags *)srcFlags
dstFlags:(const SurfaceRasterFlags *)dstFlags
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage {
initTemplatePipelineDescriptors();
const bool stencil = isStencilUsed == JNI_TRUE;
jint xorColor = (jint) [composite getXorColor];
id<MTLRenderPipelineState> pipelineState = nil;
if (isTexture) {
pipelineState = [pipelineStateStorage getXorModePipelineState:templateTexturePipelineDesc
vertexShaderId:@"vert_txt"
fragmentShaderId:@"frag_txt"
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
const int col = _paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR ? _color ^ xorColor : 0 ^ xorColor;
setTxtUniforms(encoder, col, _paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR ? 1 : 0, interpolation, NO, [composite getExtraAlpha], srcFlags, dstFlags);
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex: 0];
} else {
if (_paintState == sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR) {
pipelineState = [pipelineStateStorage getXorModePipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_col"
fragmentShaderId:@"frag_col"
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
// Calculate _color ^ xorColor for RGB components
// This color gets XORed with destination framebuffer pixel color
struct FrameUniforms uf = {RGBA_TO_V4(_color ^ xorColor)};
[encoder setVertexBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
} else if (_paintState == sun_java2d_SunGraphics2D_PAINT_GRADIENT) {
pipelineState = [pipelineStateStorage getXorModePipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_grad"
fragmentShaderId:@"frag_grad"
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
struct GradFrameUniforms uf = {
{_p0, _p1, _p3},
RGBA_TO_V4(_pixel1 ^ xorColor),
RGBA_TO_V4(_pixel2 ^ xorColor)};
[encoder setFragmentBytes: &uf length:sizeof(uf) atIndex:0];
} else if (_paintState == sun_java2d_SunGraphics2D_PAINT_TEXTURE) {
pipelineState = [pipelineStateStorage getXorModePipelineState:templateRenderPipelineDesc
vertexShaderId:@"vert_tp"
fragmentShaderId:@"frag_tp_xorMode"
srcFlags:srcFlags
dstFlags:dstFlags
stencilNeeded:stencil];
[encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
[encoder setFragmentTexture:_paintTexture atIndex: 0];
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex: 0];
}
}
[encoder setRenderPipelineState:pipelineState];
}
@end
/************************* GradientPaint support ****************************/
static void
MTLPaints_InitGradientTexture()
{
//TODO
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_InitGradientTexture -- :TODO");
}
/****************** Shared MultipleGradientPaint support ********************/
/**
* These constants are identical to those defined in the
* MultipleGradientPaint.CycleMethod enum; they are copied here for
* convenience (ideally we would pull them directly from the Java level,
* but that entails more hassle than it is worth).
*/
#define CYCLE_NONE 0
#define CYCLE_REFLECT 1
#define CYCLE_REPEAT 2
/**
* The following constants are flags that can be bitwise-or'ed together
* to control how the MultipleGradientPaint shader source code is generated:
*
* MULTI_CYCLE_METHOD
* Placeholder for the CycleMethod enum constant.
*
* MULTI_LARGE
* If set, use the (slower) shader that supports a larger number of
* gradient colors; otherwise, use the optimized codepath. See
* the MAX_FRACTIONS_SMALL/LARGE constants below for more details.
*
* MULTI_USE_MASK
* If set, apply the alpha mask value from texture unit 0 to the
* final color result (only used in the MaskFill case).
*
* MULTI_LINEAR_RGB
* If set, convert the linear RGB result back into the sRGB color space.
*/
#define MULTI_CYCLE_METHOD (3 << 0)
#define MULTI_LARGE (1 << 2)
#define MULTI_USE_MASK (1 << 3)
#define MULTI_LINEAR_RGB (1 << 4)
/**
* This value determines the size of the array of programs for each
* MultipleGradientPaint type. This value reflects the maximum value that
* can be represented by performing a bitwise-or of all the MULTI_*
* constants defined above.
*/
#define MAX_PROGRAMS 32
/** Evaluates to true if the given bit is set on the local flags variable. */
#define IS_SET(flagbit) \
(((flags) & (flagbit)) != 0)
/** Composes the given parameters as flags into the given flags variable.*/
#define COMPOSE_FLAGS(flags, cycleMethod, large, useMask, linear) \
do { \
flags |= ((cycleMethod) & MULTI_CYCLE_METHOD); \
if (large) flags |= MULTI_LARGE; \
if (useMask) flags |= MULTI_USE_MASK; \
if (linear) flags |= MULTI_LINEAR_RGB; \
} while (0)
/** Extracts the CycleMethod enum value from the given flags variable. */
#define EXTRACT_CYCLE_METHOD(flags) \
((flags) & MULTI_CYCLE_METHOD)
/**
* The maximum number of gradient "stops" supported by the fragment shader
* and related code. When the MULTI_LARGE flag is set, we will use
* MAX_FRACTIONS_LARGE; otherwise, we use MAX_FRACTIONS_SMALL. By having
* two separate values, we can have one highly optimized shader (SMALL) that
* supports only a few fractions/colors, and then another, less optimal
* shader that supports more stops.
*/
#define MAX_FRACTIONS sun_java2d_pipe_BufferedPaints_MULTI_MAX_FRACTIONS
#define MAX_FRACTIONS_LARGE MAX_FRACTIONS
#define MAX_FRACTIONS_SMALL 4
/**
* The maximum number of gradient colors supported by all of the gradient
* fragment shaders. Note that this value must be a power of two, as it
* determines the size of the 1D texture created below. It also must be
* greater than or equal to MAX_FRACTIONS (there is no strict requirement
* that the two values be equal).
*/
#define MAX_COLORS 16
/**
* The handle to the gradient color table texture object used by the shaders.
*/
static jint multiGradientTexID = 0;
/**
* This is essentially a template of the shader source code that can be used
* for either LinearGradientPaint or RadialGradientPaint. It includes the
* structure and some variables that are common to each; the remaining
* code snippets (for CycleMethod, ColorSpaceType, and mask modulation)
* are filled in prior to compiling the shader at runtime depending on the
* paint parameters. See MTLPaints_CreateMultiGradProgram() for more details.
*/
static const char *multiGradientShaderSource =
// gradient texture size (in texels)
"const int TEXTURE_SIZE = %d;"
// maximum number of fractions/colors supported by this shader
"const int MAX_FRACTIONS = %d;"
// size of a single texel
"const float FULL_TEXEL = (1.0 / float(TEXTURE_SIZE));"
// size of half of a single texel
"const float HALF_TEXEL = (FULL_TEXEL / 2.0);"
// texture containing the gradient colors
"uniform sampler1D colors;"
// array of gradient stops/fractions
"uniform float fractions[MAX_FRACTIONS];"
// array of scale factors (one for each interval)
"uniform float scaleFactors[MAX_FRACTIONS-1];"
// (placeholder for mask variable)
"%s"
// (placeholder for Linear/RadialGP-specific variables)
"%s"
""
"void main(void)"
"{"
" float dist;"
// (placeholder for Linear/RadialGradientPaint-specific code)
" %s"
""
" float tc;"
// (placeholder for CycleMethod-specific code)
" %s"
""
// calculate interpolated color
" vec4 result = texture1D(colors, tc);"
""
// (placeholder for ColorSpace conversion code)
" %s"
""
// (placeholder for mask modulation code)
" %s"
""
// modulate with gl_Color in order to apply extra alpha
" gl_FragColor = result * gl_Color;"
"}";
/**
* This code takes a "dist" value as input (as calculated earlier by the
* LGP/RGP-specific code) in the range [0,1] and produces a texture
* coordinate value "tc" that represents the position of the chosen color
* in the one-dimensional gradient texture (also in the range [0,1]).
*
* One naive way to implement this would be to iterate through the fractions
* to figure out in which interval "dist" falls, and then compute the
* relative distance between the two nearest stops. This approach would
* require an "if" check on every iteration, and it is best to avoid
* conditionals in fragment shaders for performance reasons. Also, one might
* be tempted to use a break statement to jump out of the loop once the
* interval was found, but break statements (and non-constant loop bounds)
* are not natively available on most graphics hardware today, so that is
* a non-starter.
*
* The more optimal approach used here avoids these issues entirely by using
* an accumulation function that is equivalent to the process described above.
* The scaleFactors array is pre-initialized at enable time as follows:
* scaleFactors[i] = 1.0 / (fractions[i+1] - fractions[i]);
*
* For each iteration, we subtract fractions[i] from dist and then multiply
* that value by scaleFactors[i]. If we are within the target interval,
* this value will be a fraction in the range [0,1] indicating the relative
* distance between fraction[i] and fraction[i+1]. If we are below the
* target interval, this value will be negative, so we clamp it to zero
* to avoid accumulating any value. If we are above the target interval,
* the value will be greater than one, so we clamp it to one. Upon exiting
* the loop, we will have accumulated zero or more 1.0's and a single
* fractional value. This accumulated value tells us the position of the
* fragment color in the one-dimensional gradient texture, i.e., the
* texcoord called "tc".
*/
static const char *texCoordCalcCode =
"int i;"
"float relFraction = 0.0;"
"for (i = 0; i < MAX_FRACTIONS-1; i++) {"
" relFraction +="
" clamp((dist - fractions[i]) * scaleFactors[i], 0.0, 1.0);"
"}"
// we offset by half a texel so that we find the linearly interpolated
// color between the two texel centers of interest
"tc = HALF_TEXEL + (FULL_TEXEL * relFraction);";
/** Code for NO_CYCLE that gets plugged into the CycleMethod placeholder. */
static const char *noCycleCode =
"if (dist <= 0.0) {"
" tc = 0.0;"
"} else if (dist >= 1.0) {"
" tc = 1.0;"
"} else {"
// (placeholder for texcoord calculation)
" %s"
"}";
/** Code for REFLECT that gets plugged into the CycleMethod placeholder. */
static const char *reflectCode =
"dist = 1.0 - (abs(fract(dist * 0.5) - 0.5) * 2.0);"
// (placeholder for texcoord calculation)
"%s";
/** Code for REPEAT that gets plugged into the CycleMethod placeholder. */
static const char *repeatCode =
"dist = fract(dist);"
// (placeholder for texcoord calculation)
"%s";
static void
MTLPaints_InitMultiGradientTexture()
{
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_InitMultiGradientTexture -- :TODO");
}
/**
* Compiles and links the MultipleGradientPaint shader program. If
* successful, this function returns a handle to the newly created
* shader program; otherwise returns 0.
*/
static void*
MTLPaints_CreateMultiGradProgram(jint flags,
char *paintVars, char *distCode)
{
//TODO
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_CreateMultiGradProgram -- :TODO");
return NULL;
}
/**
* Called from the MTLPaints_SetLinear/RadialGradientPaint() methods
* in order to setup the fraction/color values that are common to both.
*/
static void
MTLPaints_SetMultiGradientPaint(void* multiGradProgram,
jint numStops,
void *pFractions, void *pPixels)
{
//TODO
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints_SetMultiGradientPaint -- :TODO");
}
/********************** LinearGradientPaint support *************************/
/**
* The handles to the LinearGradientPaint fragment program objects. The
* index to the array should be a bitwise-or'ing of the MULTI_* flags defined
* above. Note that most applications will likely need to initialize one
* or two of these elements, so the array is usually sparsely populated.
*/
static void* linearGradPrograms[MAX_PROGRAMS];
/**
* Compiles and links the LinearGradientPaint shader program. If successful,
* this function returns a handle to the newly created shader program;
* otherwise returns 0.
*/
static void*
MTLPaints_CreateLinearGradProgram(jint flags)
{
char *paintVars;
char *distCode;
J2dTraceLn1(J2D_TRACE_INFO,
"MTLPaints_CreateLinearGradProgram",
flags);
/*
* To simplify the code and to make it easier to upload a number of
* uniform values at once, we pack a bunch of scalar (float) values
* into vec3 values below. Here's how the values are related:
*
* params.x = p0
* params.y = p1
* params.z = p3
*
* yoff = dstOps->yOffset + dstOps->height
*/
paintVars =
"uniform vec3 params;"
"uniform float yoff;";
distCode =
// note that gl_FragCoord is in window space relative to the
// lower-left corner, so we have to flip the y-coordinate here
"vec3 fragCoord = vec3(gl_FragCoord.x, yoff-gl_FragCoord.y, 1.0);"
"dist = dot(params, fragCoord);";
return MTLPaints_CreateMultiGradProgram(flags, paintVars, distCode);
}
/********************** RadialGradientPaint support *************************/
/**
* The handles to the RadialGradientPaint fragment program objects. The
* index to the array should be a bitwise-or'ing of the MULTI_* flags defined
* above. Note that most applications will likely need to initialize one
* or two of these elements, so the array is usually sparsely populated.
*/
static void* radialGradPrograms[MAX_PROGRAMS];
/**
* Compiles and links the RadialGradientPaint shader program. If successful,
* this function returns a handle to the newly created shader program;
* otherwise returns 0.
*/
static void*
MTLPaints_CreateRadialGradProgram(jint flags)
{
char *paintVars;
char *distCode;
J2dTraceLn1(J2D_TRACE_INFO,
"MTLPaints_CreateRadialGradProgram",
flags);
/*
* To simplify the code and to make it easier to upload a number of
* uniform values at once, we pack a bunch of scalar (float) values
* into vec3 and vec4 values below. Here's how the values are related:
*
* m0.x = m00
* m0.y = m01
* m0.z = m02
*
* m1.x = m10
* m1.y = m11
* m1.z = m12
*
* precalc.x = focusX
* precalc.y = yoff = dstOps->yOffset + dstOps->height
* precalc.z = 1.0 - (focusX * focusX)
* precalc.w = 1.0 / precalc.z
*/
paintVars =
"uniform vec3 m0;"
"uniform vec3 m1;"
"uniform vec4 precalc;";
/*
* The following code is derived from Daniel Rice's whitepaper on
* radial gradient performance (attached to the bug report for 6521533).
* Refer to that document as well as the setup code in the Java-level
* BufferedPaints.setRadialGradientPaint() method for more details.
*/
distCode =
// note that gl_FragCoord is in window space relative to the
// lower-left corner, so we have to flip the y-coordinate here
"vec3 fragCoord ="
" vec3(gl_FragCoord.x, precalc.y - gl_FragCoord.y, 1.0);"
"float x = dot(fragCoord, m0);"
"float y = dot(fragCoord, m1);"
"float xfx = x - precalc.x;"
"dist = (precalc.x*xfx + sqrt(xfx*xfx + y*y*precalc.z))*precalc.w;";
return MTLPaints_CreateMultiGradProgram(flags, paintVars, distCode);
}
#endif /* !HEADLESS */