-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
/
Copy pathrendering_server.cpp
2985 lines (2446 loc) · 146 KB
/
rendering_server.cpp
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
/**************************************************************************/
/* rendering_server.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "rendering_server.h"
#include "core/config/project_settings.h"
#include "core/object/worker_thread_pool.h"
#include "core/variant/typed_array.h"
#include "servers/rendering/rendering_server_globals.h"
#include "servers/rendering/shader_language.h"
RenderingServer *RenderingServer::singleton = nullptr;
RenderingServer *(*RenderingServer::create_func)() = nullptr;
RenderingServer *RenderingServer::get_singleton() {
return singleton;
}
RenderingServer *RenderingServer::create() {
ERR_FAIL_COND_V(singleton, nullptr);
if (create_func) {
return create_func();
}
return nullptr;
}
Array RenderingServer::_texture_debug_usage_bind() {
List<TextureInfo> list;
texture_debug_usage(&list);
Array arr;
for (const TextureInfo &E : list) {
Dictionary dict;
dict["texture"] = E.texture;
dict["width"] = E.width;
dict["height"] = E.height;
dict["depth"] = E.depth;
dict["format"] = E.format;
dict["bytes"] = E.bytes;
dict["path"] = E.path;
arr.push_back(dict);
}
return arr;
}
static PackedInt64Array to_int_array(const Vector<ObjectID> &ids) {
PackedInt64Array a;
a.resize(ids.size());
for (int i = 0; i < ids.size(); ++i) {
a.write[i] = ids[i];
}
return a;
}
PackedInt64Array RenderingServer::_instances_cull_aabb_bind(const AABB &p_aabb, RID p_scenario) const {
if (RSG::threaded) {
WARN_PRINT_ONCE("Using this function with a threaded renderer hurts performance, as it causes a server stall.");
}
Vector<ObjectID> ids = instances_cull_aabb(p_aabb, p_scenario);
return to_int_array(ids);
}
PackedInt64Array RenderingServer::_instances_cull_ray_bind(const Vector3 &p_from, const Vector3 &p_to, RID p_scenario) const {
if (RSG::threaded) {
WARN_PRINT_ONCE("Using this function with a threaded renderer hurts performance, as it causes a server stall.");
}
Vector<ObjectID> ids = instances_cull_ray(p_from, p_to, p_scenario);
return to_int_array(ids);
}
PackedInt64Array RenderingServer::_instances_cull_convex_bind(const TypedArray<Plane> &p_convex, RID p_scenario) const {
if (RSG::threaded) {
WARN_PRINT_ONCE("Using this function with a threaded renderer hurts performance, as it causes a server stall.");
}
Vector<Plane> planes;
for (int i = 0; i < p_convex.size(); ++i) {
Variant v = p_convex[i];
ERR_FAIL_COND_V(v.get_type() != Variant::PLANE, PackedInt64Array());
planes.push_back(v);
}
Vector<ObjectID> ids = instances_cull_convex(planes, p_scenario);
return to_int_array(ids);
}
RID RenderingServer::get_test_texture() {
if (test_texture.is_valid()) {
return test_texture;
};
#define TEST_TEXTURE_SIZE 256
Vector<uint8_t> test_data;
test_data.resize(TEST_TEXTURE_SIZE * TEST_TEXTURE_SIZE * 3);
{
uint8_t *w = test_data.ptrw();
for (int x = 0; x < TEST_TEXTURE_SIZE; x++) {
for (int y = 0; y < TEST_TEXTURE_SIZE; y++) {
Color c;
int r = 255 - (x + y) / 2;
if ((x % (TEST_TEXTURE_SIZE / 8)) < 2 || (y % (TEST_TEXTURE_SIZE / 8)) < 2) {
c.r = y;
c.g = r;
c.b = x;
} else {
c.r = r;
c.g = x;
c.b = y;
}
w[(y * TEST_TEXTURE_SIZE + x) * 3 + 0] = uint8_t(CLAMP(c.r * 255, 0, 255));
w[(y * TEST_TEXTURE_SIZE + x) * 3 + 1] = uint8_t(CLAMP(c.g * 255, 0, 255));
w[(y * TEST_TEXTURE_SIZE + x) * 3 + 2] = uint8_t(CLAMP(c.b * 255, 0, 255));
}
}
}
Ref<Image> data = memnew(Image(TEST_TEXTURE_SIZE, TEST_TEXTURE_SIZE, false, Image::FORMAT_RGB8, test_data));
test_texture = texture_2d_create(data);
return test_texture;
}
void RenderingServer::_free_internal_rids() {
if (test_texture.is_valid()) {
free(test_texture);
}
if (white_texture.is_valid()) {
free(white_texture);
}
if (test_material.is_valid()) {
free(test_material);
}
}
RID RenderingServer::_make_test_cube() {
Vector<Vector3> vertices;
Vector<Vector3> normals;
Vector<float> tangents;
Vector<Vector3> uvs;
#define ADD_VTX(m_idx) \
vertices.push_back(face_points[m_idx]); \
normals.push_back(normal_points[m_idx]); \
tangents.push_back(normal_points[m_idx][1]); \
tangents.push_back(normal_points[m_idx][2]); \
tangents.push_back(normal_points[m_idx][0]); \
tangents.push_back(1.0); \
uvs.push_back(Vector3(uv_points[m_idx * 2 + 0], uv_points[m_idx * 2 + 1], 0));
for (int i = 0; i < 6; i++) {
Vector3 face_points[4];
Vector3 normal_points[4];
float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
for (int j = 0; j < 4; j++) {
float v[3];
v[0] = 1.0;
v[1] = 1 - 2 * ((j >> 1) & 1);
v[2] = v[1] * (1 - 2 * (j & 1));
for (int k = 0; k < 3; k++) {
if (i < 3) {
face_points[j][(i + k) % 3] = v[k];
} else {
face_points[3 - j][(i + k) % 3] = -v[k];
}
}
normal_points[j] = Vector3();
normal_points[j][i % 3] = (i >= 3 ? -1 : 1);
}
// Tri 1
ADD_VTX(0);
ADD_VTX(1);
ADD_VTX(2);
// Tri 2
ADD_VTX(2);
ADD_VTX(3);
ADD_VTX(0);
}
RID test_cube = mesh_create();
Array d;
d.resize(RS::ARRAY_MAX);
d[RenderingServer::ARRAY_NORMAL] = normals;
d[RenderingServer::ARRAY_TANGENT] = tangents;
d[RenderingServer::ARRAY_TEX_UV] = uvs;
d[RenderingServer::ARRAY_VERTEX] = vertices;
Vector<int> indices;
indices.resize(vertices.size());
for (int i = 0; i < vertices.size(); i++) {
indices.set(i, i);
}
d[RenderingServer::ARRAY_INDEX] = indices;
mesh_add_surface_from_arrays(test_cube, PRIMITIVE_TRIANGLES, d);
/*
test_material = fixed_material_create();
//material_set_flag(material, MATERIAL_FLAG_BILLBOARD_TOGGLE,true);
fixed_material_set_texture( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, get_test_texture() );
fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR_EXP, 70 );
fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_EMISSION, Color(0.2,0.2,0.2) );
fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_DIFFUSE, Color(1, 1, 1) );
fixed_material_set_param( test_material, FIXED_MATERIAL_PARAM_SPECULAR, Color(1,1,1) );
*/
mesh_surface_set_material(test_cube, 0, test_material);
return test_cube;
}
RID RenderingServer::make_sphere_mesh(int p_lats, int p_lons, real_t p_radius) {
Vector<Vector3> vertices;
Vector<Vector3> normals;
const double lat_step = Math_TAU / p_lats;
const double lon_step = Math_TAU / p_lons;
for (int i = 1; i <= p_lats; i++) {
double lat0 = lat_step * (i - 1) - Math_TAU / 4;
double z0 = Math::sin(lat0);
double zr0 = Math::cos(lat0);
double lat1 = lat_step * i - Math_TAU / 4;
double z1 = Math::sin(lat1);
double zr1 = Math::cos(lat1);
for (int j = p_lons; j >= 1; j--) {
double lng0 = lon_step * (j - 1);
double x0 = Math::cos(lng0);
double y0 = Math::sin(lng0);
double lng1 = lon_step * j;
double x1 = Math::cos(lng1);
double y1 = Math::sin(lng1);
Vector3 v[4] = {
Vector3(x1 * zr0, z0, y1 * zr0),
Vector3(x1 * zr1, z1, y1 * zr1),
Vector3(x0 * zr1, z1, y0 * zr1),
Vector3(x0 * zr0, z0, y0 * zr0)
};
#define ADD_POINT(m_idx) \
normals.push_back(v[m_idx]); \
vertices.push_back(v[m_idx] * p_radius);
ADD_POINT(0);
ADD_POINT(1);
ADD_POINT(2);
ADD_POINT(2);
ADD_POINT(3);
ADD_POINT(0);
}
}
RID mesh = mesh_create();
Array d;
d.resize(RS::ARRAY_MAX);
d[ARRAY_VERTEX] = vertices;
d[ARRAY_NORMAL] = normals;
mesh_add_surface_from_arrays(mesh, PRIMITIVE_TRIANGLES, d);
return mesh;
}
RID RenderingServer::get_white_texture() {
if (white_texture.is_valid()) {
return white_texture;
}
Vector<uint8_t> wt;
wt.resize(16 * 3);
{
uint8_t *w = wt.ptrw();
for (int i = 0; i < 16 * 3; i++) {
w[i] = 255;
}
}
Ref<Image> white = memnew(Image(4, 4, 0, Image::FORMAT_RGB8, wt));
white_texture = texture_2d_create(white);
return white_texture;
}
Error RenderingServer::_surface_set_data(Array p_arrays, uint32_t p_format, uint32_t *p_offsets, uint32_t p_vertex_stride, uint32_t p_attrib_stride, uint32_t p_skin_stride, Vector<uint8_t> &r_vertex_array, Vector<uint8_t> &r_attrib_array, Vector<uint8_t> &r_skin_array, int p_vertex_array_len, Vector<uint8_t> &r_index_array, int p_index_array_len, AABB &r_aabb, Vector<AABB> &r_bone_aabb) {
uint8_t *vw = r_vertex_array.ptrw();
uint8_t *aw = r_attrib_array.ptrw();
uint8_t *sw = r_skin_array.ptrw();
uint8_t *iw = nullptr;
if (r_index_array.size()) {
iw = r_index_array.ptrw();
}
int max_bone = 0;
for (int ai = 0; ai < RS::ARRAY_MAX; ai++) {
if (!(p_format & (1 << ai))) { // No array
continue;
}
switch (ai) {
case RS::ARRAY_VERTEX: {
if (p_format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
Vector<Vector2> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Vector2 *src = array.ptr();
// Setting vertices means regenerating the AABB.
Rect2 aabb;
{
for (int i = 0; i < p_vertex_array_len; i++) {
float vector[2] = { (float)src[i].x, (float)src[i].y };
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, sizeof(float) * 2);
if (i == 0) {
aabb = Rect2(src[i], SMALL_VEC2); // Must have a bit of size.
} else {
aabb.expand_to(src[i]);
}
}
}
r_aabb = AABB(Vector3(aabb.position.x, aabb.position.y, 0), Vector3(aabb.size.x, aabb.size.y, 0));
} else {
Vector<Vector3> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Vector3 *src = array.ptr();
// Setting vertices means regenerating the AABB.
AABB aabb;
{
for (int i = 0; i < p_vertex_array_len; i++) {
float vector[3] = { (float)src[i].x, (float)src[i].y, (float)src[i].z };
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, sizeof(float) * 3);
if (i == 0) {
aabb = AABB(src[i], SMALL_VEC3);
} else {
aabb.expand_to(src[i]);
}
}
}
r_aabb = aabb;
}
} break;
case RS::ARRAY_NORMAL: {
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_VECTOR3_ARRAY, ERR_INVALID_PARAMETER);
Vector<Vector3> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Vector3 *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
Vector2 res = src[i].octahedron_encode();
int16_t vector[2] = {
(int16_t)CLAMP(res.x * 65535, 0, 65535),
(int16_t)CLAMP(res.y * 65535, 0, 65535),
};
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, 4);
}
} break;
case RS::ARRAY_TANGENT: {
Variant::Type type = p_arrays[ai].get_type();
ERR_FAIL_COND_V(type != Variant::PACKED_FLOAT32_ARRAY && type != Variant::PACKED_FLOAT64_ARRAY, ERR_INVALID_PARAMETER);
if (type == Variant::PACKED_FLOAT32_ARRAY) {
Vector<float> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 4, ERR_INVALID_PARAMETER);
const float *src_ptr = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
const Vector3 src(src_ptr[i * 4 + 0], src_ptr[i * 4 + 1], src_ptr[i * 4 + 2]);
Vector2 res = src.octahedron_tangent_encode(src_ptr[i * 4 + 3]);
int16_t vector[2] = {
(int16_t)CLAMP(res.x * 65535, 0, 65535),
(int16_t)CLAMP(res.y * 65535, 0, 65535),
};
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, 4);
}
} else { // PACKED_FLOAT64_ARRAY
Vector<double> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 4, ERR_INVALID_PARAMETER);
const double *src_ptr = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
const Vector3 src(src_ptr[i * 4 + 0], src_ptr[i * 4 + 1], src_ptr[i * 4 + 2]);
Vector2 res = src.octahedron_tangent_encode(src_ptr[i * 4 + 3]);
int16_t vector[2] = {
(int16_t)CLAMP(res.x * 65535, 0, 65535),
(int16_t)CLAMP(res.y * 65535, 0, 65535),
};
memcpy(&vw[p_offsets[ai] + i * p_vertex_stride], vector, 4);
}
}
} break;
case RS::ARRAY_COLOR: {
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_COLOR_ARRAY, ERR_INVALID_PARAMETER);
Vector<Color> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Color *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
uint8_t color8[4] = {
uint8_t(CLAMP(src[i].r * 255.0, 0.0, 255.0)),
uint8_t(CLAMP(src[i].g * 255.0, 0.0, 255.0)),
uint8_t(CLAMP(src[i].b * 255.0, 0.0, 255.0)),
uint8_t(CLAMP(src[i].a * 255.0, 0.0, 255.0))
};
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], color8, 4);
}
} break;
case RS::ARRAY_TEX_UV: {
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_VECTOR3_ARRAY && p_arrays[ai].get_type() != Variant::PACKED_VECTOR2_ARRAY, ERR_INVALID_PARAMETER);
Vector<Vector2> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Vector2 *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
float uv[2] = { (float)src[i].x, (float)src[i].y };
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], uv, 2 * 4);
}
} break;
case RS::ARRAY_TEX_UV2: {
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_VECTOR3_ARRAY && p_arrays[ai].get_type() != Variant::PACKED_VECTOR2_ARRAY, ERR_INVALID_PARAMETER);
Vector<Vector2> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len, ERR_INVALID_PARAMETER);
const Vector2 *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
float uv[2] = { (float)src[i].x, (float)src[i].y };
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], uv, 2 * 4);
}
} break;
case RS::ARRAY_CUSTOM0:
case RS::ARRAY_CUSTOM1:
case RS::ARRAY_CUSTOM2:
case RS::ARRAY_CUSTOM3: {
uint32_t type = (p_format >> (ARRAY_FORMAT_CUSTOM_BASE + ARRAY_FORMAT_CUSTOM_BITS * (ai - RS::ARRAY_CUSTOM0))) & ARRAY_FORMAT_CUSTOM_MASK;
switch (type) {
case ARRAY_CUSTOM_RGBA8_UNORM:
case ARRAY_CUSTOM_RGBA8_SNORM:
case ARRAY_CUSTOM_RG_HALF: {
// Size 4
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_BYTE_ARRAY, ERR_INVALID_PARAMETER);
Vector<uint8_t> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 4, ERR_INVALID_PARAMETER);
const uint8_t *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], &src[i * 4], 4);
}
} break;
case ARRAY_CUSTOM_RGBA_HALF: {
// Size 8
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_BYTE_ARRAY, ERR_INVALID_PARAMETER);
Vector<uint8_t> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != p_vertex_array_len * 8, ERR_INVALID_PARAMETER);
const uint8_t *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], &src[i * 8], 8);
}
} break;
case ARRAY_CUSTOM_R_FLOAT:
case ARRAY_CUSTOM_RG_FLOAT:
case ARRAY_CUSTOM_RGB_FLOAT:
case ARRAY_CUSTOM_RGBA_FLOAT: {
// RF
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_FLOAT32_ARRAY, ERR_INVALID_PARAMETER);
Vector<float> array = p_arrays[ai];
int32_t s = type - ARRAY_CUSTOM_R_FLOAT + 1;
ERR_FAIL_COND_V(array.size() != p_vertex_array_len * s, ERR_INVALID_PARAMETER);
const float *src = array.ptr();
for (int i = 0; i < p_vertex_array_len; i++) {
memcpy(&aw[p_offsets[ai] + i * p_attrib_stride], &src[i * s], sizeof(float) * s);
}
} break;
default: {
}
}
} break;
case RS::ARRAY_WEIGHTS: {
Variant::Type type = p_arrays[ai].get_type();
ERR_FAIL_COND_V(type != Variant::PACKED_FLOAT32_ARRAY && type != Variant::PACKED_FLOAT64_ARRAY, ERR_INVALID_PARAMETER);
uint32_t bone_count = (p_format & ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4;
if (type == Variant::PACKED_FLOAT32_ARRAY) {
Vector<float> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != (int32_t)(p_vertex_array_len * bone_count), ERR_INVALID_PARAMETER);
const float *src = array.ptr();
{
uint16_t data[8];
for (int i = 0; i < p_vertex_array_len; i++) {
for (uint32_t j = 0; j < bone_count; j++) {
data[j] = CLAMP(src[i * bone_count + j] * 65535, 0, 65535);
}
memcpy(&sw[p_offsets[ai] + i * p_skin_stride], data, 2 * bone_count);
}
}
} else { // PACKED_FLOAT64_ARRAY
Vector<double> array = p_arrays[ai];
ERR_FAIL_COND_V(array.size() != (int32_t)(p_vertex_array_len * bone_count), ERR_INVALID_PARAMETER);
const double *src = array.ptr();
{
uint16_t data[8];
for (int i = 0; i < p_vertex_array_len; i++) {
for (uint32_t j = 0; j < bone_count; j++) {
data[j] = CLAMP(src[i * bone_count + j] * 65535, 0, 65535);
}
memcpy(&sw[p_offsets[ai] + i * p_skin_stride], data, 2 * bone_count);
}
}
}
} break;
case RS::ARRAY_BONES: {
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_INT32_ARRAY && p_arrays[ai].get_type() != Variant::PACKED_FLOAT32_ARRAY, ERR_INVALID_PARAMETER);
Vector<int> array = p_arrays[ai];
uint32_t bone_count = (p_format & ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4;
ERR_FAIL_COND_V(array.size() != (int32_t)(p_vertex_array_len * bone_count), ERR_INVALID_PARAMETER);
const int *src = array.ptr();
uint16_t data[8];
for (int i = 0; i < p_vertex_array_len; i++) {
for (uint32_t j = 0; j < bone_count; j++) {
data[j] = src[i * bone_count + j];
max_bone = MAX(data[j], max_bone);
}
memcpy(&sw[p_offsets[ai] + i * p_skin_stride], data, 2 * bone_count);
}
} break;
case RS::ARRAY_INDEX: {
ERR_FAIL_NULL_V(iw, ERR_INVALID_DATA);
ERR_FAIL_COND_V(p_index_array_len <= 0, ERR_INVALID_DATA);
ERR_FAIL_COND_V(p_arrays[ai].get_type() != Variant::PACKED_INT32_ARRAY, ERR_INVALID_PARAMETER);
Vector<int> indices = p_arrays[ai];
ERR_FAIL_COND_V(indices.size() == 0, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(indices.size() != p_index_array_len, ERR_INVALID_PARAMETER);
/* determine whether using 16 or 32 bits indices */
const int *src = indices.ptr();
for (int i = 0; i < p_index_array_len; i++) {
if (p_vertex_array_len <= (1 << 16) && p_vertex_array_len > 0) {
uint16_t v = src[i];
memcpy(&iw[i * 2], &v, 2);
} else {
uint32_t v = src[i];
memcpy(&iw[i * 4], &v, 4);
}
}
} break;
default: {
ERR_FAIL_V(ERR_INVALID_DATA);
}
}
}
if (p_format & RS::ARRAY_FORMAT_BONES) {
// Create AABBs for each detected bone.
int total_bones = max_bone + 1;
bool first = r_bone_aabb.size() == 0;
r_bone_aabb.resize(total_bones);
int weight_count = (p_format & ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4;
if (first) {
for (int i = 0; i < total_bones; i++) {
r_bone_aabb.write[i].size = Vector3(-1, -1, -1); // Negative means unused.
}
}
Vector<Vector3> vertices = p_arrays[RS::ARRAY_VERTEX];
Vector<int> bones = p_arrays[RS::ARRAY_BONES];
Vector<float> weights = p_arrays[RS::ARRAY_WEIGHTS];
bool any_valid = false;
if (vertices.size() && bones.size() == vertices.size() * weight_count && weights.size() == bones.size()) {
int vs = vertices.size();
const Vector3 *rv = vertices.ptr();
const int *rb = bones.ptr();
const float *rw = weights.ptr();
AABB *bptr = r_bone_aabb.ptrw();
for (int i = 0; i < vs; i++) {
Vector3 v = rv[i];
for (int j = 0; j < weight_count; j++) {
int idx = rb[i * weight_count + j];
float w = rw[i * weight_count + j];
if (w == 0) {
continue; //break;
}
ERR_FAIL_INDEX_V(idx, total_bones, ERR_INVALID_DATA);
if (bptr[idx].size.x < 0) {
// First
bptr[idx] = AABB(v, SMALL_VEC3);
any_valid = true;
} else {
bptr[idx].expand_to(v);
}
}
}
}
if (!any_valid && first) {
r_bone_aabb.clear();
}
}
return OK;
}
uint32_t RenderingServer::mesh_surface_get_format_offset(BitField<ArrayFormat> p_format, int p_vertex_len, int p_array_index) const {
ERR_FAIL_INDEX_V(p_array_index, ARRAY_MAX, 0);
p_format = int64_t(p_format) & ~ARRAY_FORMAT_INDEX;
uint32_t offsets[ARRAY_MAX];
uint32_t vstr;
uint32_t astr;
uint32_t sstr;
mesh_surface_make_offsets_from_format(p_format, p_vertex_len, 0, offsets, vstr, astr, sstr);
return offsets[p_array_index];
}
uint32_t RenderingServer::mesh_surface_get_format_vertex_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const {
p_format = int64_t(p_format) & ~ARRAY_FORMAT_INDEX;
uint32_t offsets[ARRAY_MAX];
uint32_t vstr;
uint32_t astr;
uint32_t sstr;
mesh_surface_make_offsets_from_format(p_format, p_vertex_len, 0, offsets, vstr, astr, sstr);
return vstr;
}
uint32_t RenderingServer::mesh_surface_get_format_attribute_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const {
p_format = int64_t(p_format) & ~ARRAY_FORMAT_INDEX;
uint32_t offsets[ARRAY_MAX];
uint32_t vstr;
uint32_t astr;
uint32_t sstr;
mesh_surface_make_offsets_from_format(p_format, p_vertex_len, 0, offsets, vstr, astr, sstr);
return astr;
}
uint32_t RenderingServer::mesh_surface_get_format_skin_stride(BitField<ArrayFormat> p_format, int p_vertex_len) const {
p_format = int64_t(p_format) & ~ARRAY_FORMAT_INDEX;
uint32_t offsets[ARRAY_MAX];
uint32_t vstr;
uint32_t astr;
uint32_t sstr;
mesh_surface_make_offsets_from_format(p_format, p_vertex_len, 0, offsets, vstr, astr, sstr);
return sstr;
}
void RenderingServer::mesh_surface_make_offsets_from_format(uint32_t p_format, int p_vertex_len, int p_index_len, uint32_t *r_offsets, uint32_t &r_vertex_element_size, uint32_t &r_attrib_element_size, uint32_t &r_skin_element_size) const {
r_vertex_element_size = 0;
r_attrib_element_size = 0;
r_skin_element_size = 0;
uint32_t *size_accum = nullptr;
for (int i = 0; i < RS::ARRAY_MAX; i++) {
r_offsets[i] = 0; // Reset
if (i == RS::ARRAY_VERTEX) {
size_accum = &r_vertex_element_size;
} else if (i == RS::ARRAY_COLOR) {
size_accum = &r_attrib_element_size;
} else if (i == RS::ARRAY_BONES) {
size_accum = &r_skin_element_size;
}
if (!(p_format & (1 << i))) { // No array
continue;
}
int elem_size = 0;
switch (i) {
case RS::ARRAY_VERTEX: {
if (p_format & ARRAY_FLAG_USE_2D_VERTICES) {
elem_size = 2;
} else {
elem_size = 3;
}
elem_size *= sizeof(float);
} break;
case RS::ARRAY_NORMAL: {
elem_size = 4;
} break;
case RS::ARRAY_TANGENT: {
elem_size = 4;
} break;
case RS::ARRAY_COLOR: {
elem_size = 4;
} break;
case RS::ARRAY_TEX_UV: {
elem_size = 8;
} break;
case RS::ARRAY_TEX_UV2: {
elem_size = 8;
} break;
case RS::ARRAY_CUSTOM0:
case RS::ARRAY_CUSTOM1:
case RS::ARRAY_CUSTOM2:
case RS::ARRAY_CUSTOM3: {
uint32_t format = (p_format >> (ARRAY_FORMAT_CUSTOM_BASE + (ARRAY_FORMAT_CUSTOM_BITS * (i - ARRAY_CUSTOM0)))) & ARRAY_FORMAT_CUSTOM_MASK;
switch (format) {
case ARRAY_CUSTOM_RGBA8_UNORM: {
elem_size = 4;
} break;
case ARRAY_CUSTOM_RGBA8_SNORM: {
elem_size = 4;
} break;
case ARRAY_CUSTOM_RG_HALF: {
elem_size = 4;
} break;
case ARRAY_CUSTOM_RGBA_HALF: {
elem_size = 8;
} break;
case ARRAY_CUSTOM_R_FLOAT: {
elem_size = 4;
} break;
case ARRAY_CUSTOM_RG_FLOAT: {
elem_size = 8;
} break;
case ARRAY_CUSTOM_RGB_FLOAT: {
elem_size = 12;
} break;
case ARRAY_CUSTOM_RGBA_FLOAT: {
elem_size = 16;
} break;
}
} break;
case RS::ARRAY_WEIGHTS: {
uint32_t bone_count = (p_format & ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4;
elem_size = sizeof(uint16_t) * bone_count;
} break;
case RS::ARRAY_BONES: {
uint32_t bone_count = (p_format & ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4;
elem_size = sizeof(uint16_t) * bone_count;
} break;
case RS::ARRAY_INDEX: {
if (p_index_len <= 0) {
ERR_PRINT("index_array_len==NO_INDEX_ARRAY");
break;
}
/* determine whether using 16 or 32 bits indices */
if (p_vertex_len <= (1 << 16) && p_vertex_len > 0) {
elem_size = 2;
} else {
elem_size = 4;
}
r_offsets[i] = elem_size;
continue;
}
default: {
ERR_FAIL();
}
}
if (size_accum != nullptr) {
r_offsets[i] = (*size_accum);
(*size_accum) += elem_size;
} else {
r_offsets[i] = 0;
}
}
}
Error RenderingServer::mesh_create_surface_data_from_arrays(SurfaceData *r_surface_data, PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, const Dictionary &p_lods, uint32_t p_compress_format) {
ERR_FAIL_INDEX_V(p_primitive, RS::PRIMITIVE_MAX, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_arrays.size() != RS::ARRAY_MAX, ERR_INVALID_PARAMETER);
uint32_t format = 0;
// Validation
int index_array_len = 0;
int array_len = 0;
for (int i = 0; i < p_arrays.size(); i++) {
if (p_arrays[i].get_type() == Variant::NIL) {
continue;
}
format |= (1 << i);
if (i == RS::ARRAY_VERTEX) {
switch (p_arrays[i].get_type()) {
case Variant::PACKED_VECTOR2_ARRAY: {
Vector<Vector2> v2 = p_arrays[i];
array_len = v2.size();
format |= ARRAY_FLAG_USE_2D_VERTICES;
} break;
case Variant::PACKED_VECTOR3_ARRAY: {
ERR_FAIL_COND_V(p_compress_format & ARRAY_FLAG_USE_2D_VERTICES, ERR_INVALID_PARAMETER);
Vector<Vector3> v3 = p_arrays[i];
array_len = v3.size();
} break;
default: {
ERR_FAIL_V(ERR_INVALID_DATA);
} break;
}
ERR_FAIL_COND_V(array_len == 0, ERR_INVALID_DATA);
} else if (i == RS::ARRAY_BONES) {
switch (p_arrays[i].get_type()) {
case Variant::PACKED_INT32_ARRAY: {
Vector<Vector3> vertices = p_arrays[RS::ARRAY_VERTEX];
Vector<int32_t> bones = p_arrays[i];
int32_t bone_8_group_count = bones.size() / (ARRAY_WEIGHTS_SIZE * 2);
int32_t vertex_count = vertices.size();
if (vertex_count == bone_8_group_count) {
format |= RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
}
} break;
default: {
ERR_FAIL_V(ERR_INVALID_DATA);
} break;
}
} else if (i == RS::ARRAY_INDEX) {
index_array_len = PackedInt32Array(p_arrays[i]).size();
}
}
if (p_blend_shapes.size()) {
// Validate format for morphs.
for (int i = 0; i < p_blend_shapes.size(); i++) {
uint32_t bsformat = 0;
Array arr = p_blend_shapes[i];
for (int j = 0; j < arr.size(); j++) {
if (arr[j].get_type() != Variant::NIL) {
bsformat |= (1 << j);
}
}
ERR_FAIL_COND_V_MSG(bsformat != (format & RS::ARRAY_FORMAT_BLEND_SHAPE_MASK), ERR_INVALID_PARAMETER, "Blend shape format must match the main array format for Vertex, Normal and Tangent arrays.");
}
}
for (uint32_t i = 0; i < RS::ARRAY_CUSTOM_COUNT; ++i) {
// Include custom array format type.
if (format & (1 << (ARRAY_CUSTOM0 + i))) {
format |= (RS::ARRAY_FORMAT_CUSTOM_MASK << (RS::ARRAY_FORMAT_CUSTOM_BASE + i * RS::ARRAY_FORMAT_CUSTOM_BITS)) & p_compress_format;
}
}
uint32_t offsets[RS::ARRAY_MAX];
uint32_t vertex_element_size;
uint32_t attrib_element_size;
uint32_t skin_element_size;
mesh_surface_make_offsets_from_format(format, array_len, index_array_len, offsets, vertex_element_size, attrib_element_size, skin_element_size);
uint32_t mask = (1 << ARRAY_MAX) - 1;
format |= (~mask) & p_compress_format; // Make the full format.
if ((format & RS::ARRAY_FORMAT_VERTEX) == 0 && !(format & RS::ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY)) {
ERR_PRINT("Mesh created without vertex array. This mesh will not be visible with the default shader. If using an empty vertex array is intentional, create the mesh with the ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY flag to silence this error.");
// Set the flag here after warning to suppress errors down the pipeline.
format |= RS::ARRAY_FLAG_USES_EMPTY_VERTEX_ARRAY;
}
int vertex_array_size = vertex_element_size * array_len;
int attrib_array_size = attrib_element_size * array_len;
int skin_array_size = skin_element_size * array_len;
int index_array_size = offsets[RS::ARRAY_INDEX] * index_array_len;
Vector<uint8_t> vertex_array;
vertex_array.resize(vertex_array_size);
Vector<uint8_t> attrib_array;
attrib_array.resize(attrib_array_size);
Vector<uint8_t> skin_array;
skin_array.resize(skin_array_size);
Vector<uint8_t> index_array;
index_array.resize(index_array_size);
AABB aabb;
Vector<AABB> bone_aabb;
Error err = _surface_set_data(p_arrays, format, offsets, vertex_element_size, attrib_element_size, skin_element_size, vertex_array, attrib_array, skin_array, array_len, index_array, index_array_len, aabb, bone_aabb);
ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Invalid array format for surface.");
Vector<uint8_t> blend_shape_data;
if (p_blend_shapes.size()) {
uint32_t bs_format = format & RS::ARRAY_FORMAT_BLEND_SHAPE_MASK;
for (int i = 0; i < p_blend_shapes.size(); i++) {
Vector<uint8_t> vertex_array_shape;
vertex_array_shape.resize(vertex_array_size);
Vector<uint8_t> noindex;
Vector<uint8_t> noattrib;
Vector<uint8_t> noskin;
AABB laabb;
Error err2 = _surface_set_data(p_blend_shapes[i], bs_format, offsets, vertex_element_size, 0, 0, vertex_array_shape, noattrib, noskin, array_len, noindex, 0, laabb, bone_aabb);
aabb.merge_with(laabb);
ERR_FAIL_COND_V_MSG(err2 != OK, ERR_INVALID_DATA, "Invalid blend shape array format for surface.");
blend_shape_data.append_array(vertex_array_shape);
}
}
Vector<SurfaceData::LOD> lods;
if (index_array_len) {
List<Variant> keys;
p_lods.get_key_list(&keys);
keys.sort(); // otherwise lod levels may get skipped