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
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
|
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma version(1)
#pragma rs java_package_name(com.android.perftest)
#include "rs_graphics.rsh"
#include "shader_def.rsh"
/* Message sent from script to renderscript */
const int RS_MSG_TEST_DONE = 100;
const int RS_MSG_RESULTS_READY = 101;
const int gMaxModes = 31;
int gMaxLoops;
// Parameters for galaxy live wallpaper
rs_allocation gTSpace;
rs_allocation gTLight1;
rs_allocation gTFlares;
rs_mesh gParticlesMesh;
rs_program_fragment gPFBackground;
rs_program_fragment gPFStars;
rs_program_vertex gPVStars;
rs_program_vertex gPVBkProj;
rs_program_store gPSLights;
float gXOffset = 0.5f;
#define ELLIPSE_RATIO 0.892f
#define PI 3.1415f
#define TWO_PI 6.283f
#define ELLIPSE_TWIST 0.023333333f
static float angle = 50.f;
static int gOldWidth;
static int gOldHeight;
static int gWidth;
static int gHeight;
static float gSpeed[12000];
static int gGalaxyRadius = 300;
static rs_allocation gParticlesBuffer;
typedef struct __attribute__((packed, aligned(4))) Particle {
uchar4 color;
float3 position;
} Particle_t;
Particle_t *Particles;
typedef struct VpConsts {
rs_matrix4x4 Proj;
rs_matrix4x4 MVP;
} VpConsts_t;
VpConsts_t *vpConstants;
// End of parameters for galaxy live wallpaper
// Allocation to send test names back to java
char *gStringBuffer = 0;
// Allocation to write the results into
static float gResultBuffer[gMaxModes];
rs_program_vertex gProgVertex;
rs_program_fragment gProgFragmentColor;
rs_program_fragment gProgFragmentTexture;
rs_program_store gProgStoreBlendNoneDepth;
rs_program_store gProgStoreBlendNone;
rs_program_store gProgStoreBlendAlpha;
rs_program_store gProgStoreBlendAdd;
rs_allocation gTexOpaque;
rs_allocation gTexTorus;
rs_allocation gTexTransparent;
rs_allocation gTexChecker;
rs_allocation gTexGlobe;
typedef struct ListAllocs_s {
rs_allocation item;
} ListAllocs;
ListAllocs *gTexList100;
ListAllocs *gSampleTextList100;
ListAllocs *gListViewText;
rs_mesh g10by10Mesh;
rs_mesh g100by100Mesh;
rs_mesh gWbyHMesh;
rs_mesh gTorusMesh;
rs_mesh gSingleMesh;
rs_font gFontSans;
rs_font gFontSerif;
int gDisplayMode;
rs_sampler gLinearClamp;
rs_sampler gLinearWrap;
rs_sampler gMipLinearWrap;
rs_sampler gNearestClamp;
rs_program_raster gCullBack;
rs_program_raster gCullFront;
rs_program_raster gCullNone;
// Custom vertex shader compunents
VertexShaderConstants *gVSConstants;
FragentShaderConstants *gFSConstants;
VertexShaderConstants3 *gVSConstPixel;
FragentShaderConstants3 *gFSConstPixel;
// Export these out to easily set the inputs to shader
VertexShaderInputs *gVSInputs;
// Custom shaders we use for lighting
rs_program_vertex gProgVertexCustom;
rs_program_fragment gProgFragmentCustom;
rs_program_vertex gProgVertexPixelLight;
rs_program_vertex gProgVertexPixelLightMove;
rs_program_fragment gProgFragmentPixelLight;
rs_program_fragment gProgFragmentMultitex;
rs_allocation gRenderBufferColor;
rs_allocation gRenderBufferDepth;
float gDt = 0;
void init() {
}
static int gRenderSurfaceW;
static int gRenderSurfaceH;
static const char *sampleText = "This is a sample of small text for performace";
// Offsets for multiple layer of text
static int textOffsets[] = { 0, 0, -5, -5, 5, 5, -8, -8, 8, 8};
static float textColors[] = {1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.7f, 0.5f, 1.0f,
0.7f, 0.5f, 0.5f, 1.0f,
0.5f, 0.5f, 0.7f, 1.0f,
0.5f, 0.6f, 0.7f, 1.0f,
};
/**
* Methods to draw the galaxy live wall paper
*/
static float mapf(float minStart, float minStop, float maxStart, float maxStop, float value) {
return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
}
/**
* Helper function to generate the stars.
*/
static float randomGauss() {
float x1;
float x2;
float w = 2.f;
while (w >= 1.0f) {
x1 = rsRand(2.0f) - 1.0f;
x2 = rsRand(2.0f) - 1.0f;
w = x1 * x1 + x2 * x2;
}
w = sqrt(-2.0f * log(w) / w);
return x1 * w;
}
/**
* Generates the properties for a given star.
*/
static void createParticle(Particle_t *part, int idx, float scale) {
float d = fabs(randomGauss()) * gGalaxyRadius * 0.5f + rsRand(64.0f);
float id = d / gGalaxyRadius;
float z = randomGauss() * 0.4f * (1.0f - id);
float p = -d * ELLIPSE_TWIST;
if (d < gGalaxyRadius * 0.33f) {
part->color.x = (uchar) (220 + id * 35);
part->color.y = 220;
part->color.z = 220;
} else {
part->color.x = 180;
part->color.y = 180;
part->color.z = (uchar) clamp(140.f + id * 115.f, 140.f, 255.f);
}
// Stash point size * 10 in Alpha
part->color.w = (uchar) (rsRand(1.2f, 2.1f) * 60);
if (d > gGalaxyRadius * 0.15f) {
z *= 0.6f * (1.0f - id);
} else {
z *= 0.72f;
}
// Map to the projection coordinates (viewport.x = -1.0 -> 1.0)
d = mapf(-4.0f, gGalaxyRadius + 4.0f, 0.0f, scale, d);
part->position.x = rsRand(TWO_PI);
part->position.y = d;
gSpeed[idx] = rsRand(0.0015f, 0.0025f) * (0.5f + (scale / d)) * 0.8f;
part->position.z = z / 5.0f;
}
/**
* Initialize all the starts, called from Java
*/
void initParticles() {
Particle_t *part = Particles;
float scale = gGalaxyRadius / (gWidth * 0.5f);
int count = rsAllocationGetDimX(gParticlesBuffer);
for (int i = 0; i < count; i ++) {
createParticle(part, i, scale);
part++;
}
}
static void drawSpace() {
rsgBindProgramFragment(gPFBackground);
rsgBindTexture(gPFBackground, 0, gTSpace);
rsgDrawQuadTexCoords(
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
gWidth, 0.0f, 0.0f, 2.0f, 1.0f,
gWidth, gHeight, 0.0f, 2.0f, 0.0f,
0.0f, gHeight, 0.0f, 0.0f, 0.0f);
}
static void drawLights() {
rsgBindProgramVertex(gPVBkProj);
rsgBindProgramFragment(gPFBackground);
rsgBindTexture(gPFBackground, 0, gTLight1);
float scale = 512.0f / gWidth;
float x = -scale - scale * 0.05f;
float y = -scale;
scale *= 2.0f;
rsgDrawQuad(x, y, 0.0f,
x + scale * 1.1f, y, 0.0f,
x + scale * 1.1f, y + scale, 0.0f,
x, y + scale, 0.0f);
}
static void drawParticles(float offset) {
float a = offset * angle;
float absoluteAngle = fabs(a);
rs_matrix4x4 matrix;
rsMatrixLoadTranslate(&matrix, 0.0f, 0.0f, 10.0f - 6.0f * absoluteAngle / 50.0f);
if (gHeight > gWidth) {
rsMatrixScale(&matrix, 6.6f, 6.0f, 1.0f);
} else {
rsMatrixScale(&matrix, 12.6f, 12.0f, 1.0f);
}
rsMatrixRotate(&matrix, absoluteAngle, 1.0f, 0.0f, 0.0f);
rsMatrixRotate(&matrix, a, 0.0f, 0.4f, 0.1f);
rsMatrixLoad(&vpConstants->MVP, &vpConstants->Proj);
rsMatrixMultiply(&vpConstants->MVP, &matrix);
rsgAllocationSyncAll(rsGetAllocation(vpConstants));
rsgBindProgramVertex(gPVStars);
rsgBindProgramFragment(gPFStars);
rsgBindProgramStore(gPSLights);
rsgBindTexture(gPFStars, 0, gTFlares);
Particle_t *vtx = Particles;
int count = rsAllocationGetDimX(gParticlesBuffer);
for (int i = 0; i < count; i++) {
vtx->position.x = vtx->position.x + gSpeed[i];
vtx++;
}
rsgDrawMesh(gParticlesMesh);
}
/* end of methods for drawing galaxy */
static void setupOffscreenTarget() {
rsgBindColorTarget(gRenderBufferColor, 0);
rsgBindDepthTarget(gRenderBufferDepth);
}
static void displayFontSamples(int fillNum) {
rs_font fonts[5];
fonts[0] = gFontSans;
fonts[1] = gFontSerif;
fonts[2] = gFontSans;
fonts[3] = gFontSerif;
fonts[4] = gFontSans;
uint width = gRenderSurfaceW;
uint height = gRenderSurfaceH;
int left = 0, right = 0, top = 0, bottom = 0;
rsgMeasureText(sampleText, &left, &right, &top, &bottom);
int textHeight = top - bottom;
int textWidth = right - left;
int numVerticalLines = height / textHeight;
int yPos = top;
int xOffset = 0, yOffset = 0;
for(int fillI = 0; fillI < fillNum; fillI ++) {
rsgBindFont(fonts[fillI]);
xOffset = textOffsets[fillI * 2];
yOffset = textOffsets[fillI * 2 + 1];
float *colPtr = textColors + fillI * 4;
rsgFontColor(colPtr[0], colPtr[1], colPtr[2], colPtr[3]);
for (int h = 0; h < 4; h ++) {
yPos = top + yOffset;
for (int v = 0; v < numVerticalLines; v ++) {
rsgDrawText(sampleText, xOffset + textWidth * h, yPos);
yPos += textHeight;
}
}
}
}
static void bindProgramVertexOrtho() {
// Default vertex shader
rsgBindProgramVertex(gProgVertex);
// Setup the projection matrix
rs_matrix4x4 proj;
rsMatrixLoadOrtho(&proj, 0, gRenderSurfaceW, gRenderSurfaceH, 0, -500, 500);
rsgProgramVertexLoadProjectionMatrix(&proj);
}
static void displaySingletexFill(bool blend, int quadCount) {
bindProgramVertexOrtho();
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsgProgramVertexLoadModelMatrix(&matrix);
// Fragment shader with texture
if (!blend) {
rsgBindProgramStore(gProgStoreBlendNone);
} else {
rsgBindProgramStore(gProgStoreBlendAlpha);
}
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
for (int i = 0; i < quadCount; i ++) {
float startX = 5 * i, startY = 5 * i;
float width = gRenderSurfaceW - startX, height = gRenderSurfaceH - startY;
rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
startX, startY + height, 0, 0, 1,
startX + width, startY + height, 0, 1, 1,
startX + width, startY, 0, 1, 0);
}
}
static void displayMeshSamples(int meshNum) {
bindProgramVertexOrtho();
rs_matrix4x4 matrix;
rsMatrixLoadTranslate(&matrix, gRenderSurfaceW/2, gRenderSurfaceH/2, 0);
rsgProgramVertexLoadModelMatrix(&matrix);
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNone);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
rsgBindTexture(gProgFragmentTexture, 0, gTexOpaque);
if (meshNum == 0) {
rsgDrawMesh(g10by10Mesh);
} else if (meshNum == 1) {
rsgDrawMesh(g100by100Mesh);
} else if (meshNum == 2) {
rsgDrawMesh(gWbyHMesh);
}
}
// Display sample images in a mesh with different texture
static void displayIcons(int meshMode) {
bindProgramVertexOrtho();
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendAlpha);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
rsgBindTexture(gProgFragmentTexture, 0, gTexTorus);
rsgDrawQuadTexCoords(
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, gRenderSurfaceH, 0.0f, 0.0f, 1.0f,
gRenderSurfaceW, gRenderSurfaceH, 0.0f, 1.0f, 1.0f,
gRenderSurfaceW, 0.0f, 0.0f, 1.0f, 0.0f);
int meshCount = (int)pow(10.0f, (float)(meshMode + 1));
float wSize = gRenderSurfaceW/(float)meshCount;
float hSize = gRenderSurfaceH/(float)meshCount;
rs_matrix4x4 matrix;
rsMatrixLoadScale(&matrix, wSize, hSize, 1.0);
float yPos = 0;
float yPad = hSize / 2;
float xPad = wSize / 2;
for (int y = 0; y < meshCount; y++) {
yPos = y * hSize + yPad;
float xPos = 0;
for (int x = 0; x < meshCount; x++) {
xPos = x * wSize + xPad;
rs_matrix4x4 transMatrix;
rsMatrixLoadTranslate(&transMatrix, xPos, yPos, 0);
rsMatrixMultiply(&transMatrix, &matrix);
rsgProgramVertexLoadModelMatrix(&transMatrix);
int i = (x + y * meshCount) % 100;
rsgBindTexture(gProgFragmentTexture, 0, gTexList100[i].item);
rsgDrawMesh(gSingleMesh);
}
}
}
// Draw meshes in a single page with top left corner coordinates (xStart, yStart)
static void drawMeshInPage(float xStart, float yStart, int wResolution, int hResolution) {
// Draw wResolution * hResolution meshes in one page
float wMargin = 100.0f;
float hMargin = 100.0f;
float xPad = 50.0f;
float yPad = 20.0f;
float size = 100.0f; // size of images
// font info
rs_font font = gFontSans;
rsgBindFont(font);
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
// Measure text size
int left = 0, right = 0, top = 0, bottom = 0;
rsgMeasureText(gSampleTextList100[0].item, &left, &right, &top, &bottom);
float textHeight = (float)(top - bottom);
float textWidth = (float)(right - left);
rs_matrix4x4 matrix;
rsMatrixLoadScale(&matrix, size, size, 1.0);
for (int y = 0; y < hResolution; y++) {
float yPos = yStart + hMargin + y * size + y * yPad;
for (int x = 0; x < wResolution; x++) {
float xPos = xStart + wMargin + x * size + x * xPad;
rs_matrix4x4 transMatrix;
rsMatrixLoadTranslate(&transMatrix, xPos + size/2, yPos + size/2, 0);
rsMatrixMultiply(&transMatrix, &matrix); // scale the mesh
rsgProgramVertexLoadModelMatrix(&transMatrix);
int i = (y * wResolution + x) % 100;
rsgBindTexture(gProgFragmentTexture, 0, gTexList100[i].item);
rsgDrawMesh(gSingleMesh);
rsgDrawText(gSampleTextList100[i].item, xPos, yPos + size + yPad/2 + textHeight);
}
}
}
// Display both images and text as shown in launcher and homepage
// meshMode will decide how many pages we draw
// meshMode = 0: draw 3 pages of meshes
// meshMode = 1: draw 5 pages of meshes
static void displayImageWithText(int wResolution, int hResolution, int meshMode) {
bindProgramVertexOrtho();
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendAlpha);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
drawMeshInPage(0, 0, wResolution, hResolution);
drawMeshInPage(-1.0f*gRenderSurfaceW, 0, wResolution, hResolution);
drawMeshInPage(1.0f*gRenderSurfaceW, 0, wResolution, hResolution);
if (meshMode == 1) {
// draw another two pages of meshes
drawMeshInPage(-2.0f*gRenderSurfaceW, 0, wResolution, hResolution);
drawMeshInPage(2.0f*gRenderSurfaceW, 0, wResolution, hResolution);
}
}
// Display a list of text as the list view
static void displayListView() {
// set text color
rsgFontColor(0.9f, 0.9f, 0.9f, 1.0f);
rsgBindFont(gFontSans);
// get the size of the list
rs_allocation textAlloc;
textAlloc = rsGetAllocation(gListViewText);
int allocSize = rsAllocationGetDimX(textAlloc);
int listItemHeight = 80;
int yOffset = listItemHeight;
// set the color for the list divider
rsgBindProgramFragment(gProgFragmentColor);
rsgProgramFragmentConstantColor(gProgFragmentColor, 1.0, 1.0, 1.0, 1);
// draw the list with divider
for (int i = 0; i < allocSize; i++) {
if (yOffset - listItemHeight > gRenderSurfaceH) {
break;
}
rsgDrawRect(0, yOffset - 1, gRenderSurfaceW, yOffset, 0);
rsgDrawText(gListViewText[i].item, 20, yOffset - 10);
yOffset += listItemHeight;
}
}
static void drawGalaxy() {
rsgClearColor(0.f, 0.f, 0.f, 1.f);
gParticlesBuffer = rsGetAllocation(Particles);
rsgBindProgramFragment(gPFBackground);
gWidth = rsgGetWidth();
gHeight = rsgGetHeight();
if ((gWidth != gOldWidth) || (gHeight != gOldHeight)) {
initParticles();
gOldWidth = gWidth;
gOldHeight = gHeight;
}
float offset = mix(-1.0f, 1.0f, gXOffset);
drawSpace();
drawParticles(offset);
drawLights();
}
// Display images and text with live wallpaper in the background
static void displayLiveWallPaper(int wResolution, int hResolution) {
bindProgramVertexOrtho();
drawGalaxy();
rsgBindProgramVertex(gProgVertex);
rsgBindProgramStore(gProgStoreBlendAlpha);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
drawMeshInPage(0, 0, wResolution, hResolution);
drawMeshInPage(-1.0f*gRenderSurfaceW, 0, wResolution, hResolution);
drawMeshInPage(1.0f*gRenderSurfaceW, 0, wResolution, hResolution);
drawMeshInPage(-2.0f*gRenderSurfaceW, 0, wResolution, hResolution);
drawMeshInPage(2.0f*gRenderSurfaceW, 0, wResolution, hResolution);
}
static float gTorusRotation = 0;
static void updateModelMatrix(rs_matrix4x4 *matrix, void *buffer) {
if (buffer == 0) {
rsgProgramVertexLoadModelMatrix(matrix);
} else {
rsgAllocationSyncAll(rsGetAllocation(buffer));
}
}
static void drawToruses(int numMeshes, rs_matrix4x4 *matrix, void *buffer) {
if (numMeshes == 1) {
rsMatrixLoadTranslate(matrix, 0.0f, 0.0f, -7.5f);
rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
updateModelMatrix(matrix, buffer);
rsgDrawMesh(gTorusMesh);
return;
}
if (numMeshes == 2) {
rsMatrixLoadTranslate(matrix, -1.6f, 0.0f, -7.5f);
rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
updateModelMatrix(matrix, buffer);
rsgDrawMesh(gTorusMesh);
rsMatrixLoadTranslate(matrix, 1.6f, 0.0f, -7.5f);
rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
updateModelMatrix(matrix, buffer);
rsgDrawMesh(gTorusMesh);
return;
}
float startX = -5.0f;
float startY = -1.5f;
float startZ = -15.0f;
float dist = 3.2f;
for (int h = 0; h < 4; h ++) {
for (int v = 0; v < 2; v ++) {
// Position our model on the screen
rsMatrixLoadTranslate(matrix, startX + dist * h, startY + dist * v, startZ);
rsMatrixRotate(matrix, gTorusRotation, 1.0f, 0.0f, 0.0f);
updateModelMatrix(matrix, buffer);
rsgDrawMesh(gTorusMesh);
}
}
}
// Quick hack to get some geometry numbers
static void displaySimpleGeoSamples(bool useTexture, int numMeshes) {
rsgBindProgramVertex(gProgVertex);
rsgBindProgramRaster(gCullBack);
// Setup the projection matrix with 30 degree field of view
rs_matrix4x4 proj;
float aspect = (float)gRenderSurfaceW / (float)gRenderSurfaceH;
rsMatrixLoadPerspective(&proj, 30.0f, aspect, 0.1f, 100.0f);
rsgProgramVertexLoadProjectionMatrix(&proj);
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNoneDepth);
if (useTexture) {
rsgBindProgramFragment(gProgFragmentTexture);
} else {
rsgBindProgramFragment(gProgFragmentColor);
rsgProgramFragmentConstantColor(gProgFragmentColor, 0.1, 0.7, 0.1, 1);
}
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
rsgBindTexture(gProgFragmentTexture, 0, gTexTorus);
// Apply a rotation to our mesh
gTorusRotation += 50.0f * gDt;
if (gTorusRotation > 360.0f) {
gTorusRotation -= 360.0f;
}
rs_matrix4x4 matrix;
drawToruses(numMeshes, &matrix, 0);
}
float gLight0Rotation = 0;
float gLight1Rotation = 0;
static void setupCustomShaderLights() {
float4 light0Pos = {-5.0f, 5.0f, -10.0f, 1.0f};
float4 light1Pos = {2.0f, 5.0f, 15.0f, 1.0f};
float4 light0DiffCol = {0.9f, 0.7f, 0.7f, 1.0f};
float4 light0SpecCol = {0.9f, 0.6f, 0.6f, 1.0f};
float4 light1DiffCol = {0.5f, 0.5f, 0.9f, 1.0f};
float4 light1SpecCol = {0.5f, 0.5f, 0.9f, 1.0f};
gLight0Rotation += 50.0f * gDt;
if (gLight0Rotation > 360.0f) {
gLight0Rotation -= 360.0f;
}
gLight1Rotation -= 50.0f * gDt;
if (gLight1Rotation > 360.0f) {
gLight1Rotation -= 360.0f;
}
rs_matrix4x4 l0Mat;
rsMatrixLoadRotate(&l0Mat, gLight0Rotation, 1.0f, 0.0f, 0.0f);
light0Pos = rsMatrixMultiply(&l0Mat, light0Pos);
rs_matrix4x4 l1Mat;
rsMatrixLoadRotate(&l1Mat, gLight1Rotation, 0.0f, 0.0f, 1.0f);
light1Pos = rsMatrixMultiply(&l1Mat, light1Pos);
// Set light 0 properties
gVSConstants->light0_Posision = light0Pos;
gVSConstants->light0_Diffuse = 1.0f;
gVSConstants->light0_Specular = 0.5f;
gVSConstants->light0_CosinePower = 10.0f;
// Set light 1 properties
gVSConstants->light1_Posision = light1Pos;
gVSConstants->light1_Diffuse = 1.0f;
gVSConstants->light1_Specular = 0.7f;
gVSConstants->light1_CosinePower = 25.0f;
rsgAllocationSyncAll(rsGetAllocation(gVSConstants));
// Update fragment shader constants
// Set light 0 colors
gFSConstants->light0_DiffuseColor = light0DiffCol;
gFSConstants->light0_SpecularColor = light0SpecCol;
// Set light 1 colors
gFSConstants->light1_DiffuseColor = light1DiffCol;
gFSConstants->light1_SpecularColor = light1SpecCol;
rsgAllocationSyncAll(rsGetAllocation(gFSConstants));
// Set light 0 properties for per pixel lighting
gFSConstPixel->light0_Posision = light0Pos;
gFSConstPixel->light0_Diffuse = 1.0f;
gFSConstPixel->light0_Specular = 0.5f;
gFSConstPixel->light0_CosinePower = 10.0f;
gFSConstPixel->light0_DiffuseColor = light0DiffCol;
gFSConstPixel->light0_SpecularColor = light0SpecCol;
// Set light 1 properties
gFSConstPixel->light1_Posision = light1Pos;
gFSConstPixel->light1_Diffuse = 1.0f;
gFSConstPixel->light1_Specular = 0.7f;
gFSConstPixel->light1_CosinePower = 25.0f;
gFSConstPixel->light1_DiffuseColor = light1DiffCol;
gFSConstPixel->light1_SpecularColor = light1SpecCol;
rsgAllocationSyncAll(rsGetAllocation(gFSConstPixel));
}
static void displayCustomShaderSamples(int numMeshes) {
// Update vertex shader constants
// Load model matrix
// Apply a rotation to our mesh
gTorusRotation += 50.0f * gDt;
if (gTorusRotation > 360.0f) {
gTorusRotation -= 360.0f;
}
// Setup the projection matrix
float aspect = (float)gRenderSurfaceW / (float)gRenderSurfaceH;
rsMatrixLoadPerspective(&gVSConstants->proj, 30.0f, aspect, 0.1f, 100.0f);
setupCustomShaderLights();
rsgBindProgramVertex(gProgVertexCustom);
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentCustom);
rsgBindSampler(gProgFragmentCustom, 0, gLinearClamp);
rsgBindTexture(gProgFragmentCustom, 0, gTexTorus);
// Use back face culling
rsgBindProgramRaster(gCullBack);
drawToruses(numMeshes, &gVSConstants->model, gVSConstants);
}
static void displayPixelLightSamples(int numMeshes, bool heavyVertex) {
// Update vertex shader constants
// Load model matrix
// Apply a rotation to our mesh
gTorusRotation += 30.0f * gDt;
if (gTorusRotation > 360.0f) {
gTorusRotation -= 360.0f;
}
gVSConstPixel->time = rsUptimeMillis()*0.005;
// Setup the projection matrix
float aspect = (float)gRenderSurfaceW / (float)gRenderSurfaceH;
rsMatrixLoadPerspective(&gVSConstPixel->proj, 30.0f, aspect, 0.1f, 100.0f);
setupCustomShaderLights();
if (heavyVertex) {
rsgBindProgramVertex(gProgVertexPixelLightMove);
} else {
rsgBindProgramVertex(gProgVertexPixelLight);
}
// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentPixelLight);
rsgBindSampler(gProgFragmentPixelLight, 0, gLinearClamp);
rsgBindTexture(gProgFragmentPixelLight, 0, gTexTorus);
// Use back face culling
rsgBindProgramRaster(gCullBack);
drawToruses(numMeshes, &gVSConstPixel->model, gVSConstPixel);
}
static void displayMultitextureSample(bool blend, int quadCount) {
bindProgramVertexOrtho();
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsgProgramVertexLoadModelMatrix(&matrix);
// Fragment shader with texture
if (!blend) {
rsgBindProgramStore(gProgStoreBlendNone);
} else {
rsgBindProgramStore(gProgStoreBlendAlpha);
}
rsgBindProgramFragment(gProgFragmentMultitex);
rsgBindSampler(gProgFragmentMultitex, 0, gLinearClamp);
rsgBindSampler(gProgFragmentMultitex, 1, gLinearWrap);
rsgBindSampler(gProgFragmentMultitex, 2, gLinearClamp);
rsgBindTexture(gProgFragmentMultitex, 0, gTexChecker);
rsgBindTexture(gProgFragmentMultitex, 1, gTexTorus);
rsgBindTexture(gProgFragmentMultitex, 2, gTexTransparent);
for (int i = 0; i < quadCount; i ++) {
float startX = 10 * i, startY = 10 * i;
float width = gRenderSurfaceW - startX, height = gRenderSurfaceH - startY;
rsgDrawQuadTexCoords(startX, startY, 0, 0, 0,
startX, startY + height, 0, 0, 1,
startX + width, startY + height, 0, 1, 1,
startX + width, startY, 0, 1, 0);
}
}
static bool checkInit() {
static int countdown = 5;
// Perform all the uploads so we only measure rendered time
if(countdown > 1) {
displayFontSamples(5);
displaySingletexFill(true, 3);
displayMeshSamples(0);
displayMeshSamples(1);
displayMeshSamples(2);
displayMultitextureSample(true, 5);
displayPixelLightSamples(1, false);
displayPixelLightSamples(1, true);
countdown --;
rsgClearColor(0.2f, 0.2f, 0.2f, 0.0f);
rsgFontColor(0.9f, 0.9f, 0.95f, 1.0f);
rsgBindFont(gFontSerif);
if (countdown == 1) {
rsgDrawText("Rendering", 50, 50);
} else {
rsgDrawText("Initializing", 50, 50);
}
return false;
}
return true;
}
static int benchMode = 0;
static int runningLoops = 0;
static bool sendMsgFlag = false;
static const char *testNames[] = {
"Fill screen with text 1 time",
"Fill screen with text 3 times",
"Fill screen with text 5 times",
"Geo test 25.6k flat color",
"Geo test 51.2k flat color",
"Geo test 204.8k small tries flat color",
"Geo test 25.6k single texture",
"Geo test 51.2k single texture",
"Geo test 204.8k small tries single texture",
"Full screen mesh 10 by 10",
"Full screen mesh 100 by 100",
"Full screen mesh W / 4 by H / 4",
"Geo test 25.6k geo heavy vertex",
"Geo test 51.2k geo heavy vertex",
"Geo test 204.8k geo raster load heavy vertex",
"Fill screen 10x singletexture",
"Fill screen 10x 3tex multitexture",
"Fill screen 10x blended singletexture",
"Fill screen 10x blended 3tex multitexture",
"Geo test 25.6k heavy fragment",
"Geo test 51.2k heavy fragment",
"Geo test 204.8k small tries heavy fragment",
"Geo test 25.6k heavy fragment heavy vertex",
"Geo test 51.2k heavy fragment heavy vertex",
"Geo test 204.8k small tries heavy fragment heavy vertex",
"UI test with icon display 10 by 10",
"UI test with icon display 100 by 100",
"UI test with image and text display 3 pages",
"UI test with image and text display 5 pages",
"UI test with list view",
"UI test with live wallpaper",
};
void getTestName(int testIndex) {
int bufferLen = rsAllocationGetDimX(rsGetAllocation(gStringBuffer));
if (testIndex >= gMaxModes) {
return;
}
uint charIndex = 0;
while (testNames[testIndex][charIndex] != '\0' && charIndex < bufferLen) {
gStringBuffer[charIndex] = testNames[testIndex][charIndex];
charIndex ++;
}
gStringBuffer[charIndex] = '\0';
}
static void runTest(int index) {
switch (index) {
case 0:
displayFontSamples(1);
break;
case 1:
displayFontSamples(3);
break;
case 2:
displayFontSamples(5);
break;
case 3:
displaySimpleGeoSamples(false, 1);
break;
case 4:
displaySimpleGeoSamples(false, 2);
break;
case 5:
displaySimpleGeoSamples(false, 8);
break;
case 6:
displaySimpleGeoSamples(true, 1);
break;
case 7:
displaySimpleGeoSamples(true, 2);
break;
case 8:
displaySimpleGeoSamples(true, 8);
break;
case 9:
displayMeshSamples(0);
break;
case 10:
displayMeshSamples(1);
break;
case 11:
displayMeshSamples(2);
break;
case 12:
displayCustomShaderSamples(1);
break;
case 13:
displayCustomShaderSamples(2);
break;
case 14:
displayCustomShaderSamples(10);
break;
case 15:
displaySingletexFill(false, 10);
break;
case 16:
displayMultitextureSample(false, 10);
break;
case 17:
displaySingletexFill(true, 10);
break;
case 18:
displayMultitextureSample(true, 8);
break;
case 19:
displayPixelLightSamples(1, false);
break;
case 20:
displayPixelLightSamples(2, false);
break;
case 21:
displayPixelLightSamples(8, false);
break;
case 22:
displayPixelLightSamples(1, true);
break;
case 23:
displayPixelLightSamples(2, true);
break;
case 24:
displayPixelLightSamples(8, true);
break;
case 25:
displayIcons(0);
break;
case 26:
displayIcons(1);
break;
case 27:
displayImageWithText(7, 5, 0);
break;
case 28:
displayImageWithText(7, 5, 1);
break;
case 29:
displayListView();
break;
case 30:
displayLiveWallPaper(7, 5);
break;
}
}
static void drawOffscreenResult(int posX, int posY, int width, int height) {
bindProgramVertexOrtho();
rs_matrix4x4 matrix;
rsMatrixLoadIdentity(&matrix);
rsgProgramVertexLoadModelMatrix(&matrix);
rsgBindProgramFragment(gProgFragmentTexture);
rsgBindSampler(gProgFragmentTexture, 0, gLinearClamp);
rsgBindTexture(gProgFragmentTexture, 0, gRenderBufferColor);
float startX = posX, startY = posY;
rsgDrawQuadTexCoords(startX, startY, 0, 0, 1,
startX, startY + height, 0, 0, 0,
startX + width, startY + height, 0, 1, 0,
startX + width, startY, 0, 1, 1);
}
int root(void) {
gRenderSurfaceW = rsgGetWidth();
gRenderSurfaceH = rsgGetHeight();
rsgClearColor(0.2f, 0.2f, 0.2f, 1.0f);
rsgClearDepth(1.0f);
if(!checkInit()) {
return 1;
}
gDt = 1.0f / 60.0f;
rsgFinish();
int64_t start = rsUptimeMillis();
int drawPos = 0;
int frameCount = 100;
for(int i = 0; i < frameCount; i ++) {
setupOffscreenTarget();
gRenderSurfaceW = rsAllocationGetDimX(gRenderBufferColor);
gRenderSurfaceH = rsAllocationGetDimY(gRenderBufferColor);
rsgClearColor(0.1f, 0.1f, 0.1f, 1.0f);
rsgClearDepth(1.0f);
runTest(benchMode);
rsgClearAllRenderTargets();
gRenderSurfaceW = rsgGetWidth();
gRenderSurfaceH = rsgGetHeight();
int size = 8;
// draw each frame at (8, 3/4 gRenderSurfaceH) with size
drawOffscreenResult((drawPos+=size)%gRenderSurfaceW, (gRenderSurfaceH * 3) / 4, size, size);
}
rsgFinish();
int64_t end = rsUptimeMillis();
float fps = (float)(frameCount) / ((float)(end - start)*0.001f);
rsDebug(testNames[benchMode], fps);
gResultBuffer[benchMode] = fps;
drawOffscreenResult(0, 0,
gRenderSurfaceW / 2,
gRenderSurfaceH / 2);
const char* text = testNames[benchMode];
int left = 0, right = 0, top = 0, bottom = 0;
uint width = rsgGetWidth();
uint height = rsgGetHeight();
rsgFontColor(0.9f, 0.9f, 0.95f, 1.0f);
rsgBindFont(gFontSerif);
rsgMeasureText(text, &left, &right, &top, &bottom);
rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
rsgDrawText(text, 2 -left, height - 2 + bottom);
benchMode ++;
gTorusRotation = 0;
if (benchMode == gMaxModes) {
rsSendToClientBlocking(RS_MSG_RESULTS_READY, gResultBuffer, gMaxModes*sizeof(float));
benchMode = 0;
runningLoops++;
if ((gMaxLoops > 0) && (runningLoops > gMaxLoops) && !sendMsgFlag) {
//Notifiy the test to stop and get results
rsDebug("gMaxLoops and runningLoops: ", gMaxLoops, runningLoops);
rsSendToClientBlocking(RS_MSG_TEST_DONE);
sendMsgFlag = true;
}
}
return 1;
}
|