forked from openjdk/lanai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTLPaints.m
987 lines (857 loc) · 38.7 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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/*
* Copyright (c) 2019, 2021, 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.
*/
#include "MTLPaints.h"
#include "MTLClip.h"
#include "common.h"
#include "sun_java2d_SunGraphics2D.h"
#include "sun_java2d_pipe_BufferedPaints.h"
#import "MTLComposite.h"
#import "MTLBufImgOps.h"
#include "MTLRenderQueue.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 \
}
#define FLOAT_ARR_TO_V4(p) \
{ \
p[0], \
p[1], \
p[2], \
p[3] \
}
static MTLRenderPipelineDescriptor * templateRenderPipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateTexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateAATexturePipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateLCDPipelineDesc = nil;
static MTLRenderPipelineDescriptor * templateAAPipelineDesc = nil;
static void
setTxtUniforms(MTLContext *mtlc, int color, id <MTLRenderCommandEncoder> encoder, int interpolation, bool repeat,
jfloat extraAlpha, const SurfaceRasterFlags *srcFlags, const SurfaceRasterFlags *dstFlags, int mode);
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];
templateRenderPipelineDesc.sampleCount = 1;
templateRenderPipelineDesc.vertexDescriptor = vertDesc;
templateRenderPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateRenderPipelineDesc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
templateRenderPipelineDesc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
templateRenderPipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorOne;
templateRenderPipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
templateRenderPipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateRenderPipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateRenderPipelineDesc.label = @"template_render";
templateTexturePipelineDesc = [templateRenderPipelineDesc copy];
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];
templateAATexturePipelineDesc.label = @"template_aa_texture";
templateLCDPipelineDesc = [MTLRenderPipelineDescriptor new];
templateLCDPipelineDesc.sampleCount = 1;
templateLCDPipelineDesc.vertexDescriptor = vertDesc;
templateLCDPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateLCDPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stride = sizeof(struct TxtVertex);
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepRate = 1;
templateLCDPipelineDesc.vertexDescriptor.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateLCDPipelineDesc.label = @"template_lcd";
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 AAVertex);
vertDesc.layouts[MeshVertexBuffer].stepRate = 1;
vertDesc.layouts[MeshVertexBuffer].stepFunction = MTLVertexStepFunctionPerVertex;
templateAAPipelineDesc = [MTLRenderPipelineDescriptor new];
templateAAPipelineDesc.sampleCount = 1;
templateAAPipelineDesc.vertexDescriptor = vertDesc;
templateAAPipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
templateAAPipelineDesc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
templateAAPipelineDesc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
templateAAPipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorOne;
templateAAPipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorOne;
templateAAPipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateAAPipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
templateAAPipelineDesc.colorAttachments[0].blendingEnabled = YES;
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].format = MTLVertexFormatFloat2;
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].offset = 2*sizeof(float);
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeTexPos].bufferIndex = MeshVertexBuffer;
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeITexPos].format = MTLVertexFormatFloat2;
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeITexPos].offset = 4*sizeof(float);
templateAAPipelineDesc.vertexDescriptor.attributes[VertexAttributeITexPos].bufferIndex = MeshVertexBuffer;
templateAAPipelineDesc.label = @"template_aa";
}
@implementation MTLColorPaint {
// color-mode
jint _color;
}
- (id)initWithColor:(jint)color {
self = [super initWithState:sun_java2d_SunGraphics2D_PAINT_ALPHACOLOR];
if (self) {
_color = color;
}
return self;
}
- (jint)color {
return _color;
}
- (BOOL)isEqual:(MTLColorPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]])
return NO;
return _color == other->_color;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + _color;
return h;
}
- (NSString *)description {
return [NSString stringWithFormat:
@"[r=%d g=%d b=%d a=%d]",
(_color >> 16) & (0xFF),
(_color >> 8) & 0xFF,
(_color) & 0xFF,
(_color >> 24) & 0xFF];
}
- (void)setPipelineState:(id<MTLRenderCommandEncoder>)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
MTLRenderPipelineDescriptor *rpDesc = nil;
NSString *vertShader = @"vert_col";
NSString *fragShader = @"frag_col";
if (renderOptions->isTexture) {
vertShader = @"vert_txt";
fragShader = @"frag_txt";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
if (renderOptions->isAA) {
fragShader = @"aa_frag_txt";
rpDesc = [[templateAATexturePipelineDesc copy] autorelease];
}
if (renderOptions->isText) {
fragShader = @"frag_text";
}
if (renderOptions->isLCD) {
vertShader = @"vert_txt_lcd";
fragShader = @"lcd_color";
rpDesc = [[templateLCDPipelineDesc copy] autorelease];
}
setTxtUniforms(mtlc, _color, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha], &renderOptions->srcFlags,
&renderOptions->dstFlags, 1);
} else if (renderOptions->isAAShader) {
vertShader = @"vert_col_aa";
fragShader = @"frag_col_aa";
rpDesc = [[templateAAPipelineDesc copy] autorelease];
} else {
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
}
struct FrameUniforms uf = {RGBA_TO_V4(_color)};
[encoder setVertexBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
- (void)setXorModePipelineState:(id<MTLRenderCommandEncoder>)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
NSString * vertShader = @"vert_col_xorMode";
NSString * fragShader = @"frag_col_xorMode";
MTLRenderPipelineDescriptor * rpDesc = nil;
jint xorColor = (jint) [mtlc.composite getXorColor];
// Calculate _color ^ xorColor for RGB components
// This color gets XORed with destination framebuffer pixel color
const int col = _color ^ xorColor;
BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination();
if (renderOptions->isTexture) {
vertShader = @"vert_txt_xorMode";
fragShader = @"frag_txt_xorMode";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
setTxtUniforms(mtlc, col, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags, &renderOptions->dstFlags, 1);
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex:0];
[encoder setFragmentTexture:dstOps->pTexture atIndex:1];
} else {
struct FrameUniforms uf = {RGBA_TO_V4(col)};
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
[encoder setVertexBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[encoder setFragmentTexture:dstOps->pTexture atIndex:0];
}
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
@end
@implementation MTLBaseGradPaint {
jboolean _useMask;
@protected
jint _cyclic;
}
- (id)initWithState:(jint)state mask:(jboolean)useMask cyclic:(jboolean)cyclic {
self = [super initWithState:state];
if (self) {
_useMask = useMask;
_cyclic = cyclic;
}
return self;
}
- (BOOL)isEqual:(MTLBaseGradPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]])
return NO;
return [super isEqual:self] && _cyclic == other->_cyclic && _useMask == other->_useMask;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + _cyclic;
h = h*31 + _useMask;
return h;
}
@end
@implementation MTLGradPaint {
jdouble _p0;
jdouble _p1;
jdouble _p3;
jint _pixel1;
jint _pixel2;
}
- (id)initWithUseMask:(jboolean)useMask
cyclic:(jboolean)cyclic
p0:(jdouble)p0
p1:(jdouble)p1
p3:(jdouble)p3
pixel1:(jint)pixel1
pixel2:(jint)pixel2
{
self = [super initWithState:sun_java2d_SunGraphics2D_PAINT_GRADIENT
mask:useMask
cyclic:cyclic];
if (self) {
_p0 = p0;
_p1 = p1;
_p3 = p3;
_pixel1 = pixel1;
_pixel2 = pixel2;
}
return self;
}
- (BOOL)isEqual:(MTLGradPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]])
return NO;
return [super isEqual:self] && _p0 == other->_p0 &&
_p1 == other->_p1 && _p3 == other->_p3 &&
_pixel1 == other->_pixel1 && _pixel2 == other->_pixel2;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + [@(_p0) hash];
h = h*31 + [@(_p1) hash];;
h = h*31 + [@(_p3) hash];;
h = h*31 + _pixel1;
h = h*31 + _pixel2;
return h;
}
- (NSString *)description {
return [NSString stringWithFormat:@"gradient"];
}
- (void)setPipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
MTLRenderPipelineDescriptor *rpDesc = nil;
NSString *vertShader = @"vert_grad";
NSString *fragShader = @"frag_grad";
struct GradFrameUniforms uf = {
{_p0, _p1, _p3},
RGBA_TO_V4(_pixel1),
RGBA_TO_V4(_pixel2),
_cyclic,
[mtlc.composite getExtraAlpha]
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:0];
if (renderOptions->isTexture) {
vertShader = @"vert_txt_grad";
fragShader = @"frag_txt_grad";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
} else {
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
}
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
- (void)setXorModePipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
// This block is not reached in current implementation.
// Gradient paint XOR mode rendering uses a tile based rendering using a SW pipe (similar to OGL)
NSString* vertShader = @"vert_grad_xorMode";
NSString* fragShader = @"frag_grad_xorMode";
MTLRenderPipelineDescriptor *rpDesc = [[templateRenderPipelineDesc copy] autorelease];
jint xorColor = (jint) [mtlc.composite getXorColor];
struct GradFrameUniforms uf = {
{_p0, _p1, _p3},
RGBA_TO_V4(_pixel1 ^ xorColor),
RGBA_TO_V4(_pixel2 ^ xorColor),
_cyclic,
[mtlc.composite getExtraAlpha]
};
[encoder setFragmentBytes: &uf length:sizeof(uf) atIndex:0];
BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination();
[encoder setFragmentTexture:dstOps->pTexture atIndex:0];
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints - setXorModePipelineState -- PAINT_GRADIENT");
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
@end
@implementation MTLBaseMultiGradPaint {
@protected
jboolean _linear;
jint _numFracts;
jfloat _fract[GRAD_MAX_FRACTIONS];
jint _pixel[GRAD_MAX_FRACTIONS];
}
- (id)initWithState:(jint)state
mask:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
numStops:(jint)numStops
fractions:(jfloat *)fractions
pixels:(jint *)pixels
{
self = [super initWithState:state
mask:useMask
cyclic:cycleMethod];
if (self) {
_linear = linear;
memcpy(_fract, fractions,numStops*sizeof(jfloat));
memcpy(_pixel, pixels, numStops*sizeof(jint));
_numFracts = numStops;
}
return self;
}
- (BOOL)isEqual:(MTLBaseMultiGradPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]])
return NO;
if (_numFracts != other->_numFracts || ![super isEqual:self])
return NO;
for (int i = 0; i < _numFracts; i++) {
if (_fract[i] != other->_fract[i]) return NO;
if (_pixel[i] != other->_pixel[i]) return NO;
}
return YES;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + _numFracts;
for (int i = 0; i < _numFracts; i++) {
h = h*31 + [@(_fract[i]) hash];
h = h*31 + _pixel[i];
}
return h;
}
@end
@implementation MTLLinearGradPaint {
jdouble _p0;
jdouble _p1;
jdouble _p3;
}
- (void)setPipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
MTLRenderPipelineDescriptor *rpDesc = nil;
NSString *vertShader = @"vert_grad";
NSString *fragShader = @"frag_lin_grad";
if (renderOptions->isTexture) {
vertShader = @"vert_txt_grad";
fragShader = @"frag_txt_lin_grad";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
} else {
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
}
struct LinGradFrameUniforms uf = {
{_p0, _p1, _p3},
{},
{},
_numFracts,
_linear,
_cyclic,
[mtlc.composite getExtraAlpha]
};
memcpy(uf.fract, _fract, _numFracts*sizeof(jfloat));
for (int i = 0; i < _numFracts; i++) {
vector_float4 v = RGBA_TO_V4(_pixel[i]);
uf.color[i] = v;
}
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:0];
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
- (id)initWithUseMask:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
numStops:(jint)numStops
p0:(jfloat)p0
p1:(jfloat)p1
p3:(jfloat)p3
fractions:(jfloat *)fractions
pixels:(jint *)pixels
{
self = [super initWithState:sun_java2d_SunGraphics2D_PAINT_LIN_GRADIENT
mask:useMask
linear:linear
cycleMethod:cycleMethod
numStops:numStops
fractions:fractions
pixels:pixels];
if (self) {
_p0 = p0;
_p1 = p1;
_p3 = p3;
}
return self;
}
- (BOOL)isEqual:(MTLLinearGradPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]] || ![super isEqual:other])
return NO;
return _p0 == other->_p0 && _p1 == other->_p1 && _p3 == other->_p3;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + [@(_p0) hash];
h = h*31 + [@(_p1) hash];
h = h*31 + [@(_p3) hash];
return h;
}
- (NSString *)description {
return [NSString stringWithFormat:@"linear_gradient"];
}
@end
@implementation MTLRadialGradPaint {
jfloat _m00;
jfloat _m01;
jfloat _m02;
jfloat _m10;
jfloat _m11;
jfloat _m12;
jfloat _focusX;
}
- (id)initWithUseMask:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jint)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
{
self = [super initWithState:sun_java2d_SunGraphics2D_PAINT_RAD_GRADIENT
mask:useMask
linear:linear
cycleMethod:cycleMethod
numStops:numStops
fractions:fractions
pixels:pixels];
if (self) {
_m00 = m00;
_m01 = m01;
_m02 = m02;
_m10 = m10;
_m11 = m11;
_m12 = m12;
_focusX = focusX;
}
return self;
}
- (BOOL)isEqual:(MTLRadialGradPaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]]
|| ![super isEqual:self])
return NO;
return _m00 == other->_m00 && _m01 == other->_m01 && _m02 == other->_m02 &&
_m10 == other->_m10 && _m11 == other->_m11 && _m12 == other->_m12 &&
_focusX == other->_focusX;
}
- (NSUInteger)hash {
NSUInteger h = [super hash];
h = h*31 + [@(_m00) hash];
h = h*31 + [@(_m01) hash];
h = h*31 + [@(_m02) hash];
h = h*31 + [@(_m10) hash];
h = h*31 + [@(_m11) hash];
h = h*31 + [@(_m12) hash];
return h;
}
- (NSString *)description {
return [NSString stringWithFormat:@"radial_gradient"];
}
- (void)setPipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
MTLRenderPipelineDescriptor *rpDesc = nil;
NSString *vertShader = @"vert_grad";
NSString *fragShader = @"frag_rad_grad";
if (renderOptions->isTexture) {
vertShader = @"vert_txt_grad";
fragShader = @"frag_txt_rad_grad";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
} else {
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
}
struct RadGradFrameUniforms uf = {
{},
{},
_numFracts,
_linear,
_cyclic,
{_m00, _m01, _m02},
{_m10, _m11, _m12},
{},
[mtlc.composite getExtraAlpha]
};
uf.precalc[0] = _focusX;
uf.precalc[1] = 1.0 - (_focusX * _focusX);
uf.precalc[2] = 1.0 / uf.precalc[1];
memcpy(uf.fract, _fract, _numFracts*sizeof(jfloat));
for (int i = 0; i < _numFracts; i++) {
vector_float4 v = RGBA_TO_V4(_pixel[i]);
uf.color[i] = v;
}
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:0];
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
@end
@implementation MTLTexturePaint {
struct AnchorData _anchor;
id <MTLTexture> _paintTexture;
jboolean _isOpaque;
}
- (id)initWithUseMask:(jboolean)useMask
textureID:(id)textureId
isOpaque:(jboolean)isOpaque
filter:(jboolean)filter
xp0:(jdouble)xp0
xp1:(jdouble)xp1
xp3:(jdouble)xp3
yp0:(jdouble)yp0
yp1:(jdouble)yp1
yp3:(jdouble)yp3
{
self = [super initWithState:sun_java2d_SunGraphics2D_PAINT_TEXTURE];
if (self) {
_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;
_isOpaque = isOpaque;
}
return self;
}
- (BOOL)isEqual:(MTLTexturePaint *)other {
if (other == self)
return YES;
if (!other || ![[other class] isEqual:[self class]])
return NO;
return [_paintTexture isEqual: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];
}
- (NSString *)description {
return [NSString stringWithFormat:@"texture_paint"];
}
- (void)setPipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
MTLRenderPipelineDescriptor *rpDesc = nil;
NSString* vertShader = @"vert_tp";
NSString* fragShader = @"frag_tp";
[encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
if (renderOptions->isTexture) {
vertShader = @"vert_txt_tp";
fragShader = @"frag_txt_tp";
rpDesc = [[templateTexturePipelineDesc copy] autorelease];
[encoder setFragmentTexture:_paintTexture atIndex:1];
} else {
rpDesc = [[templateRenderPipelineDesc copy] autorelease];
[encoder setFragmentTexture:_paintTexture atIndex:0];
}
const SurfaceRasterFlags srcFlags = {_isOpaque, renderOptions->srcFlags.isPremultiplied};
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, YES, [mtlc.composite getExtraAlpha],
&srcFlags, &renderOptions->dstFlags, 0);
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
- (void)setXorModePipelineState:(id)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
// This block is not reached in current implementation.
// Texture paint XOR mode rendering uses a tile based rendering using a SW pipe (similar to OGL)
NSString* vertShader = @"vert_tp_xorMode";
NSString* fragShader = @"frag_tp_xorMode";
MTLRenderPipelineDescriptor *rpDesc = [[templateRenderPipelineDesc copy] autorelease];
jint xorColor = (jint) [mtlc.composite getXorColor];
[encoder setVertexBytes:&_anchor length:sizeof(_anchor) atIndex:FrameUniformBuffer];
[encoder setFragmentTexture:_paintTexture atIndex: 0];
BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination();
[encoder setFragmentTexture:dstOps->pTexture atIndex:1];
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex: 0];
J2dTraceLn(J2D_TRACE_INFO, "MTLPaints - setXorModePipelineState -- PAINT_TEXTURE");
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
@end
@implementation MTLPaint {
jint _paintState;
}
- (instancetype)init {
self = [super init];
if (self) {
_paintState = sun_java2d_SunGraphics2D_PAINT_UNDEFINED;
}
return self;
}
- (instancetype)initWithState:(jint)state {
self = [super init];
if (self) {
_paintState = state;
}
return self;
}
- (BOOL)isEqual:(MTLPaint *)other {
if (other == self)
return YES;
if (!other || ![other isKindOfClass:[self class]])
return NO;
return _paintState == other->_paintState;
}
- (NSUInteger)hash {
return _paintState;
}
- (NSString *)description {
return @"unknown-paint";
}
static void
setTxtUniforms(MTLContext *mtlc, int color, id <MTLRenderCommandEncoder> encoder, int interpolation, bool repeat,
jfloat extraAlpha, const SurfaceRasterFlags *srcFlags, const SurfaceRasterFlags *dstFlags, int mode) {
struct TxtFrameUniforms uf = {RGBA_TO_V4(color), mode, srcFlags->isOpaque, dstFlags->isOpaque, extraAlpha};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:interpolation repeat:repeat];
}
// For the current paint mode:
// 1. Selects vertex+fragment shaders (and corresponding pipelineDesc) and set pipelineState
// 2. Set vertex and fragment buffers
// Base implementation is used in drawTex2Tex
- (void)setPipelineState:(id <MTLRenderCommandEncoder>)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
initTemplatePipelineDescriptors();
// Called from drawTex2Tex used in flushBuffer and for buffered image ops
if (renderOptions->isTexture) {
NSString * vertShader = @"vert_txt";
NSString * fragShader = @"frag_txt";
MTLRenderPipelineDescriptor* rpDesc = [[templateTexturePipelineDesc copy] autorelease];
NSObject *bufImgOp = [mtlc getBufImgOp];
if (bufImgOp != nil) {
if ([bufImgOp isKindOfClass:[MTLRescaleOp class]]) {
MTLRescaleOp *rescaleOp = bufImgOp;
fragShader = @"frag_txt_op_rescale";
struct TxtFrameOpRescaleUniforms uf = {
RGBA_TO_V4(0), [mtlc.composite getExtraAlpha], renderOptions->srcFlags.isOpaque,
rescaleOp.isNonPremult,
FLOAT_ARR_TO_V4([rescaleOp getScaleFactors]), FLOAT_ARR_TO_V4([rescaleOp getOffsets])
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];
} else if ([bufImgOp isKindOfClass:[MTLConvolveOp class]]) {
MTLConvolveOp *convolveOp = bufImgOp;
fragShader = @"frag_txt_op_convolve";
struct TxtFrameOpConvolveUniforms uf = {
[mtlc.composite getExtraAlpha], renderOptions->srcFlags.isOpaque,
FLOAT_ARR_TO_V4([convolveOp getImgEdge]),
convolveOp.kernelSize, convolveOp.isEdgeZeroFill,
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];
[encoder setFragmentBuffer:[convolveOp getBuffer] offset:0 atIndex:2];
} else if ([bufImgOp isKindOfClass:[MTLLookupOp class]]) {
MTLLookupOp *lookupOp = bufImgOp;
fragShader = @"frag_txt_op_lookup";
struct TxtFrameOpLookupUniforms uf = {
[mtlc.composite getExtraAlpha], renderOptions->srcFlags.isOpaque,
FLOAT_ARR_TO_V4([lookupOp getOffset]), lookupOp.isUseSrcAlpha, lookupOp.isNonPremult,
};
[encoder setFragmentBytes:&uf length:sizeof(uf) atIndex:FrameUniformBuffer];
[mtlc.samplerManager setSamplerWithEncoder:encoder interpolation:renderOptions->interpolation repeat:NO];
[encoder setFragmentTexture:[lookupOp getLookupTexture] atIndex:1];
}
} else {
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags,
&renderOptions->dstFlags, 0);
}
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
}
// For the current paint mode:
// 1. Selects vertex+fragment shaders (and corresponding pipelineDesc) and set pipelineState
// 2. Set vertex and fragment buffers
- (void)setXorModePipelineState:(id <MTLRenderCommandEncoder>)encoder
context:(MTLContext *)mtlc
renderOptions:(const RenderOptions *)renderOptions
pipelineStateStorage:(MTLPipelineStatesStorage *)pipelineStateStorage
{
if (renderOptions->isTexture) {
initTemplatePipelineDescriptors();
jint xorColor = (jint) [mtlc.composite getXorColor];
NSString * vertShader = @"vert_txt_xorMode";
NSString * fragShader = @"frag_txt_xorMode";
MTLRenderPipelineDescriptor * rpDesc = [[templateTexturePipelineDesc copy] autorelease];
const int col = 0 ^ xorColor;
setTxtUniforms(mtlc, col, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags, &renderOptions->dstFlags, 0);
[encoder setFragmentBytes:&xorColor length:sizeof(xorColor) atIndex: 0];
BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination();
[encoder setFragmentTexture:dstOps->pTexture atIndex:1];
setTxtUniforms(mtlc, 0, encoder,
renderOptions->interpolation, NO, [mtlc.composite getExtraAlpha],
&renderOptions->srcFlags,
&renderOptions->dstFlags, 0);
id <MTLRenderPipelineState> pipelineState = [pipelineStateStorage getPipelineState:rpDesc
vertexShaderId:vertShader
fragmentShaderId:fragShader
composite:mtlc.composite
renderOptions:renderOptions
stencilNeeded:[mtlc.clip isShape]];
[encoder setRenderPipelineState:pipelineState];
}
}
@end