2
0

renderer_storage_rd.h 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  1. /*************************************************************************/
  2. /* renderer_storage_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RENDERING_SERVER_STORAGE_RD_H
  31. #define RENDERING_SERVER_STORAGE_RD_H
  32. #include "core/templates/list.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "servers/rendering/renderer_compositor.h"
  36. #include "servers/rendering/renderer_rd/effects_rd.h"
  37. #include "servers/rendering/renderer_rd/shader_compiler_rd.h"
  38. #include "servers/rendering/renderer_rd/shaders/canvas_sdf.glsl.gen.h"
  39. #include "servers/rendering/renderer_rd/shaders/giprobe_sdf.glsl.gen.h"
  40. #include "servers/rendering/renderer_rd/shaders/particles.glsl.gen.h"
  41. #include "servers/rendering/renderer_rd/shaders/particles_copy.glsl.gen.h"
  42. #include "servers/rendering/renderer_rd/shaders/skeleton.glsl.gen.h"
  43. #include "servers/rendering/renderer_scene_render.h"
  44. #include "servers/rendering/rendering_device.h"
  45. class RendererStorageRD : public RendererStorage {
  46. public:
  47. static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) {
  48. p_array[0] = p_mtx.basis.elements[0][0];
  49. p_array[1] = p_mtx.basis.elements[1][0];
  50. p_array[2] = p_mtx.basis.elements[2][0];
  51. p_array[3] = 0;
  52. p_array[4] = p_mtx.basis.elements[0][1];
  53. p_array[5] = p_mtx.basis.elements[1][1];
  54. p_array[6] = p_mtx.basis.elements[2][1];
  55. p_array[7] = 0;
  56. p_array[8] = p_mtx.basis.elements[0][2];
  57. p_array[9] = p_mtx.basis.elements[1][2];
  58. p_array[10] = p_mtx.basis.elements[2][2];
  59. p_array[11] = 0;
  60. p_array[12] = p_mtx.origin.x;
  61. p_array[13] = p_mtx.origin.y;
  62. p_array[14] = p_mtx.origin.z;
  63. p_array[15] = 1;
  64. }
  65. static _FORCE_INLINE_ void store_basis_3x4(const Basis &p_mtx, float *p_array) {
  66. p_array[0] = p_mtx.elements[0][0];
  67. p_array[1] = p_mtx.elements[1][0];
  68. p_array[2] = p_mtx.elements[2][0];
  69. p_array[3] = 0;
  70. p_array[4] = p_mtx.elements[0][1];
  71. p_array[5] = p_mtx.elements[1][1];
  72. p_array[6] = p_mtx.elements[2][1];
  73. p_array[7] = 0;
  74. p_array[8] = p_mtx.elements[0][2];
  75. p_array[9] = p_mtx.elements[1][2];
  76. p_array[10] = p_mtx.elements[2][2];
  77. p_array[11] = 0;
  78. }
  79. static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_mtx, float *p_array) {
  80. p_array[0] = p_mtx.elements[0][0];
  81. p_array[1] = p_mtx.elements[1][0];
  82. p_array[2] = p_mtx.elements[2][0];
  83. p_array[3] = 0;
  84. p_array[4] = p_mtx.elements[0][1];
  85. p_array[5] = p_mtx.elements[1][1];
  86. p_array[6] = p_mtx.elements[2][1];
  87. p_array[7] = 0;
  88. p_array[8] = p_mtx.elements[0][2];
  89. p_array[9] = p_mtx.elements[1][2];
  90. p_array[10] = p_mtx.elements[2][2];
  91. p_array[11] = 0;
  92. }
  93. static _FORCE_INLINE_ void store_transform_transposed_3x4(const Transform &p_mtx, float *p_array) {
  94. p_array[0] = p_mtx.basis.elements[0][0];
  95. p_array[1] = p_mtx.basis.elements[0][1];
  96. p_array[2] = p_mtx.basis.elements[0][2];
  97. p_array[3] = p_mtx.origin.x;
  98. p_array[4] = p_mtx.basis.elements[1][0];
  99. p_array[5] = p_mtx.basis.elements[1][1];
  100. p_array[6] = p_mtx.basis.elements[1][2];
  101. p_array[7] = p_mtx.origin.y;
  102. p_array[8] = p_mtx.basis.elements[2][0];
  103. p_array[9] = p_mtx.basis.elements[2][1];
  104. p_array[10] = p_mtx.basis.elements[2][2];
  105. p_array[11] = p_mtx.origin.z;
  106. }
  107. static _FORCE_INLINE_ void store_camera(const CameraMatrix &p_mtx, float *p_array) {
  108. for (int i = 0; i < 4; i++) {
  109. for (int j = 0; j < 4; j++) {
  110. p_array[i * 4 + j] = p_mtx.matrix[i][j];
  111. }
  112. }
  113. }
  114. static _FORCE_INLINE_ void store_soft_shadow_kernel(const float *p_kernel, float *p_array) {
  115. for (int i = 0; i < 128; i++) {
  116. p_array[i] = p_kernel[i];
  117. }
  118. }
  119. enum ShaderType {
  120. SHADER_TYPE_2D,
  121. SHADER_TYPE_3D,
  122. SHADER_TYPE_PARTICLES,
  123. SHADER_TYPE_SKY,
  124. SHADER_TYPE_MAX
  125. };
  126. struct ShaderData {
  127. virtual void set_code(const String &p_Code) = 0;
  128. virtual void set_default_texture_param(const StringName &p_name, RID p_texture) = 0;
  129. virtual void get_param_list(List<PropertyInfo> *p_param_list) const = 0;
  130. virtual void get_instance_param_list(List<InstanceShaderParam> *p_param_list) const = 0;
  131. virtual bool is_param_texture(const StringName &p_param) const = 0;
  132. virtual bool is_animated() const = 0;
  133. virtual bool casts_shadows() const = 0;
  134. virtual Variant get_default_parameter(const StringName &p_parameter) const = 0;
  135. virtual RS::ShaderNativeSourceCode get_native_source_code() const { return RS::ShaderNativeSourceCode(); }
  136. virtual ~ShaderData() {}
  137. };
  138. typedef ShaderData *(*ShaderDataRequestFunction)();
  139. struct MaterialData {
  140. void update_uniform_buffer(const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Map<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color);
  141. void update_textures(const Map<StringName, Variant> &p_parameters, const Map<StringName, RID> &p_default_textures, const Vector<ShaderCompilerRD::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color);
  142. virtual void set_render_priority(int p_priority) = 0;
  143. virtual void set_next_pass(RID p_pass) = 0;
  144. virtual void update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) = 0;
  145. virtual ~MaterialData();
  146. private:
  147. friend class RendererStorageRD;
  148. RID self;
  149. List<RID>::Element *global_buffer_E = nullptr;
  150. List<RID>::Element *global_texture_E = nullptr;
  151. uint64_t global_textures_pass = 0;
  152. Map<StringName, uint64_t> used_global_textures;
  153. };
  154. typedef MaterialData *(*MaterialDataRequestFunction)(ShaderData *);
  155. enum DefaultRDTexture {
  156. DEFAULT_RD_TEXTURE_WHITE,
  157. DEFAULT_RD_TEXTURE_BLACK,
  158. DEFAULT_RD_TEXTURE_NORMAL,
  159. DEFAULT_RD_TEXTURE_ANISO,
  160. DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER,
  161. DEFAULT_RD_TEXTURE_CUBEMAP_BLACK,
  162. DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK,
  163. DEFAULT_RD_TEXTURE_CUBEMAP_WHITE,
  164. DEFAULT_RD_TEXTURE_3D_WHITE,
  165. DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE,
  166. DEFAULT_RD_TEXTURE_2D_UINT,
  167. DEFAULT_RD_TEXTURE_MAX
  168. };
  169. enum DefaultRDBuffer {
  170. DEFAULT_RD_BUFFER_VERTEX,
  171. DEFAULT_RD_BUFFER_NORMAL,
  172. DEFAULT_RD_BUFFER_TANGENT,
  173. DEFAULT_RD_BUFFER_COLOR,
  174. DEFAULT_RD_BUFFER_TEX_UV,
  175. DEFAULT_RD_BUFFER_TEX_UV2,
  176. DEFAULT_RD_BUFFER_CUSTOM0,
  177. DEFAULT_RD_BUFFER_CUSTOM1,
  178. DEFAULT_RD_BUFFER_CUSTOM2,
  179. DEFAULT_RD_BUFFER_CUSTOM3,
  180. DEFAULT_RD_BUFFER_BONES,
  181. DEFAULT_RD_BUFFER_WEIGHTS,
  182. DEFAULT_RD_BUFFER_MAX,
  183. };
  184. private:
  185. /* CANVAS TEXTURE API (2D) */
  186. struct CanvasTexture {
  187. RID diffuse;
  188. RID normal_map;
  189. RID specular;
  190. Color specular_color = Color(1, 1, 1, 1);
  191. float shininess = 1.0;
  192. RS::CanvasItemTextureFilter texture_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT;
  193. RS::CanvasItemTextureRepeat texture_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT;
  194. RID uniform_sets[RS::CANVAS_ITEM_TEXTURE_FILTER_MAX][RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX];
  195. Size2i size_cache = Size2i(1, 1);
  196. bool use_normal_cache = false;
  197. bool use_specular_cache = false;
  198. bool cleared_cache = true;
  199. void clear_sets();
  200. ~CanvasTexture();
  201. };
  202. RID_PtrOwner<CanvasTexture, true> canvas_texture_owner;
  203. /* TEXTURE API */
  204. struct Texture {
  205. enum Type {
  206. TYPE_2D,
  207. TYPE_LAYERED,
  208. TYPE_3D
  209. };
  210. Type type;
  211. RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
  212. RenderingDevice::TextureType rd_type;
  213. RID rd_texture;
  214. RID rd_texture_srgb;
  215. RenderingDevice::DataFormat rd_format;
  216. RenderingDevice::DataFormat rd_format_srgb;
  217. RD::TextureView rd_view;
  218. Image::Format format;
  219. Image::Format validated_format;
  220. int width;
  221. int height;
  222. int depth;
  223. int layers;
  224. int mipmaps;
  225. int height_2d;
  226. int width_2d;
  227. struct BufferSlice3D {
  228. Size2i size;
  229. uint32_t offset = 0;
  230. uint32_t buffer_size = 0;
  231. };
  232. Vector<BufferSlice3D> buffer_slices_3d;
  233. uint32_t buffer_size_3d = 0;
  234. bool is_render_target;
  235. bool is_proxy;
  236. Ref<Image> image_cache_2d;
  237. String path;
  238. RID proxy_to;
  239. Vector<RID> proxies;
  240. Set<RID> lightmap_users;
  241. RS::TextureDetectCallback detect_3d_callback = nullptr;
  242. void *detect_3d_callback_ud = nullptr;
  243. RS::TextureDetectCallback detect_normal_callback = nullptr;
  244. void *detect_normal_callback_ud = nullptr;
  245. RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
  246. void *detect_roughness_callback_ud = nullptr;
  247. CanvasTexture *canvas_texture = nullptr;
  248. };
  249. struct TextureToRDFormat {
  250. RD::DataFormat format;
  251. RD::DataFormat format_srgb;
  252. RD::TextureSwizzle swizzle_r;
  253. RD::TextureSwizzle swizzle_g;
  254. RD::TextureSwizzle swizzle_b;
  255. RD::TextureSwizzle swizzle_a;
  256. TextureToRDFormat() {
  257. format = RD::DATA_FORMAT_MAX;
  258. format_srgb = RD::DATA_FORMAT_MAX;
  259. swizzle_r = RD::TEXTURE_SWIZZLE_R;
  260. swizzle_g = RD::TEXTURE_SWIZZLE_G;
  261. swizzle_b = RD::TEXTURE_SWIZZLE_B;
  262. swizzle_a = RD::TEXTURE_SWIZZLE_A;
  263. }
  264. };
  265. //textures can be created from threads, so this RID_Owner is thread safe
  266. mutable RID_Owner<Texture, true> texture_owner;
  267. Ref<Image> _validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format);
  268. RID default_rd_textures[DEFAULT_RD_TEXTURE_MAX];
  269. RID default_rd_samplers[RS::CANVAS_ITEM_TEXTURE_FILTER_MAX][RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX];
  270. RID default_rd_storage_buffer;
  271. /* DECAL ATLAS */
  272. struct DecalAtlas {
  273. struct Texture {
  274. int panorama_to_dp_users;
  275. int users;
  276. Rect2 uv_rect;
  277. };
  278. struct SortItem {
  279. RID texture;
  280. Size2i pixel_size;
  281. Size2i size;
  282. Point2i pos;
  283. bool operator<(const SortItem &p_item) const {
  284. //sort larger to smaller
  285. if (size.height == p_item.size.height) {
  286. return size.width > p_item.size.width;
  287. } else {
  288. return size.height > p_item.size.height;
  289. }
  290. }
  291. };
  292. HashMap<RID, Texture> textures;
  293. bool dirty = true;
  294. int mipmaps = 5;
  295. RID texture;
  296. RID texture_srgb;
  297. struct MipMap {
  298. RID fb;
  299. RID texture;
  300. Size2i size;
  301. };
  302. Vector<MipMap> texture_mipmaps;
  303. Size2i size;
  304. } decal_atlas;
  305. void _update_decal_atlas();
  306. /* SHADER */
  307. struct Material;
  308. struct Shader {
  309. ShaderData *data;
  310. String code;
  311. ShaderType type;
  312. Map<StringName, RID> default_texture_parameter;
  313. Set<Material *> owners;
  314. };
  315. ShaderDataRequestFunction shader_data_request_func[SHADER_TYPE_MAX];
  316. mutable RID_Owner<Shader, true> shader_owner;
  317. /* Material */
  318. struct Material {
  319. RID self;
  320. MaterialData *data;
  321. Shader *shader;
  322. //shortcut to shader data and type
  323. ShaderType shader_type;
  324. uint32_t shader_id = 0;
  325. bool update_requested;
  326. bool uniform_dirty;
  327. bool texture_dirty;
  328. Material *update_next;
  329. Map<StringName, Variant> params;
  330. int32_t priority;
  331. RID next_pass;
  332. Dependency dependency;
  333. };
  334. MaterialDataRequestFunction material_data_request_func[SHADER_TYPE_MAX];
  335. mutable RID_Owner<Material, true> material_owner;
  336. Material *material_update_list;
  337. void _material_queue_update(Material *material, bool p_uniform, bool p_texture);
  338. void _update_queued_materials();
  339. /* Mesh */
  340. struct MeshInstance;
  341. struct Mesh {
  342. struct Surface {
  343. RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS;
  344. uint32_t format = 0;
  345. RID vertex_buffer;
  346. RID attribute_buffer;
  347. RID skin_buffer;
  348. uint32_t vertex_count = 0;
  349. uint32_t vertex_buffer_size = 0;
  350. uint32_t skin_buffer_size = 0;
  351. // A different pipeline needs to be allocated
  352. // depending on the inputs available in the
  353. // material.
  354. // There are never that many geometry/material
  355. // combinations, so a simple array is the most
  356. // cache-efficient structure.
  357. struct Version {
  358. uint32_t input_mask = 0;
  359. RD::VertexFormatID vertex_format = 0;
  360. RID vertex_array;
  361. };
  362. SpinLock version_lock; //needed to access versions
  363. Version *versions = nullptr; //allocated on demand
  364. uint32_t version_count = 0;
  365. RID index_buffer;
  366. RID index_array;
  367. uint32_t index_count = 0;
  368. struct LOD {
  369. float edge_length = 0.0;
  370. RID index_buffer;
  371. RID index_array;
  372. };
  373. LOD *lods = nullptr;
  374. uint32_t lod_count = 0;
  375. AABB aabb;
  376. Vector<AABB> bone_aabbs;
  377. RID blend_shape_buffer;
  378. RID material;
  379. uint32_t render_index = 0;
  380. uint64_t render_pass = 0;
  381. uint32_t multimesh_render_index = 0;
  382. uint64_t multimesh_render_pass = 0;
  383. uint32_t particles_render_index = 0;
  384. uint64_t particles_render_pass = 0;
  385. RID uniform_set;
  386. };
  387. uint32_t blend_shape_count = 0;
  388. RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED;
  389. Surface **surfaces = nullptr;
  390. uint32_t surface_count = 0;
  391. Vector<AABB> bone_aabbs;
  392. bool has_bone_weights = false;
  393. AABB aabb;
  394. AABB custom_aabb;
  395. Vector<RID> material_cache;
  396. List<MeshInstance *> instances;
  397. RID shadow_mesh;
  398. Set<Mesh *> shadow_owners;
  399. Dependency dependency;
  400. };
  401. mutable RID_Owner<Mesh, true> mesh_owner;
  402. struct MeshInstance {
  403. Mesh *mesh;
  404. RID skeleton;
  405. struct Surface {
  406. RID vertex_buffer;
  407. RID uniform_set;
  408. Mesh::Surface::Version *versions = nullptr; //allocated on demand
  409. uint32_t version_count = 0;
  410. };
  411. LocalVector<Surface> surfaces;
  412. LocalVector<float> blend_weights;
  413. RID blend_weights_buffer;
  414. List<MeshInstance *>::Element *I = nullptr; //used to erase itself
  415. uint64_t skeleton_version = 0;
  416. bool dirty = false;
  417. bool weights_dirty = false;
  418. SelfList<MeshInstance> weight_update_list;
  419. SelfList<MeshInstance> array_update_list;
  420. MeshInstance() :
  421. weight_update_list(this), array_update_list(this) {}
  422. };
  423. void _mesh_instance_clear(MeshInstance *mi);
  424. void _mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface);
  425. mutable RID_PtrOwner<MeshInstance> mesh_instance_owner;
  426. SelfList<MeshInstance>::List dirty_mesh_instance_weights;
  427. SelfList<MeshInstance>::List dirty_mesh_instance_arrays;
  428. struct SkeletonShader {
  429. struct PushConstant {
  430. uint32_t has_normal;
  431. uint32_t has_tangent;
  432. uint32_t has_skeleton;
  433. uint32_t has_blend_shape;
  434. uint32_t vertex_count;
  435. uint32_t vertex_stride;
  436. uint32_t skin_stride;
  437. uint32_t skin_weight_offset;
  438. uint32_t blend_shape_count;
  439. uint32_t normalized_blend_shapes;
  440. uint32_t pad0;
  441. uint32_t pad1;
  442. };
  443. enum {
  444. UNIFORM_SET_INSTANCE = 0,
  445. UNIFORM_SET_SURFACE = 1,
  446. UNIFORM_SET_SKELETON = 2,
  447. };
  448. enum {
  449. SHADER_MODE_2D,
  450. SHADER_MODE_3D,
  451. SHADER_MODE_MAX
  452. };
  453. SkeletonShaderRD shader;
  454. RID version;
  455. RID version_shader[SHADER_MODE_MAX];
  456. RID pipeline[SHADER_MODE_MAX];
  457. RID default_skeleton_uniform_set;
  458. } skeleton_shader;
  459. void _mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint32_t p_input_mask, MeshInstance::Surface *mis = nullptr);
  460. RID mesh_default_rd_buffers[DEFAULT_RD_BUFFER_MAX];
  461. /* MultiMesh */
  462. struct MultiMesh {
  463. RID mesh;
  464. int instances = 0;
  465. RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D;
  466. bool uses_colors = false;
  467. bool uses_custom_data = false;
  468. int visible_instances = -1;
  469. AABB aabb;
  470. bool aabb_dirty = false;
  471. bool buffer_set = false;
  472. uint32_t stride_cache = 0;
  473. uint32_t color_offset_cache = 0;
  474. uint32_t custom_data_offset_cache = 0;
  475. Vector<float> data_cache; //used if individual setting is used
  476. bool *data_cache_dirty_regions = nullptr;
  477. uint32_t data_cache_used_dirty_regions = 0;
  478. RID buffer; //storage buffer
  479. RID uniform_set_3d;
  480. bool dirty = false;
  481. MultiMesh *dirty_list = nullptr;
  482. Dependency dependency;
  483. };
  484. mutable RID_Owner<MultiMesh, true> multimesh_owner;
  485. MultiMesh *multimesh_dirty_list = nullptr;
  486. _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const;
  487. _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb);
  488. _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb);
  489. _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances);
  490. void _update_dirty_multimeshes();
  491. /* PARTICLES */
  492. struct ParticleData {
  493. float xform[16];
  494. float velocity[3];
  495. uint32_t active;
  496. float color[4];
  497. float custom[3];
  498. float lifetime;
  499. uint32_t pad[3];
  500. };
  501. struct ParticlesFrameParams {
  502. enum {
  503. MAX_ATTRACTORS = 32,
  504. MAX_COLLIDERS = 32,
  505. MAX_3D_TEXTURES = 7
  506. };
  507. enum AttractorType {
  508. ATTRACTOR_TYPE_SPHERE,
  509. ATTRACTOR_TYPE_BOX,
  510. ATTRACTOR_TYPE_VECTOR_FIELD,
  511. };
  512. struct Attractor {
  513. float transform[16];
  514. float extents[3]; //exents or radius
  515. uint32_t type;
  516. uint32_t texture_index; //texture index for vector field
  517. float strength;
  518. float attenuation;
  519. float directionality;
  520. };
  521. enum CollisionType {
  522. COLLISION_TYPE_SPHERE,
  523. COLLISION_TYPE_BOX,
  524. COLLISION_TYPE_SDF,
  525. COLLISION_TYPE_HEIGHT_FIELD
  526. };
  527. struct Collider {
  528. float transform[16];
  529. float extents[3]; //exents or radius
  530. uint32_t type;
  531. uint32_t texture_index; //texture index for vector field
  532. float scale;
  533. uint32_t pad[2];
  534. };
  535. uint32_t emitting;
  536. float system_phase;
  537. float prev_system_phase;
  538. uint32_t cycle;
  539. float explosiveness;
  540. float randomness;
  541. float time;
  542. float delta;
  543. uint32_t frame;
  544. uint32_t pad0;
  545. uint32_t pad1;
  546. uint32_t pad2;
  547. uint32_t random_seed;
  548. uint32_t attractor_count;
  549. uint32_t collider_count;
  550. float particle_size;
  551. float emission_transform[16];
  552. Attractor attractors[MAX_ATTRACTORS];
  553. Collider colliders[MAX_COLLIDERS];
  554. };
  555. struct ParticleEmissionBufferData {
  556. };
  557. struct ParticleEmissionBuffer {
  558. struct Data {
  559. float xform[16];
  560. float velocity[3];
  561. uint32_t flags;
  562. float color[4];
  563. float custom[4];
  564. };
  565. int32_t particle_count;
  566. int32_t particle_max;
  567. uint32_t pad1;
  568. uint32_t pad2;
  569. Data data[1]; //its 2020 and empty arrays are still non standard in C++
  570. };
  571. struct Particles {
  572. bool inactive = true;
  573. float inactive_time = 0.0;
  574. bool emitting = false;
  575. bool one_shot = false;
  576. int amount = 0;
  577. float lifetime = 1.0;
  578. float pre_process_time = 0.0;
  579. float explosiveness = 0.0;
  580. float randomness = 0.0;
  581. bool restart_request = false;
  582. AABB custom_aabb = AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8));
  583. bool use_local_coords = true;
  584. RID process_material;
  585. uint32_t frame_counter = 0;
  586. RS::ParticlesTransformAlign transform_align = RS::PARTICLES_TRANSFORM_ALIGN_DISABLED;
  587. RS::ParticlesDrawOrder draw_order = RS::PARTICLES_DRAW_ORDER_INDEX;
  588. Vector<RID> draw_passes;
  589. Vector<Transform> trail_bind_poses;
  590. bool trail_bind_poses_dirty = false;
  591. RID trail_bind_pose_buffer;
  592. RID trail_bind_pose_uniform_set;
  593. RID particle_buffer;
  594. RID particle_instance_buffer;
  595. RID frame_params_buffer;
  596. RID particles_material_uniform_set;
  597. RID particles_copy_uniform_set;
  598. RID particles_transforms_buffer_uniform_set;
  599. RID collision_textures_uniform_set;
  600. RID collision_3d_textures[ParticlesFrameParams::MAX_3D_TEXTURES];
  601. uint32_t collision_3d_textures_used = 0;
  602. RID collision_heightmap_texture;
  603. RID particles_sort_buffer;
  604. RID particles_sort_uniform_set;
  605. bool dirty = false;
  606. Particles *update_list = nullptr;
  607. RID sub_emitter;
  608. float phase = 0.0;
  609. float prev_phase = 0.0;
  610. uint64_t prev_ticks = 0;
  611. uint32_t random_seed = 0;
  612. uint32_t cycle_number = 0;
  613. float speed_scale = 1.0;
  614. int fixed_fps = 30;
  615. bool interpolate = true;
  616. bool fractional_delta = false;
  617. float frame_remainder = 0;
  618. float collision_base_size = 0.01;
  619. bool clear = true;
  620. bool force_sub_emit = false;
  621. Transform emission_transform;
  622. Vector<uint8_t> emission_buffer_data;
  623. ParticleEmissionBuffer *emission_buffer = nullptr;
  624. RID emission_storage_buffer;
  625. Set<RID> collisions;
  626. Dependency dependency;
  627. float trail_length = 1.0;
  628. bool trails_enabled = false;
  629. LocalVector<ParticlesFrameParams> frame_history;
  630. LocalVector<ParticlesFrameParams> trail_params;
  631. Particles() {
  632. }
  633. };
  634. void _particles_process(Particles *p_particles, float p_delta);
  635. void _particles_allocate_emission_buffer(Particles *particles);
  636. void _particles_free_data(Particles *particles);
  637. void _particles_update_buffers(Particles *particles);
  638. struct ParticlesShader {
  639. struct PushConstant {
  640. float lifetime;
  641. uint32_t clear;
  642. uint32_t total_particles;
  643. uint32_t trail_size;
  644. uint32_t use_fractional_delta;
  645. uint32_t sub_emitter_mode;
  646. uint32_t can_emit;
  647. uint32_t trail_pass;
  648. };
  649. ParticlesShaderRD shader;
  650. ShaderCompilerRD compiler;
  651. RID default_shader;
  652. RID default_material;
  653. RID default_shader_rd;
  654. RID base_uniform_set;
  655. struct CopyPushConstant {
  656. float sort_direction[3];
  657. uint32_t total_particles;
  658. uint32_t trail_size;
  659. uint32_t trail_total;
  660. float frame_delta;
  661. float frame_remainder;
  662. float align_up[3];
  663. uint32_t align_mode;
  664. };
  665. enum {
  666. COPY_MODE_FILL_INSTANCES,
  667. COPY_MODE_FILL_SORT_BUFFER,
  668. COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER,
  669. COPY_MODE_MAX,
  670. };
  671. ParticlesCopyShaderRD copy_shader;
  672. RID copy_shader_version;
  673. RID copy_pipelines[COPY_MODE_MAX];
  674. LocalVector<float> pose_update_buffer;
  675. } particles_shader;
  676. Particles *particle_update_list = nullptr;
  677. struct ParticlesShaderData : public ShaderData {
  678. bool valid;
  679. RID version;
  680. //PipelineCacheRD pipelines[SKY_VERSION_MAX];
  681. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  682. Vector<ShaderCompilerRD::GeneratedCode::Texture> texture_uniforms;
  683. Vector<uint32_t> ubo_offsets;
  684. uint32_t ubo_size;
  685. String path;
  686. String code;
  687. Map<StringName, RID> default_texture_params;
  688. RID pipeline;
  689. bool uses_time;
  690. virtual void set_code(const String &p_Code);
  691. virtual void set_default_texture_param(const StringName &p_name, RID p_texture);
  692. virtual void get_param_list(List<PropertyInfo> *p_param_list) const;
  693. virtual void get_instance_param_list(List<RendererStorage::InstanceShaderParam> *p_param_list) const;
  694. virtual bool is_param_texture(const StringName &p_param) const;
  695. virtual bool is_animated() const;
  696. virtual bool casts_shadows() const;
  697. virtual Variant get_default_parameter(const StringName &p_parameter) const;
  698. virtual RS::ShaderNativeSourceCode get_native_source_code() const;
  699. ParticlesShaderData();
  700. virtual ~ParticlesShaderData();
  701. };
  702. ShaderData *_create_particles_shader_func();
  703. static RendererStorageRD::ShaderData *_create_particles_shader_funcs() {
  704. return base_singleton->_create_particles_shader_func();
  705. }
  706. struct ParticlesMaterialData : public MaterialData {
  707. uint64_t last_frame;
  708. ParticlesShaderData *shader_data;
  709. RID uniform_buffer;
  710. RID uniform_set;
  711. Vector<RID> texture_cache;
  712. Vector<uint8_t> ubo_data;
  713. bool uniform_set_updated;
  714. virtual void set_render_priority(int p_priority) {}
  715. virtual void set_next_pass(RID p_pass) {}
  716. virtual void update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
  717. virtual ~ParticlesMaterialData();
  718. };
  719. MaterialData *_create_particles_material_func(ParticlesShaderData *p_shader);
  720. static RendererStorageRD::MaterialData *_create_particles_material_funcs(ShaderData *p_shader) {
  721. return base_singleton->_create_particles_material_func(static_cast<ParticlesShaderData *>(p_shader));
  722. }
  723. void update_particles();
  724. mutable RID_Owner<Particles, true> particles_owner;
  725. /* Particles Collision */
  726. struct ParticlesCollision {
  727. RS::ParticlesCollisionType type = RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT;
  728. uint32_t cull_mask = 0xFFFFFFFF;
  729. float radius = 1.0;
  730. Vector3 extents = Vector3(1, 1, 1);
  731. float attractor_strength = 1.0;
  732. float attractor_attenuation = 1.0;
  733. float attractor_directionality = 0.0;
  734. RID field_texture;
  735. RID heightfield_texture;
  736. RID heightfield_fb;
  737. Size2i heightfield_fb_size;
  738. RS::ParticlesCollisionHeightfieldResolution heightfield_resolution = RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_1024;
  739. Dependency dependency;
  740. };
  741. mutable RID_Owner<ParticlesCollision, true> particles_collision_owner;
  742. struct ParticlesCollisionInstance {
  743. RID collision;
  744. Transform transform;
  745. bool active = false;
  746. };
  747. mutable RID_Owner<ParticlesCollisionInstance> particles_collision_instance_owner;
  748. /* Skeleton */
  749. struct Skeleton {
  750. bool use_2d = false;
  751. int size = 0;
  752. Vector<float> data;
  753. RID buffer;
  754. bool dirty = false;
  755. Skeleton *dirty_list = nullptr;
  756. Transform2D base_transform_2d;
  757. RID uniform_set_3d;
  758. RID uniform_set_mi;
  759. uint64_t version = 1;
  760. Dependency dependency;
  761. };
  762. mutable RID_Owner<Skeleton, true> skeleton_owner;
  763. _FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton);
  764. Skeleton *skeleton_dirty_list = nullptr;
  765. void _update_dirty_skeletons();
  766. /* LIGHT */
  767. struct Light {
  768. RS::LightType type;
  769. float param[RS::LIGHT_PARAM_MAX];
  770. Color color = Color(1, 1, 1, 1);
  771. Color shadow_color;
  772. RID projector;
  773. bool shadow = false;
  774. bool negative = false;
  775. bool reverse_cull = false;
  776. RS::LightBakeMode bake_mode = RS::LIGHT_BAKE_DYNAMIC;
  777. uint32_t max_sdfgi_cascade = 2;
  778. uint32_t cull_mask = 0xFFFFFFFF;
  779. RS::LightOmniShadowMode omni_shadow_mode = RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  780. RS::LightDirectionalShadowMode directional_shadow_mode = RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  781. RS::LightDirectionalShadowDepthRangeMode directional_range_mode = RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
  782. bool directional_blend_splits = false;
  783. bool directional_sky_only = false;
  784. uint64_t version = 0;
  785. Dependency dependency;
  786. };
  787. mutable RID_Owner<Light, true> light_owner;
  788. /* REFLECTION PROBE */
  789. struct ReflectionProbe {
  790. RS::ReflectionProbeUpdateMode update_mode = RS::REFLECTION_PROBE_UPDATE_ONCE;
  791. int resolution = 256;
  792. float intensity = 1.0;
  793. RS::ReflectionProbeAmbientMode ambient_mode = RS::REFLECTION_PROBE_AMBIENT_ENVIRONMENT;
  794. Color ambient_color;
  795. float ambient_color_energy = 1.0;
  796. float max_distance = 0;
  797. Vector3 extents = Vector3(1, 1, 1);
  798. Vector3 origin_offset;
  799. bool interior = false;
  800. bool box_projection = false;
  801. bool enable_shadows = false;
  802. uint32_t cull_mask = (1 << 20) - 1;
  803. float lod_threshold = 0.01;
  804. Dependency dependency;
  805. };
  806. mutable RID_Owner<ReflectionProbe, true> reflection_probe_owner;
  807. /* DECAL */
  808. struct Decal {
  809. Vector3 extents = Vector3(1, 1, 1);
  810. RID textures[RS::DECAL_TEXTURE_MAX];
  811. float emission_energy = 1.0;
  812. float albedo_mix = 1.0;
  813. Color modulate = Color(1, 1, 1, 1);
  814. uint32_t cull_mask = (1 << 20) - 1;
  815. float upper_fade = 0.3;
  816. float lower_fade = 0.3;
  817. bool distance_fade = false;
  818. float distance_fade_begin = 10;
  819. float distance_fade_length = 1;
  820. float normal_fade = 0.0;
  821. Dependency dependency;
  822. };
  823. mutable RID_Owner<Decal, true> decal_owner;
  824. /* GI PROBE */
  825. struct GIProbe {
  826. RID octree_buffer;
  827. RID data_buffer;
  828. RID sdf_texture;
  829. uint32_t octree_buffer_size = 0;
  830. uint32_t data_buffer_size = 0;
  831. Vector<int> level_counts;
  832. int cell_count = 0;
  833. Transform to_cell_xform;
  834. AABB bounds;
  835. Vector3i octree_size;
  836. float dynamic_range = 4.0;
  837. float energy = 1.0;
  838. float ao = 0.0;
  839. float ao_size = 0.5;
  840. float bias = 1.4;
  841. float normal_bias = 0.0;
  842. float propagation = 0.7;
  843. bool interior = false;
  844. bool use_two_bounces = false;
  845. float anisotropy_strength = 0.5;
  846. uint32_t version = 1;
  847. uint32_t data_version = 1;
  848. Dependency dependency;
  849. };
  850. GiprobeSdfShaderRD giprobe_sdf_shader;
  851. RID giprobe_sdf_shader_version;
  852. RID giprobe_sdf_shader_version_shader;
  853. RID giprobe_sdf_shader_pipeline;
  854. mutable RID_Owner<GIProbe, true> gi_probe_owner;
  855. /* REFLECTION PROBE */
  856. struct Lightmap {
  857. RID light_texture;
  858. bool uses_spherical_harmonics = false;
  859. bool interior = false;
  860. AABB bounds = AABB(Vector3(), Vector3(1, 1, 1));
  861. int32_t array_index = -1; //unassigned
  862. PackedVector3Array points;
  863. PackedColorArray point_sh;
  864. PackedInt32Array tetrahedra;
  865. PackedInt32Array bsp_tree;
  866. struct BSP {
  867. static const int32_t EMPTY_LEAF = INT32_MIN;
  868. float plane[4];
  869. int32_t over = EMPTY_LEAF, under = EMPTY_LEAF;
  870. };
  871. Dependency dependency;
  872. };
  873. bool using_lightmap_array; //high end uses this
  874. /* for high end */
  875. Vector<RID> lightmap_textures;
  876. uint64_t lightmap_array_version = 0;
  877. mutable RID_Owner<Lightmap, true> lightmap_owner;
  878. float lightmap_probe_capture_update_speed = 4;
  879. /* RENDER TARGET */
  880. struct RenderTarget {
  881. Size2i size;
  882. RID framebuffer;
  883. RID color;
  884. //used for retrieving from CPU
  885. RD::DataFormat color_format = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  886. RD::DataFormat color_format_srgb = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  887. Image::Format image_format = Image::FORMAT_L8;
  888. bool flags[RENDER_TARGET_FLAG_MAX];
  889. RID backbuffer; //used for effects
  890. RID backbuffer_fb;
  891. RID backbuffer_mipmap0;
  892. struct BackbufferMipmap {
  893. RID mipmap;
  894. RID mipmap_copy;
  895. };
  896. Vector<BackbufferMipmap> backbuffer_mipmaps;
  897. RID framebuffer_uniform_set;
  898. RID backbuffer_uniform_set;
  899. RID sdf_buffer_write;
  900. RID sdf_buffer_write_fb;
  901. RID sdf_buffer_process[2];
  902. RID sdf_buffer_read;
  903. RID sdf_buffer_process_uniform_sets[2];
  904. RS::ViewportSDFOversize sdf_oversize = RS::VIEWPORT_SDF_OVERSIZE_120_PERCENT;
  905. RS::ViewportSDFScale sdf_scale = RS::VIEWPORT_SDF_SCALE_50_PERCENT;
  906. Size2i process_size;
  907. //texture generated for this owner (nor RD).
  908. RID texture;
  909. bool was_used;
  910. //clear request
  911. bool clear_requested;
  912. Color clear_color;
  913. };
  914. mutable RID_Owner<RenderTarget> render_target_owner;
  915. void _clear_render_target(RenderTarget *rt);
  916. void _update_render_target(RenderTarget *rt);
  917. void _create_render_target_backbuffer(RenderTarget *rt);
  918. void _render_target_allocate_sdf(RenderTarget *rt);
  919. void _render_target_clear_sdf(RenderTarget *rt);
  920. Rect2i _render_target_get_sdf_rect(const RenderTarget *rt) const;
  921. struct RenderTargetSDF {
  922. enum {
  923. SHADER_LOAD,
  924. SHADER_LOAD_SHRINK,
  925. SHADER_PROCESS,
  926. SHADER_PROCESS_OPTIMIZED,
  927. SHADER_STORE,
  928. SHADER_STORE_SHRINK,
  929. SHADER_MAX
  930. };
  931. struct PushConstant {
  932. int32_t size[2];
  933. int32_t stride;
  934. int32_t shift;
  935. int32_t base_size[2];
  936. int32_t pad[2];
  937. };
  938. CanvasSdfShaderRD shader;
  939. RID shader_version;
  940. RID pipelines[SHADER_MAX];
  941. } rt_sdf;
  942. /* GLOBAL SHADER VARIABLES */
  943. struct GlobalVariables {
  944. enum {
  945. BUFFER_DIRTY_REGION_SIZE = 1024
  946. };
  947. struct Variable {
  948. Set<RID> texture_materials; // materials using this
  949. RS::GlobalVariableType type;
  950. Variant value;
  951. Variant override;
  952. int32_t buffer_index; //for vectors
  953. int32_t buffer_elements; //for vectors
  954. };
  955. HashMap<StringName, Variable> variables;
  956. struct Value {
  957. float x;
  958. float y;
  959. float z;
  960. float w;
  961. };
  962. struct ValueInt {
  963. int32_t x;
  964. int32_t y;
  965. int32_t z;
  966. int32_t w;
  967. };
  968. struct ValueUInt {
  969. uint32_t x;
  970. uint32_t y;
  971. uint32_t z;
  972. uint32_t w;
  973. };
  974. struct ValueUsage {
  975. uint32_t elements = 0;
  976. };
  977. List<RID> materials_using_buffer;
  978. List<RID> materials_using_texture;
  979. RID buffer;
  980. Value *buffer_values;
  981. ValueUsage *buffer_usage;
  982. bool *buffer_dirty_regions;
  983. uint32_t buffer_dirty_region_count = 0;
  984. uint32_t buffer_size;
  985. bool must_update_texture_materials = false;
  986. bool must_update_buffer_materials = false;
  987. HashMap<RID, int32_t> instance_buffer_pos;
  988. } global_variables;
  989. int32_t _global_variable_allocate(uint32_t p_elements);
  990. void _global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value);
  991. void _global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements);
  992. void _update_global_variables();
  993. /* EFFECTS */
  994. EffectsRD effects;
  995. public:
  996. virtual bool can_create_resources_async() const;
  997. /* TEXTURE API */
  998. virtual RID texture_allocate();
  999. virtual void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image);
  1000. virtual void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type);
  1001. virtual void texture_3d_initialize(RID p_texture, Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data); //all slices, then all the mipmaps, must be coherent
  1002. virtual void texture_proxy_initialize(RID p_texture, RID p_base);
  1003. virtual void _texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate);
  1004. virtual void texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer = 0); //mostly used for video and streaming
  1005. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  1006. virtual void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data);
  1007. virtual void texture_proxy_update(RID p_texture, RID p_proxy_to);
  1008. //these two APIs can be used together or in combination with the others.
  1009. virtual void texture_2d_placeholder_initialize(RID p_texture);
  1010. virtual void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type);
  1011. virtual void texture_3d_placeholder_initialize(RID p_texture);
  1012. virtual Ref<Image> texture_2d_get(RID p_texture) const;
  1013. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const;
  1014. virtual Vector<Ref<Image>> texture_3d_get(RID p_texture) const;
  1015. virtual void texture_replace(RID p_texture, RID p_by_texture);
  1016. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
  1017. virtual void texture_set_path(RID p_texture, const String &p_path);
  1018. virtual String texture_get_path(RID p_texture) const;
  1019. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  1020. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  1021. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata);
  1022. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info);
  1023. virtual void texture_set_proxy(RID p_proxy, RID p_base);
  1024. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable);
  1025. virtual Size2 texture_size_with_proxy(RID p_proxy);
  1026. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  1027. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  1028. RID decal_atlas_get_texture() const;
  1029. RID decal_atlas_get_texture_srgb() const;
  1030. _FORCE_INLINE_ Rect2 decal_atlas_get_texture_rect(RID p_texture) {
  1031. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  1032. if (!t) {
  1033. return Rect2();
  1034. }
  1035. return t->uv_rect;
  1036. }
  1037. //internal usage
  1038. _FORCE_INLINE_ RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) {
  1039. if (p_texture.is_null()) {
  1040. return RID();
  1041. }
  1042. Texture *tex = texture_owner.getornull(p_texture);
  1043. if (!tex) {
  1044. return RID();
  1045. }
  1046. return (p_srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1047. }
  1048. _FORCE_INLINE_ Size2i texture_2d_get_size(RID p_texture) {
  1049. if (p_texture.is_null()) {
  1050. return Size2i();
  1051. }
  1052. Texture *tex = texture_owner.getornull(p_texture);
  1053. if (!tex) {
  1054. return Size2i();
  1055. }
  1056. return Size2i(tex->width_2d, tex->height_2d);
  1057. }
  1058. _FORCE_INLINE_ RID texture_rd_get_default(DefaultRDTexture p_texture) {
  1059. return default_rd_textures[p_texture];
  1060. }
  1061. _FORCE_INLINE_ RID sampler_rd_get_default(RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat) {
  1062. return default_rd_samplers[p_filter][p_repeat];
  1063. }
  1064. /* CANVAS TEXTURE API */
  1065. RID canvas_texture_allocate();
  1066. void canvas_texture_initialize(RID p_canvas_texture);
  1067. virtual void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture);
  1068. virtual void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_specular_color, float p_shininess);
  1069. virtual void canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter);
  1070. virtual void canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat);
  1071. bool canvas_texture_get_uniform_set(RID p_texture, RS::CanvasItemTextureFilter p_base_filter, RS::CanvasItemTextureRepeat p_base_repeat, RID p_base_shader, int p_base_set, RID &r_uniform_set, Size2i &r_size, Color &r_specular_shininess, bool &r_use_normal, bool &r_use_specular);
  1072. /* SHADER API */
  1073. RID shader_allocate();
  1074. void shader_initialize(RID p_shader);
  1075. void shader_set_code(RID p_shader, const String &p_code);
  1076. String shader_get_code(RID p_shader) const;
  1077. void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  1078. void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture);
  1079. RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const;
  1080. Variant shader_get_param_default(RID p_shader, const StringName &p_param) const;
  1081. void shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function);
  1082. virtual RS::ShaderNativeSourceCode shader_get_native_source_code(RID p_shader) const;
  1083. /* COMMON MATERIAL API */
  1084. RID material_allocate();
  1085. void material_initialize(RID p_material);
  1086. void material_set_shader(RID p_material, RID p_shader);
  1087. void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value);
  1088. Variant material_get_param(RID p_material, const StringName &p_param) const;
  1089. void material_set_next_pass(RID p_material, RID p_next_material);
  1090. void material_set_render_priority(RID p_material, int priority);
  1091. bool material_is_animated(RID p_material);
  1092. bool material_casts_shadows(RID p_material);
  1093. void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters);
  1094. void material_update_dependency(RID p_material, DependencyTracker *p_instance);
  1095. void material_force_update_textures(RID p_material, ShaderType p_shader_type);
  1096. void material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function);
  1097. _FORCE_INLINE_ uint32_t material_get_shader_id(RID p_material) {
  1098. Material *material = material_owner.getornull(p_material);
  1099. return material->shader_id;
  1100. }
  1101. _FORCE_INLINE_ MaterialData *material_get_data(RID p_material, ShaderType p_shader_type) {
  1102. Material *material = material_owner.getornull(p_material);
  1103. if (!material || material->shader_type != p_shader_type) {
  1104. return nullptr;
  1105. } else {
  1106. return material->data;
  1107. }
  1108. }
  1109. /* MESH API */
  1110. RID mesh_allocate();
  1111. void mesh_initialize(RID p_mesh);
  1112. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count);
  1113. /// Return stride
  1114. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface);
  1115. virtual int mesh_get_blend_shape_count(RID p_mesh) const;
  1116. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode);
  1117. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const;
  1118. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  1119. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material);
  1120. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  1121. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const;
  1122. virtual int mesh_get_surface_count(RID p_mesh) const;
  1123. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb);
  1124. virtual AABB mesh_get_custom_aabb(RID p_mesh) const;
  1125. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID());
  1126. virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh);
  1127. virtual void mesh_clear(RID p_mesh);
  1128. virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton);
  1129. /* MESH INSTANCE */
  1130. virtual RID mesh_instance_create(RID p_base);
  1131. virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton);
  1132. virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight);
  1133. virtual void mesh_instance_check_for_update(RID p_mesh_instance);
  1134. virtual void update_mesh_instances();
  1135. _FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) {
  1136. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1137. ERR_FAIL_COND_V(!mesh, nullptr);
  1138. r_surface_count = mesh->surface_count;
  1139. if (r_surface_count == 0) {
  1140. return nullptr;
  1141. }
  1142. if (mesh->material_cache.is_empty()) {
  1143. mesh->material_cache.resize(mesh->surface_count);
  1144. for (uint32_t i = 0; i < r_surface_count; i++) {
  1145. mesh->material_cache.write[i] = mesh->surfaces[i]->material;
  1146. }
  1147. }
  1148. return mesh->material_cache.ptr();
  1149. }
  1150. _FORCE_INLINE_ void *mesh_get_surface(RID p_mesh, uint32_t p_surface_index) {
  1151. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1152. ERR_FAIL_COND_V(!mesh, nullptr);
  1153. ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, nullptr);
  1154. return mesh->surfaces[p_surface_index];
  1155. }
  1156. _FORCE_INLINE_ RID mesh_get_shadow_mesh(RID p_mesh) {
  1157. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1158. ERR_FAIL_COND_V(!mesh, RID());
  1159. return mesh->shadow_mesh;
  1160. }
  1161. _FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(void *p_surface) {
  1162. Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface);
  1163. return surface->primitive;
  1164. }
  1165. _FORCE_INLINE_ bool mesh_surface_has_lod(void *p_surface) const {
  1166. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1167. return s->lod_count > 0;
  1168. }
  1169. _FORCE_INLINE_ uint32_t mesh_surface_get_lod(void *p_surface, float p_model_scale, float p_distance_threshold, float p_lod_threshold) const {
  1170. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1171. int32_t current_lod = -1;
  1172. for (uint32_t i = 0; i < s->lod_count; i++) {
  1173. float screen_size = s->lods[i].edge_length * p_model_scale / p_distance_threshold;
  1174. if (screen_size > p_lod_threshold) {
  1175. break;
  1176. }
  1177. current_lod = i;
  1178. }
  1179. if (current_lod == -1) {
  1180. return 0;
  1181. } else {
  1182. return current_lod + 1;
  1183. }
  1184. }
  1185. _FORCE_INLINE_ RID mesh_surface_get_index_array(void *p_surface, uint32_t p_lod) const {
  1186. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1187. if (p_lod == 0) {
  1188. return s->index_array;
  1189. } else {
  1190. return s->lods[p_lod - 1].index_array;
  1191. }
  1192. }
  1193. _FORCE_INLINE_ void mesh_surface_get_vertex_arrays_and_format(void *p_surface, uint32_t p_input_mask, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) {
  1194. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  1195. s->version_lock.lock();
  1196. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  1197. for (uint32_t i = 0; i < s->version_count; i++) {
  1198. if (s->versions[i].input_mask != p_input_mask) {
  1199. continue;
  1200. }
  1201. //we have this version, hooray
  1202. r_vertex_format = s->versions[i].vertex_format;
  1203. r_vertex_array_rd = s->versions[i].vertex_array;
  1204. s->version_lock.unlock();
  1205. return;
  1206. }
  1207. uint32_t version = s->version_count;
  1208. s->version_count++;
  1209. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  1210. _mesh_surface_generate_version_for_input_mask(s->versions[version], s, p_input_mask);
  1211. r_vertex_format = s->versions[version].vertex_format;
  1212. r_vertex_array_rd = s->versions[version].vertex_array;
  1213. s->version_lock.unlock();
  1214. }
  1215. _FORCE_INLINE_ void mesh_instance_surface_get_vertex_arrays_and_format(RID p_mesh_instance, uint32_t p_surface_index, uint32_t p_input_mask, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) {
  1216. MeshInstance *mi = mesh_instance_owner.getornull(p_mesh_instance);
  1217. ERR_FAIL_COND(!mi);
  1218. Mesh *mesh = mi->mesh;
  1219. ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count);
  1220. MeshInstance::Surface *mis = &mi->surfaces[p_surface_index];
  1221. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1222. s->version_lock.lock();
  1223. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  1224. for (uint32_t i = 0; i < mis->version_count; i++) {
  1225. if (mis->versions[i].input_mask != p_input_mask) {
  1226. continue;
  1227. }
  1228. //we have this version, hooray
  1229. r_vertex_format = mis->versions[i].vertex_format;
  1230. r_vertex_array_rd = mis->versions[i].vertex_array;
  1231. s->version_lock.unlock();
  1232. return;
  1233. }
  1234. uint32_t version = mis->version_count;
  1235. mis->version_count++;
  1236. mis->versions = (Mesh::Surface::Version *)memrealloc(mis->versions, sizeof(Mesh::Surface::Version) * mis->version_count);
  1237. _mesh_surface_generate_version_for_input_mask(mis->versions[version], s, p_input_mask, mis);
  1238. r_vertex_format = mis->versions[version].vertex_format;
  1239. r_vertex_array_rd = mis->versions[version].vertex_array;
  1240. s->version_lock.unlock();
  1241. }
  1242. _FORCE_INLINE_ RID mesh_get_default_rd_buffer(DefaultRDBuffer p_buffer) {
  1243. ERR_FAIL_INDEX_V(p_buffer, DEFAULT_RD_BUFFER_MAX, RID());
  1244. return mesh_default_rd_buffers[p_buffer];
  1245. }
  1246. _FORCE_INLINE_ uint32_t mesh_surface_get_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1247. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1248. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1249. if (s->render_pass != p_render_pass) {
  1250. (*r_index)++;
  1251. s->render_pass = p_render_pass;
  1252. s->render_index = *r_index;
  1253. }
  1254. return s->render_index;
  1255. }
  1256. _FORCE_INLINE_ uint32_t mesh_surface_get_multimesh_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1257. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1258. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1259. if (s->multimesh_render_pass != p_render_pass) {
  1260. (*r_index)++;
  1261. s->multimesh_render_pass = p_render_pass;
  1262. s->multimesh_render_index = *r_index;
  1263. }
  1264. return s->multimesh_render_index;
  1265. }
  1266. _FORCE_INLINE_ uint32_t mesh_surface_get_particles_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) {
  1267. Mesh *mesh = mesh_owner.getornull(p_mesh);
  1268. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  1269. if (s->particles_render_pass != p_render_pass) {
  1270. (*r_index)++;
  1271. s->particles_render_pass = p_render_pass;
  1272. s->particles_render_index = *r_index;
  1273. }
  1274. return s->particles_render_index;
  1275. }
  1276. /* MULTIMESH API */
  1277. RID multimesh_allocate();
  1278. void multimesh_initialize(RID p_multimesh);
  1279. void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false);
  1280. int multimesh_get_instance_count(RID p_multimesh) const;
  1281. void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  1282. void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform);
  1283. void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  1284. void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  1285. void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color);
  1286. RID multimesh_get_mesh(RID p_multimesh) const;
  1287. Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  1288. Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  1289. Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  1290. Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  1291. void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer);
  1292. Vector<float> multimesh_get_buffer(RID p_multimesh) const;
  1293. void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  1294. int multimesh_get_visible_instances(RID p_multimesh) const;
  1295. AABB multimesh_get_aabb(RID p_multimesh) const;
  1296. _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const {
  1297. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1298. return multimesh->xform_format;
  1299. }
  1300. _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const {
  1301. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1302. return multimesh->uses_colors;
  1303. }
  1304. _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const {
  1305. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1306. return multimesh->uses_custom_data;
  1307. }
  1308. _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const {
  1309. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1310. if (multimesh->visible_instances >= 0) {
  1311. return multimesh->visible_instances;
  1312. }
  1313. return multimesh->instances;
  1314. }
  1315. _FORCE_INLINE_ RID multimesh_get_3d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const {
  1316. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  1317. if (!multimesh->uniform_set_3d.is_valid()) {
  1318. Vector<RD::Uniform> uniforms;
  1319. RD::Uniform u;
  1320. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1321. u.binding = 0;
  1322. u.ids.push_back(multimesh->buffer);
  1323. uniforms.push_back(u);
  1324. multimesh->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1325. }
  1326. return multimesh->uniform_set_3d;
  1327. }
  1328. /* IMMEDIATE API */
  1329. RID immediate_allocate() { return RID(); }
  1330. void immediate_initialize(RID p_immediate) {}
  1331. virtual void immediate_begin(RID p_immediate, RS::PrimitiveType p_rimitive, RID p_texture = RID()) {}
  1332. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {}
  1333. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal) {}
  1334. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent) {}
  1335. virtual void immediate_color(RID p_immediate, const Color &p_color) {}
  1336. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv) {}
  1337. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {}
  1338. virtual void immediate_end(RID p_immediate) {}
  1339. virtual void immediate_clear(RID p_immediate) {}
  1340. virtual void immediate_set_material(RID p_immediate, RID p_material) {}
  1341. virtual RID immediate_get_material(RID p_immediate) const { return RID(); }
  1342. virtual AABB immediate_get_aabb(RID p_immediate) const { return AABB(); }
  1343. /* SKELETON API */
  1344. RID skeleton_allocate();
  1345. void skeleton_initialize(RID p_skeleton);
  1346. void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false);
  1347. void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform);
  1348. void skeleton_set_world_transform(RID p_skeleton, bool p_enable, const Transform &p_world_transform);
  1349. int skeleton_get_bone_count(RID p_skeleton) const;
  1350. void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform);
  1351. Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const;
  1352. void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform);
  1353. Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const;
  1354. _FORCE_INLINE_ bool skeleton_is_valid(RID p_skeleton) {
  1355. return skeleton_owner.getornull(p_skeleton) != nullptr;
  1356. }
  1357. _FORCE_INLINE_ RID skeleton_get_3d_uniform_set(RID p_skeleton, RID p_shader, uint32_t p_set) const {
  1358. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  1359. ERR_FAIL_COND_V(!skeleton, RID());
  1360. ERR_FAIL_COND_V(skeleton->size == 0, RID());
  1361. if (skeleton->use_2d) {
  1362. return RID();
  1363. }
  1364. if (!skeleton->uniform_set_3d.is_valid()) {
  1365. Vector<RD::Uniform> uniforms;
  1366. RD::Uniform u;
  1367. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1368. u.binding = 0;
  1369. u.ids.push_back(skeleton->buffer);
  1370. uniforms.push_back(u);
  1371. skeleton->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1372. }
  1373. return skeleton->uniform_set_3d;
  1374. }
  1375. /* Light API */
  1376. void _light_initialize(RID p_rid, RS::LightType p_type);
  1377. RID directional_light_allocate();
  1378. void directional_light_initialize(RID p_light);
  1379. RID omni_light_allocate();
  1380. void omni_light_initialize(RID p_light);
  1381. RID spot_light_allocate();
  1382. void spot_light_initialize(RID p_light);
  1383. void light_set_color(RID p_light, const Color &p_color);
  1384. void light_set_param(RID p_light, RS::LightParam p_param, float p_value);
  1385. void light_set_shadow(RID p_light, bool p_enabled);
  1386. void light_set_shadow_color(RID p_light, const Color &p_color);
  1387. void light_set_projector(RID p_light, RID p_texture);
  1388. void light_set_negative(RID p_light, bool p_enable);
  1389. void light_set_cull_mask(RID p_light, uint32_t p_mask);
  1390. void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
  1391. void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode);
  1392. void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade);
  1393. void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode);
  1394. void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode);
  1395. void light_directional_set_blend_splits(RID p_light, bool p_enable);
  1396. bool light_directional_get_blend_splits(RID p_light) const;
  1397. void light_directional_set_sky_only(RID p_light, bool p_sky_only);
  1398. bool light_directional_is_sky_only(RID p_light) const;
  1399. void light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode);
  1400. RS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const;
  1401. RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light);
  1402. RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light);
  1403. _FORCE_INLINE_ RS::LightType light_get_type(RID p_light) const {
  1404. const Light *light = light_owner.getornull(p_light);
  1405. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1406. return light->type;
  1407. }
  1408. AABB light_get_aabb(RID p_light) const;
  1409. _FORCE_INLINE_ float light_get_param(RID p_light, RS::LightParam p_param) {
  1410. const Light *light = light_owner.getornull(p_light);
  1411. ERR_FAIL_COND_V(!light, 0);
  1412. return light->param[p_param];
  1413. }
  1414. _FORCE_INLINE_ RID light_get_projector(RID p_light) {
  1415. const Light *light = light_owner.getornull(p_light);
  1416. ERR_FAIL_COND_V(!light, RID());
  1417. return light->projector;
  1418. }
  1419. _FORCE_INLINE_ Color light_get_color(RID p_light) {
  1420. const Light *light = light_owner.getornull(p_light);
  1421. ERR_FAIL_COND_V(!light, Color());
  1422. return light->color;
  1423. }
  1424. _FORCE_INLINE_ Color light_get_shadow_color(RID p_light) {
  1425. const Light *light = light_owner.getornull(p_light);
  1426. ERR_FAIL_COND_V(!light, Color());
  1427. return light->shadow_color;
  1428. }
  1429. _FORCE_INLINE_ uint32_t light_get_cull_mask(RID p_light) {
  1430. const Light *light = light_owner.getornull(p_light);
  1431. ERR_FAIL_COND_V(!light, 0);
  1432. return light->cull_mask;
  1433. }
  1434. _FORCE_INLINE_ bool light_has_shadow(RID p_light) const {
  1435. const Light *light = light_owner.getornull(p_light);
  1436. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1437. return light->shadow;
  1438. }
  1439. _FORCE_INLINE_ bool light_is_negative(RID p_light) const {
  1440. const Light *light = light_owner.getornull(p_light);
  1441. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  1442. return light->negative;
  1443. }
  1444. _FORCE_INLINE_ float light_get_transmittance_bias(RID p_light) const {
  1445. const Light *light = light_owner.getornull(p_light);
  1446. ERR_FAIL_COND_V(!light, 0.0);
  1447. return light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
  1448. }
  1449. _FORCE_INLINE_ float light_get_shadow_volumetric_fog_fade(RID p_light) const {
  1450. const Light *light = light_owner.getornull(p_light);
  1451. ERR_FAIL_COND_V(!light, 0.0);
  1452. return light->param[RS::LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE];
  1453. }
  1454. RS::LightBakeMode light_get_bake_mode(RID p_light);
  1455. uint32_t light_get_max_sdfgi_cascade(RID p_light);
  1456. uint64_t light_get_version(RID p_light) const;
  1457. /* PROBE API */
  1458. RID reflection_probe_allocate();
  1459. void reflection_probe_initialize(RID p_reflection_probe);
  1460. void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode);
  1461. void reflection_probe_set_intensity(RID p_probe, float p_intensity);
  1462. void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode);
  1463. void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color);
  1464. void reflection_probe_set_ambient_energy(RID p_probe, float p_energy);
  1465. void reflection_probe_set_max_distance(RID p_probe, float p_distance);
  1466. void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents);
  1467. void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset);
  1468. void reflection_probe_set_as_interior(RID p_probe, bool p_enable);
  1469. void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable);
  1470. void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable);
  1471. void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers);
  1472. void reflection_probe_set_resolution(RID p_probe, int p_resolution);
  1473. void reflection_probe_set_lod_threshold(RID p_probe, float p_ratio);
  1474. AABB reflection_probe_get_aabb(RID p_probe) const;
  1475. RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const;
  1476. uint32_t reflection_probe_get_cull_mask(RID p_probe) const;
  1477. Vector3 reflection_probe_get_extents(RID p_probe) const;
  1478. Vector3 reflection_probe_get_origin_offset(RID p_probe) const;
  1479. float reflection_probe_get_origin_max_distance(RID p_probe) const;
  1480. float reflection_probe_get_lod_threshold(RID p_probe) const;
  1481. int reflection_probe_get_resolution(RID p_probe) const;
  1482. bool reflection_probe_renders_shadows(RID p_probe) const;
  1483. float reflection_probe_get_intensity(RID p_probe) const;
  1484. bool reflection_probe_is_interior(RID p_probe) const;
  1485. bool reflection_probe_is_box_projection(RID p_probe) const;
  1486. RS::ReflectionProbeAmbientMode reflection_probe_get_ambient_mode(RID p_probe) const;
  1487. Color reflection_probe_get_ambient_color(RID p_probe) const;
  1488. float reflection_probe_get_ambient_color_energy(RID p_probe) const;
  1489. void base_update_dependency(RID p_base, DependencyTracker *p_instance);
  1490. void skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance);
  1491. /* DECAL API */
  1492. RID decal_allocate();
  1493. void decal_initialize(RID p_decal);
  1494. virtual void decal_set_extents(RID p_decal, const Vector3 &p_extents);
  1495. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture);
  1496. virtual void decal_set_emission_energy(RID p_decal, float p_energy);
  1497. virtual void decal_set_albedo_mix(RID p_decal, float p_mix);
  1498. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate);
  1499. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers);
  1500. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length);
  1501. virtual void decal_set_fade(RID p_decal, float p_above, float p_below);
  1502. virtual void decal_set_normal_fade(RID p_decal, float p_fade);
  1503. _FORCE_INLINE_ Vector3 decal_get_extents(RID p_decal) {
  1504. const Decal *decal = decal_owner.getornull(p_decal);
  1505. return decal->extents;
  1506. }
  1507. _FORCE_INLINE_ RID decal_get_texture(RID p_decal, RS::DecalTexture p_texture) {
  1508. const Decal *decal = decal_owner.getornull(p_decal);
  1509. return decal->textures[p_texture];
  1510. }
  1511. _FORCE_INLINE_ Color decal_get_modulate(RID p_decal) {
  1512. const Decal *decal = decal_owner.getornull(p_decal);
  1513. return decal->modulate;
  1514. }
  1515. _FORCE_INLINE_ float decal_get_emission_energy(RID p_decal) {
  1516. const Decal *decal = decal_owner.getornull(p_decal);
  1517. return decal->emission_energy;
  1518. }
  1519. _FORCE_INLINE_ float decal_get_albedo_mix(RID p_decal) {
  1520. const Decal *decal = decal_owner.getornull(p_decal);
  1521. return decal->albedo_mix;
  1522. }
  1523. _FORCE_INLINE_ uint32_t decal_get_cull_mask(RID p_decal) {
  1524. const Decal *decal = decal_owner.getornull(p_decal);
  1525. return decal->cull_mask;
  1526. }
  1527. _FORCE_INLINE_ float decal_get_upper_fade(RID p_decal) {
  1528. const Decal *decal = decal_owner.getornull(p_decal);
  1529. return decal->upper_fade;
  1530. }
  1531. _FORCE_INLINE_ float decal_get_lower_fade(RID p_decal) {
  1532. const Decal *decal = decal_owner.getornull(p_decal);
  1533. return decal->lower_fade;
  1534. }
  1535. _FORCE_INLINE_ float decal_get_normal_fade(RID p_decal) {
  1536. const Decal *decal = decal_owner.getornull(p_decal);
  1537. return decal->normal_fade;
  1538. }
  1539. _FORCE_INLINE_ bool decal_is_distance_fade_enabled(RID p_decal) {
  1540. const Decal *decal = decal_owner.getornull(p_decal);
  1541. return decal->distance_fade;
  1542. }
  1543. _FORCE_INLINE_ float decal_get_distance_fade_begin(RID p_decal) {
  1544. const Decal *decal = decal_owner.getornull(p_decal);
  1545. return decal->distance_fade_begin;
  1546. }
  1547. _FORCE_INLINE_ float decal_get_distance_fade_length(RID p_decal) {
  1548. const Decal *decal = decal_owner.getornull(p_decal);
  1549. return decal->distance_fade_length;
  1550. }
  1551. virtual AABB decal_get_aabb(RID p_decal) const;
  1552. /* GI PROBE API */
  1553. RID gi_probe_allocate();
  1554. void gi_probe_initialize(RID p_gi_probe);
  1555. void gi_probe_allocate_data(RID p_gi_probe, const Transform &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts);
  1556. AABB gi_probe_get_bounds(RID p_gi_probe) const;
  1557. Vector3i gi_probe_get_octree_size(RID p_gi_probe) const;
  1558. Vector<uint8_t> gi_probe_get_octree_cells(RID p_gi_probe) const;
  1559. Vector<uint8_t> gi_probe_get_data_cells(RID p_gi_probe) const;
  1560. Vector<uint8_t> gi_probe_get_distance_field(RID p_gi_probe) const;
  1561. Vector<int> gi_probe_get_level_counts(RID p_gi_probe) const;
  1562. Transform gi_probe_get_to_cell_xform(RID p_gi_probe) const;
  1563. void gi_probe_set_dynamic_range(RID p_gi_probe, float p_range);
  1564. float gi_probe_get_dynamic_range(RID p_gi_probe) const;
  1565. void gi_probe_set_propagation(RID p_gi_probe, float p_range);
  1566. float gi_probe_get_propagation(RID p_gi_probe) const;
  1567. void gi_probe_set_energy(RID p_gi_probe, float p_energy);
  1568. float gi_probe_get_energy(RID p_gi_probe) const;
  1569. void gi_probe_set_ao(RID p_gi_probe, float p_ao);
  1570. float gi_probe_get_ao(RID p_gi_probe) const;
  1571. void gi_probe_set_ao_size(RID p_gi_probe, float p_strength);
  1572. float gi_probe_get_ao_size(RID p_gi_probe) const;
  1573. void gi_probe_set_bias(RID p_gi_probe, float p_bias);
  1574. float gi_probe_get_bias(RID p_gi_probe) const;
  1575. void gi_probe_set_normal_bias(RID p_gi_probe, float p_range);
  1576. float gi_probe_get_normal_bias(RID p_gi_probe) const;
  1577. void gi_probe_set_interior(RID p_gi_probe, bool p_enable);
  1578. bool gi_probe_is_interior(RID p_gi_probe) const;
  1579. void gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable);
  1580. bool gi_probe_is_using_two_bounces(RID p_gi_probe) const;
  1581. void gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength);
  1582. float gi_probe_get_anisotropy_strength(RID p_gi_probe) const;
  1583. uint32_t gi_probe_get_version(RID p_probe);
  1584. uint32_t gi_probe_get_data_version(RID p_probe);
  1585. RID gi_probe_get_octree_buffer(RID p_gi_probe) const;
  1586. RID gi_probe_get_data_buffer(RID p_gi_probe) const;
  1587. RID gi_probe_get_sdf_texture(RID p_gi_probe);
  1588. /* LIGHTMAP CAPTURE */
  1589. RID lightmap_allocate();
  1590. void lightmap_initialize(RID p_lightmap);
  1591. virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics);
  1592. virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds);
  1593. virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior);
  1594. virtual void lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree);
  1595. virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const;
  1596. virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const;
  1597. virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const;
  1598. virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const;
  1599. virtual AABB lightmap_get_aabb(RID p_lightmap) const;
  1600. virtual bool lightmap_is_interior(RID p_lightmap) const;
  1601. virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh);
  1602. virtual void lightmap_set_probe_capture_update_speed(float p_speed);
  1603. _FORCE_INLINE_ float lightmap_get_probe_capture_update_speed() const {
  1604. return lightmap_probe_capture_update_speed;
  1605. }
  1606. _FORCE_INLINE_ RID lightmap_get_texture(RID p_lightmap) const {
  1607. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  1608. ERR_FAIL_COND_V(!lm, RID());
  1609. return lm->light_texture;
  1610. }
  1611. _FORCE_INLINE_ int32_t lightmap_get_array_index(RID p_lightmap) const {
  1612. ERR_FAIL_COND_V(!using_lightmap_array, -1); //only for arrays
  1613. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  1614. return lm->array_index;
  1615. }
  1616. _FORCE_INLINE_ bool lightmap_uses_spherical_harmonics(RID p_lightmap) const {
  1617. ERR_FAIL_COND_V(!using_lightmap_array, false); //only for arrays
  1618. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  1619. return lm->uses_spherical_harmonics;
  1620. }
  1621. _FORCE_INLINE_ uint64_t lightmap_array_get_version() const {
  1622. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1623. return lightmap_array_version;
  1624. }
  1625. _FORCE_INLINE_ int lightmap_array_get_size() const {
  1626. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1627. return lightmap_textures.size();
  1628. }
  1629. _FORCE_INLINE_ const Vector<RID> &lightmap_array_get_textures() const {
  1630. ERR_FAIL_COND_V(!using_lightmap_array, lightmap_textures); //only for arrays
  1631. return lightmap_textures;
  1632. }
  1633. /* PARTICLES */
  1634. RID particles_allocate();
  1635. void particles_initialize(RID p_particles_collision);
  1636. void particles_set_emitting(RID p_particles, bool p_emitting);
  1637. void particles_set_amount(RID p_particles, int p_amount);
  1638. void particles_set_lifetime(RID p_particles, float p_lifetime);
  1639. void particles_set_one_shot(RID p_particles, bool p_one_shot);
  1640. void particles_set_pre_process_time(RID p_particles, float p_time);
  1641. void particles_set_explosiveness_ratio(RID p_particles, float p_ratio);
  1642. void particles_set_randomness_ratio(RID p_particles, float p_ratio);
  1643. void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb);
  1644. void particles_set_speed_scale(RID p_particles, float p_scale);
  1645. void particles_set_use_local_coordinates(RID p_particles, bool p_enable);
  1646. void particles_set_process_material(RID p_particles, RID p_material);
  1647. void particles_set_fixed_fps(RID p_particles, int p_fps);
  1648. void particles_set_interpolate(RID p_particles, bool p_enable);
  1649. void particles_set_fractional_delta(RID p_particles, bool p_enable);
  1650. void particles_set_collision_base_size(RID p_particles, float p_size);
  1651. void particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align);
  1652. void particles_set_trails(RID p_particles, bool p_enable, float p_length);
  1653. void particles_set_trail_bind_poses(RID p_particles, const Vector<Transform> &p_bind_poses);
  1654. void particles_restart(RID p_particles);
  1655. void particles_emit(RID p_particles, const Transform &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags);
  1656. void particles_set_subemitter(RID p_particles, RID p_subemitter_particles);
  1657. void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order);
  1658. void particles_set_draw_passes(RID p_particles, int p_count);
  1659. void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh);
  1660. void particles_request_process(RID p_particles);
  1661. AABB particles_get_current_aabb(RID p_particles);
  1662. AABB particles_get_aabb(RID p_particles) const;
  1663. void particles_set_emission_transform(RID p_particles, const Transform &p_transform);
  1664. bool particles_get_emitting(RID p_particles);
  1665. int particles_get_draw_passes(RID p_particles) const;
  1666. RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const;
  1667. void particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis);
  1668. virtual bool particles_is_inactive(RID p_particles) const;
  1669. _FORCE_INLINE_ uint32_t particles_get_amount(RID p_particles, uint32_t &r_trail_divisor) {
  1670. Particles *particles = particles_owner.getornull(p_particles);
  1671. ERR_FAIL_COND_V(!particles, 0);
  1672. if (particles->trails_enabled && particles->trail_bind_poses.size() > 1) {
  1673. r_trail_divisor = particles->trail_bind_poses.size();
  1674. } else {
  1675. r_trail_divisor = 1;
  1676. }
  1677. return particles->amount * r_trail_divisor;
  1678. }
  1679. _FORCE_INLINE_ uint32_t particles_is_using_local_coords(RID p_particles) {
  1680. Particles *particles = particles_owner.getornull(p_particles);
  1681. ERR_FAIL_COND_V(!particles, false);
  1682. return particles->use_local_coords;
  1683. }
  1684. _FORCE_INLINE_ RID particles_get_instance_buffer_uniform_set(RID p_particles, RID p_shader, uint32_t p_set) {
  1685. Particles *particles = particles_owner.getornull(p_particles);
  1686. ERR_FAIL_COND_V(!particles, RID());
  1687. if (particles->particles_transforms_buffer_uniform_set.is_null()) {
  1688. _particles_update_buffers(particles);
  1689. Vector<RD::Uniform> uniforms;
  1690. {
  1691. RD::Uniform u;
  1692. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  1693. u.binding = 0;
  1694. u.ids.push_back(particles->particle_instance_buffer);
  1695. uniforms.push_back(u);
  1696. }
  1697. particles->particles_transforms_buffer_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  1698. }
  1699. return particles->particles_transforms_buffer_uniform_set;
  1700. }
  1701. virtual void particles_add_collision(RID p_particles, RID p_particles_collision_instance);
  1702. virtual void particles_remove_collision(RID p_particles, RID p_particles_collision_instance);
  1703. /* PARTICLES COLLISION */
  1704. RID particles_collision_allocate();
  1705. void particles_collision_initialize(RID p_particles_collision);
  1706. virtual void particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type);
  1707. virtual void particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask);
  1708. virtual void particles_collision_set_sphere_radius(RID p_particles_collision, float p_radius); //for spheres
  1709. virtual void particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents); //for non-spheres
  1710. virtual void particles_collision_set_attractor_strength(RID p_particles_collision, float p_strength);
  1711. virtual void particles_collision_set_attractor_directionality(RID p_particles_collision, float p_directionality);
  1712. virtual void particles_collision_set_attractor_attenuation(RID p_particles_collision, float p_curve);
  1713. virtual void particles_collision_set_field_texture(RID p_particles_collision, RID p_texture); //for SDF and vector field, heightfield is dynamic
  1714. virtual void particles_collision_height_field_update(RID p_particles_collision); //for SDF and vector field
  1715. virtual void particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution); //for SDF and vector field
  1716. virtual AABB particles_collision_get_aabb(RID p_particles_collision) const;
  1717. virtual Vector3 particles_collision_get_extents(RID p_particles_collision) const;
  1718. virtual bool particles_collision_is_heightfield(RID p_particles_collision) const;
  1719. RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const;
  1720. //used from 2D and 3D
  1721. virtual RID particles_collision_instance_create(RID p_collision);
  1722. virtual void particles_collision_instance_set_transform(RID p_collision_instance, const Transform &p_transform);
  1723. virtual void particles_collision_instance_set_active(RID p_collision_instance, bool p_active);
  1724. /* GLOBAL VARIABLES API */
  1725. virtual void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value);
  1726. virtual void global_variable_remove(const StringName &p_name);
  1727. virtual Vector<StringName> global_variable_get_list() const;
  1728. virtual void global_variable_set(const StringName &p_name, const Variant &p_value);
  1729. virtual void global_variable_set_override(const StringName &p_name, const Variant &p_value);
  1730. virtual Variant global_variable_get(const StringName &p_name) const;
  1731. virtual RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const;
  1732. RS::GlobalVariableType global_variable_get_type_internal(const StringName &p_name) const;
  1733. virtual void global_variables_load_settings(bool p_load_textures = true);
  1734. virtual void global_variables_clear();
  1735. virtual int32_t global_variables_instance_allocate(RID p_instance);
  1736. virtual void global_variables_instance_free(RID p_instance);
  1737. virtual void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value);
  1738. RID global_variables_get_storage_buffer() const;
  1739. /* RENDER TARGET API */
  1740. RID render_target_create();
  1741. void render_target_set_position(RID p_render_target, int p_x, int p_y);
  1742. void render_target_set_size(RID p_render_target, int p_width, int p_height);
  1743. RID render_target_get_texture(RID p_render_target);
  1744. void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id);
  1745. void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value);
  1746. bool render_target_was_used(RID p_render_target);
  1747. void render_target_set_as_unused(RID p_render_target);
  1748. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps);
  1749. void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color);
  1750. void render_target_gen_back_buffer_mipmaps(RID p_render_target, const Rect2i &p_region);
  1751. RID render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader);
  1752. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color);
  1753. virtual bool render_target_is_clear_requested(RID p_render_target);
  1754. virtual Color render_target_get_clear_request_color(RID p_render_target);
  1755. virtual void render_target_disable_clear_request(RID p_render_target);
  1756. virtual void render_target_do_clear_request(RID p_render_target);
  1757. virtual void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale);
  1758. RID render_target_get_sdf_texture(RID p_render_target);
  1759. RID render_target_get_sdf_framebuffer(RID p_render_target);
  1760. void render_target_sdf_process(RID p_render_target);
  1761. virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const;
  1762. Size2 render_target_get_size(RID p_render_target);
  1763. RID render_target_get_rd_framebuffer(RID p_render_target);
  1764. RID render_target_get_rd_texture(RID p_render_target);
  1765. RID render_target_get_rd_backbuffer(RID p_render_target);
  1766. RID render_target_get_rd_backbuffer_framebuffer(RID p_render_target);
  1767. RID render_target_get_framebuffer_uniform_set(RID p_render_target);
  1768. RID render_target_get_backbuffer_uniform_set(RID p_render_target);
  1769. void render_target_set_framebuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  1770. void render_target_set_backbuffer_uniform_set(RID p_render_target, RID p_uniform_set);
  1771. RS::InstanceType get_base_type(RID p_rid) const;
  1772. bool free(RID p_rid);
  1773. bool has_os_feature(const String &p_feature) const;
  1774. void update_dirty_resources();
  1775. void set_debug_generate_wireframes(bool p_generate) {}
  1776. void render_info_begin_capture() {}
  1777. void render_info_end_capture() {}
  1778. int get_captured_render_info(RS::RenderInfo p_info) { return 0; }
  1779. uint64_t get_render_info(RS::RenderInfo p_info) { return 0; }
  1780. String get_video_adapter_name() const { return String(); }
  1781. String get_video_adapter_vendor() const { return String(); }
  1782. virtual void capture_timestamps_begin();
  1783. virtual void capture_timestamp(const String &p_name);
  1784. virtual uint32_t get_captured_timestamps_count() const;
  1785. virtual uint64_t get_captured_timestamps_frame() const;
  1786. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1787. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1788. virtual String get_captured_timestamp_name(uint32_t p_index) const;
  1789. RID get_default_rd_storage_buffer() { return default_rd_storage_buffer; }
  1790. static RendererStorageRD *base_singleton;
  1791. EffectsRD *get_effects();
  1792. RendererStorageRD();
  1793. ~RendererStorageRD();
  1794. };
  1795. #endif // RASTERIZER_STORAGE_RD_H