rasterizer_storage_rd.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. /*************************************************************************/
  2. /* rasterizer_storage_rd.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 RASTERIZER_STORAGE_RD_H
  31. #define RASTERIZER_STORAGE_RD_H
  32. #include "core/rid_owner.h"
  33. #include "servers/rendering/rasterizer.h"
  34. #include "servers/rendering/rasterizer_rd/rasterizer_effects_rd.h"
  35. #include "servers/rendering/rasterizer_rd/shader_compiler_rd.h"
  36. #include "servers/rendering/rasterizer_rd/shaders/giprobe_sdf.glsl.gen.h"
  37. #include "servers/rendering/rendering_device.h"
  38. class RasterizerStorageRD : public RasterizerStorage {
  39. public:
  40. enum ShaderType {
  41. SHADER_TYPE_2D,
  42. SHADER_TYPE_3D,
  43. SHADER_TYPE_PARTICLES,
  44. SHADER_TYPE_SKY,
  45. SHADER_TYPE_MAX
  46. };
  47. struct ShaderData {
  48. virtual void set_code(const String &p_Code) = 0;
  49. virtual void set_default_texture_param(const StringName &p_name, RID p_texture) = 0;
  50. virtual void get_param_list(List<PropertyInfo> *p_param_list) const = 0;
  51. virtual void get_instance_param_list(List<InstanceShaderParam> *p_param_list) const = 0;
  52. virtual bool is_param_texture(const StringName &p_param) const = 0;
  53. virtual bool is_animated() const = 0;
  54. virtual bool casts_shadows() const = 0;
  55. virtual Variant get_default_parameter(const StringName &p_parameter) const = 0;
  56. virtual ~ShaderData() {}
  57. };
  58. typedef ShaderData *(*ShaderDataRequestFunction)();
  59. struct MaterialData {
  60. 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);
  61. 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);
  62. virtual void set_render_priority(int p_priority) = 0;
  63. virtual void set_next_pass(RID p_pass) = 0;
  64. virtual void update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) = 0;
  65. virtual ~MaterialData();
  66. private:
  67. friend class RasterizerStorageRD;
  68. RID self;
  69. List<RID>::Element *global_buffer_E = nullptr;
  70. List<RID>::Element *global_texture_E = nullptr;
  71. uint64_t global_textures_pass = 0;
  72. Map<StringName, uint64_t> used_global_textures;
  73. };
  74. typedef MaterialData *(*MaterialDataRequestFunction)(ShaderData *);
  75. enum DefaultRDTexture {
  76. DEFAULT_RD_TEXTURE_WHITE,
  77. DEFAULT_RD_TEXTURE_BLACK,
  78. DEFAULT_RD_TEXTURE_NORMAL,
  79. DEFAULT_RD_TEXTURE_ANISO,
  80. DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER,
  81. DEFAULT_RD_TEXTURE_CUBEMAP_BLACK,
  82. DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK,
  83. DEFAULT_RD_TEXTURE_3D_WHITE,
  84. DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE,
  85. DEFAULT_RD_TEXTURE_2D_UINT,
  86. DEFAULT_RD_TEXTURE_MAX
  87. };
  88. enum DefaultRDBuffer {
  89. DEFAULT_RD_BUFFER_VERTEX,
  90. DEFAULT_RD_BUFFER_NORMAL,
  91. DEFAULT_RD_BUFFER_TANGENT,
  92. DEFAULT_RD_BUFFER_COLOR,
  93. DEFAULT_RD_BUFFER_TEX_UV,
  94. DEFAULT_RD_BUFFER_TEX_UV2,
  95. DEFAULT_RD_BUFFER_BONES,
  96. DEFAULT_RD_BUFFER_WEIGHTS,
  97. DEFAULT_RD_BUFFER_MAX,
  98. };
  99. private:
  100. /* TEXTURE API */
  101. struct Texture {
  102. enum Type {
  103. TYPE_2D,
  104. TYPE_LAYERED,
  105. TYPE_3D
  106. };
  107. Type type;
  108. RS::TextureLayeredType layered_type = RS::TEXTURE_LAYERED_2D_ARRAY;
  109. RenderingDevice::TextureType rd_type;
  110. RID rd_texture;
  111. RID rd_texture_srgb;
  112. RenderingDevice::DataFormat rd_format;
  113. RenderingDevice::DataFormat rd_format_srgb;
  114. RD::TextureView rd_view;
  115. Image::Format format;
  116. Image::Format validated_format;
  117. int width;
  118. int height;
  119. int depth;
  120. int layers;
  121. int mipmaps;
  122. int height_2d;
  123. int width_2d;
  124. bool is_render_target;
  125. bool is_proxy;
  126. Ref<Image> image_cache_2d;
  127. String path;
  128. RID proxy_to;
  129. Vector<RID> proxies;
  130. Set<RID> lightmap_users;
  131. RS::TextureDetectCallback detect_3d_callback = nullptr;
  132. void *detect_3d_callback_ud = nullptr;
  133. RS::TextureDetectCallback detect_normal_callback = nullptr;
  134. void *detect_normal_callback_ud = nullptr;
  135. RS::TextureDetectRoughnessCallback detect_roughness_callback = nullptr;
  136. void *detect_roughness_callback_ud = nullptr;
  137. };
  138. struct TextureToRDFormat {
  139. RD::DataFormat format;
  140. RD::DataFormat format_srgb;
  141. RD::TextureSwizzle swizzle_r;
  142. RD::TextureSwizzle swizzle_g;
  143. RD::TextureSwizzle swizzle_b;
  144. RD::TextureSwizzle swizzle_a;
  145. TextureToRDFormat() {
  146. format = RD::DATA_FORMAT_MAX;
  147. format_srgb = RD::DATA_FORMAT_MAX;
  148. swizzle_r = RD::TEXTURE_SWIZZLE_R;
  149. swizzle_g = RD::TEXTURE_SWIZZLE_G;
  150. swizzle_b = RD::TEXTURE_SWIZZLE_B;
  151. swizzle_a = RD::TEXTURE_SWIZZLE_A;
  152. }
  153. };
  154. //textures can be created from threads, so this RID_Owner is thread safe
  155. mutable RID_Owner<Texture, true> texture_owner;
  156. Ref<Image> _validate_texture_format(const Ref<Image> &p_image, TextureToRDFormat &r_format);
  157. RID default_rd_textures[DEFAULT_RD_TEXTURE_MAX];
  158. RID default_rd_samplers[RS::CANVAS_ITEM_TEXTURE_FILTER_MAX][RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX];
  159. /* DECAL ATLAS */
  160. struct DecalAtlas {
  161. struct Texture {
  162. int panorama_to_dp_users;
  163. int users;
  164. Rect2 uv_rect;
  165. };
  166. struct SortItem {
  167. RID texture;
  168. Size2i pixel_size;
  169. Size2i size;
  170. Point2i pos;
  171. bool operator<(const SortItem &p_item) const {
  172. //sort larger to smaller
  173. if (size.height == p_item.size.height) {
  174. return size.width > p_item.size.width;
  175. } else {
  176. return size.height > p_item.size.height;
  177. }
  178. }
  179. };
  180. HashMap<RID, Texture> textures;
  181. bool dirty = true;
  182. int mipmaps = 5;
  183. RID texture;
  184. RID texture_srgb;
  185. struct MipMap {
  186. RID fb;
  187. RID texture;
  188. Size2i size;
  189. };
  190. Vector<MipMap> texture_mipmaps;
  191. Size2i size;
  192. } decal_atlas;
  193. void _update_decal_atlas();
  194. /* SHADER */
  195. struct Material;
  196. struct Shader {
  197. ShaderData *data;
  198. String code;
  199. ShaderType type;
  200. Map<StringName, RID> default_texture_parameter;
  201. Set<Material *> owners;
  202. };
  203. ShaderDataRequestFunction shader_data_request_func[SHADER_TYPE_MAX];
  204. mutable RID_Owner<Shader> shader_owner;
  205. /* Material */
  206. struct Material {
  207. RID self;
  208. MaterialData *data;
  209. Shader *shader;
  210. //shortcut to shader data and type
  211. ShaderType shader_type;
  212. bool update_requested;
  213. bool uniform_dirty;
  214. bool texture_dirty;
  215. Material *update_next;
  216. Map<StringName, Variant> params;
  217. int32_t priority;
  218. RID next_pass;
  219. RasterizerScene::InstanceDependency instance_dependency;
  220. };
  221. MaterialDataRequestFunction material_data_request_func[SHADER_TYPE_MAX];
  222. mutable RID_Owner<Material> material_owner;
  223. Material *material_update_list;
  224. void _material_queue_update(Material *material, bool p_uniform, bool p_texture);
  225. void _update_queued_materials();
  226. /* Mesh */
  227. struct Mesh {
  228. struct Surface {
  229. RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS;
  230. uint32_t format = 0;
  231. RID vertex_buffer;
  232. uint32_t vertex_count = 0;
  233. // A different pipeline needs to be allocated
  234. // depending on the inputs available in the
  235. // material.
  236. // There are never that many geometry/material
  237. // combinations, so a simple array is the most
  238. // cache-efficient structure.
  239. struct Version {
  240. uint32_t input_mask = 0;
  241. RD::VertexFormatID vertex_format = 0;
  242. RID vertex_array;
  243. };
  244. SpinLock version_lock; //needed to access versions
  245. Version *versions = nullptr; //allocated on demand
  246. uint32_t version_count = 0;
  247. RID index_buffer;
  248. RID index_array;
  249. uint32_t index_count = 0;
  250. struct LOD {
  251. float edge_length = 0.0;
  252. RID index_buffer;
  253. RID index_array;
  254. };
  255. LOD *lods = nullptr;
  256. uint32_t lod_count = 0;
  257. AABB aabb;
  258. Vector<AABB> bone_aabbs;
  259. Vector<RID> blend_shapes;
  260. RID blend_shape_base_buffer; //source buffer goes here when using blend shapes, and main one is uncompressed
  261. RID material;
  262. uint32_t render_index = 0;
  263. uint64_t render_pass = 0;
  264. uint32_t multimesh_render_index = 0;
  265. uint64_t multimesh_render_pass = 0;
  266. };
  267. uint32_t blend_shape_count = 0;
  268. RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED;
  269. Surface **surfaces = nullptr;
  270. uint32_t surface_count = 0;
  271. Vector<AABB> bone_aabbs;
  272. AABB aabb;
  273. AABB custom_aabb;
  274. Vector<RID> material_cache;
  275. RasterizerScene::InstanceDependency instance_dependency;
  276. };
  277. mutable RID_Owner<Mesh> mesh_owner;
  278. void _mesh_surface_generate_version_for_input_mask(Mesh::Surface *s, uint32_t p_input_mask);
  279. RID mesh_default_rd_buffers[DEFAULT_RD_BUFFER_MAX];
  280. /* MultiMesh */
  281. struct MultiMesh {
  282. RID mesh;
  283. int instances = 0;
  284. RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D;
  285. bool uses_colors = false;
  286. bool uses_custom_data = false;
  287. int visible_instances = -1;
  288. AABB aabb;
  289. bool aabb_dirty = false;
  290. bool buffer_set = false;
  291. uint32_t stride_cache = 0;
  292. uint32_t color_offset_cache = 0;
  293. uint32_t custom_data_offset_cache = 0;
  294. Vector<float> data_cache; //used if individual setting is used
  295. bool *data_cache_dirty_regions = nullptr;
  296. uint32_t data_cache_used_dirty_regions = 0;
  297. RID buffer; //storage buffer
  298. RID uniform_set_3d;
  299. bool dirty = false;
  300. MultiMesh *dirty_list = nullptr;
  301. RasterizerScene::InstanceDependency instance_dependency;
  302. };
  303. mutable RID_Owner<MultiMesh> multimesh_owner;
  304. MultiMesh *multimesh_dirty_list = nullptr;
  305. _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const;
  306. _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb);
  307. _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb);
  308. _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances);
  309. void _update_dirty_multimeshes();
  310. /* Skeleton */
  311. struct Skeleton {
  312. bool use_2d = false;
  313. int size = 0;
  314. Vector<float> data;
  315. RID buffer;
  316. bool dirty = false;
  317. Skeleton *dirty_list = nullptr;
  318. Transform2D base_transform_2d;
  319. RID uniform_set_3d;
  320. RasterizerScene::InstanceDependency instance_dependency;
  321. };
  322. mutable RID_Owner<Skeleton> skeleton_owner;
  323. _FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton);
  324. Skeleton *skeleton_dirty_list = nullptr;
  325. void _update_dirty_skeletons();
  326. /* LIGHT */
  327. struct Light {
  328. RS::LightType type;
  329. float param[RS::LIGHT_PARAM_MAX];
  330. Color color = Color(1, 1, 1, 1);
  331. Color shadow_color;
  332. RID projector;
  333. bool shadow = false;
  334. bool negative = false;
  335. bool reverse_cull = false;
  336. RS::LightBakeMode bake_mode = RS::LIGHT_BAKE_DYNAMIC;
  337. uint32_t max_sdfgi_cascade = 2;
  338. uint32_t cull_mask = 0xFFFFFFFF;
  339. RS::LightOmniShadowMode omni_shadow_mode = RS::LIGHT_OMNI_SHADOW_DUAL_PARABOLOID;
  340. RS::LightDirectionalShadowMode directional_shadow_mode = RS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL;
  341. RS::LightDirectionalShadowDepthRangeMode directional_range_mode = RS::LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE;
  342. bool directional_blend_splits = false;
  343. uint64_t version = 0;
  344. RasterizerScene::InstanceDependency instance_dependency;
  345. };
  346. mutable RID_Owner<Light> light_owner;
  347. /* REFLECTION PROBE */
  348. struct ReflectionProbe {
  349. RS::ReflectionProbeUpdateMode update_mode = RS::REFLECTION_PROBE_UPDATE_ONCE;
  350. int resolution = 256;
  351. float intensity = 1.0;
  352. RS::ReflectionProbeAmbientMode ambient_mode = RS::REFLECTION_PROBE_AMBIENT_ENVIRONMENT;
  353. Color ambient_color;
  354. float ambient_color_energy = 1.0;
  355. float max_distance = 0;
  356. Vector3 extents = Vector3(1, 1, 1);
  357. Vector3 origin_offset;
  358. bool interior = false;
  359. bool box_projection = false;
  360. bool enable_shadows = false;
  361. uint32_t cull_mask = (1 << 20) - 1;
  362. RasterizerScene::InstanceDependency instance_dependency;
  363. };
  364. mutable RID_Owner<ReflectionProbe> reflection_probe_owner;
  365. /* DECAL */
  366. struct Decal {
  367. Vector3 extents = Vector3(1, 1, 1);
  368. RID textures[RS::DECAL_TEXTURE_MAX];
  369. float emission_energy = 1.0;
  370. float albedo_mix = 1.0;
  371. Color modulate = Color(1, 1, 1, 1);
  372. uint32_t cull_mask = (1 << 20) - 1;
  373. float upper_fade = 0.3;
  374. float lower_fade = 0.3;
  375. bool distance_fade = false;
  376. float distance_fade_begin = 10;
  377. float distance_fade_length = 1;
  378. float normal_fade = 0.0;
  379. RasterizerScene::InstanceDependency instance_dependency;
  380. };
  381. mutable RID_Owner<Decal> decal_owner;
  382. /* GI PROBE */
  383. struct GIProbe {
  384. RID octree_buffer;
  385. RID data_buffer;
  386. RID sdf_texture;
  387. uint32_t octree_buffer_size = 0;
  388. uint32_t data_buffer_size = 0;
  389. Vector<int> level_counts;
  390. int cell_count = 0;
  391. Transform to_cell_xform;
  392. AABB bounds;
  393. Vector3i octree_size;
  394. float dynamic_range = 4.0;
  395. float energy = 1.0;
  396. float ao = 0.0;
  397. float ao_size = 0.5;
  398. float bias = 1.4;
  399. float normal_bias = 0.0;
  400. float propagation = 0.7;
  401. bool interior = false;
  402. bool use_two_bounces = false;
  403. float anisotropy_strength = 0.5;
  404. uint32_t version = 1;
  405. uint32_t data_version = 1;
  406. RasterizerScene::InstanceDependency instance_dependency;
  407. };
  408. GiprobeSdfShaderRD giprobe_sdf_shader;
  409. RID giprobe_sdf_shader_version;
  410. RID giprobe_sdf_shader_version_shader;
  411. RID giprobe_sdf_shader_pipeline;
  412. mutable RID_Owner<GIProbe> gi_probe_owner;
  413. /* REFLECTION PROBE */
  414. struct Lightmap {
  415. RID light_texture;
  416. bool uses_spherical_harmonics = false;
  417. bool interior = false;
  418. AABB bounds = AABB(Vector3(), Vector3(1, 1, 1));
  419. int32_t array_index = -1; //unassigned
  420. PackedVector3Array points;
  421. PackedColorArray point_sh;
  422. PackedInt32Array tetrahedra;
  423. PackedInt32Array bsp_tree;
  424. struct BSP {
  425. static const int32_t EMPTY_LEAF = INT32_MIN;
  426. float plane[4];
  427. int32_t over = EMPTY_LEAF, under = EMPTY_LEAF;
  428. };
  429. RasterizerScene::InstanceDependency instance_dependency;
  430. };
  431. bool using_lightmap_array; //high end uses this
  432. /* for high end */
  433. Vector<RID> lightmap_textures;
  434. uint64_t lightmap_array_version = 0;
  435. mutable RID_Owner<Lightmap> lightmap_owner;
  436. float lightmap_probe_capture_update_speed = 4;
  437. /* RENDER TARGET */
  438. struct RenderTarget {
  439. Size2i size;
  440. RID framebuffer;
  441. RID color;
  442. //used for retrieving from CPU
  443. RD::DataFormat color_format = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  444. RD::DataFormat color_format_srgb = RD::DATA_FORMAT_R4G4_UNORM_PACK8;
  445. Image::Format image_format = Image::FORMAT_L8;
  446. bool flags[RENDER_TARGET_FLAG_MAX];
  447. RID backbuffer; //used for effects
  448. RID backbuffer_mipmap0;
  449. struct BackbufferMipmap {
  450. RID mipmap;
  451. RID mipmap_copy;
  452. };
  453. Vector<BackbufferMipmap> backbuffer_mipmaps;
  454. RID backbuffer_uniform_set;
  455. //texture generated for this owner (nor RD).
  456. RID texture;
  457. bool was_used;
  458. //clear request
  459. bool clear_requested;
  460. Color clear_color;
  461. };
  462. RID_Owner<RenderTarget> render_target_owner;
  463. void _clear_render_target(RenderTarget *rt);
  464. void _update_render_target(RenderTarget *rt);
  465. void _create_render_target_backbuffer(RenderTarget *rt);
  466. /* GLOBAL SHADER VARIABLES */
  467. struct GlobalVariables {
  468. enum {
  469. BUFFER_DIRTY_REGION_SIZE = 1024
  470. };
  471. struct Variable {
  472. Set<RID> texture_materials; // materials using this
  473. RS::GlobalVariableType type;
  474. Variant value;
  475. Variant override;
  476. int32_t buffer_index; //for vectors
  477. int32_t buffer_elements; //for vectors
  478. };
  479. HashMap<StringName, Variable> variables;
  480. struct Value {
  481. float x;
  482. float y;
  483. float z;
  484. float w;
  485. };
  486. struct ValueInt {
  487. int32_t x;
  488. int32_t y;
  489. int32_t z;
  490. int32_t w;
  491. };
  492. struct ValueUInt {
  493. uint32_t x;
  494. uint32_t y;
  495. uint32_t z;
  496. uint32_t w;
  497. };
  498. struct ValueUsage {
  499. uint32_t elements = 0;
  500. };
  501. List<RID> materials_using_buffer;
  502. List<RID> materials_using_texture;
  503. RID buffer;
  504. Value *buffer_values;
  505. ValueUsage *buffer_usage;
  506. bool *buffer_dirty_regions;
  507. uint32_t buffer_dirty_region_count = 0;
  508. uint32_t buffer_size;
  509. bool must_update_texture_materials = false;
  510. bool must_update_buffer_materials = false;
  511. HashMap<RID, int32_t> instance_buffer_pos;
  512. } global_variables;
  513. int32_t _global_variable_allocate(uint32_t p_elements);
  514. void _global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value);
  515. void _global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements);
  516. void _update_global_variables();
  517. /* EFFECTS */
  518. RasterizerEffectsRD effects;
  519. public:
  520. /* TEXTURE API */
  521. virtual RID texture_2d_create(const Ref<Image> &p_image);
  522. virtual RID texture_2d_layered_create(const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type);
  523. virtual RID texture_3d_create(const Vector<Ref<Image>> &p_slices); //all slices, then all the mipmaps, must be coherent
  524. virtual RID texture_proxy_create(RID p_base);
  525. virtual void _texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer, bool p_immediate);
  526. virtual void texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer = 0); //mostly used for video and streaming
  527. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  528. virtual void texture_3d_update(RID p_texture, const Ref<Image> &p_image, int p_depth, int p_mipmap);
  529. virtual void texture_proxy_update(RID p_texture, RID p_proxy_to);
  530. //these two APIs can be used together or in combination with the others.
  531. virtual RID texture_2d_placeholder_create();
  532. virtual RID texture_2d_layered_placeholder_create(RenderingServer::TextureLayeredType p_layered_type);
  533. virtual RID texture_3d_placeholder_create();
  534. virtual Ref<Image> texture_2d_get(RID p_texture) const;
  535. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const;
  536. virtual Ref<Image> texture_3d_slice_get(RID p_texture, int p_depth, int p_mipmap) const;
  537. virtual void texture_replace(RID p_texture, RID p_by_texture);
  538. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height);
  539. virtual void texture_set_path(RID p_texture, const String &p_path);
  540. virtual String texture_get_path(RID p_texture) const;
  541. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  542. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  543. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata);
  544. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info);
  545. virtual void texture_set_proxy(RID p_proxy, RID p_base);
  546. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable);
  547. virtual Size2 texture_size_with_proxy(RID p_proxy);
  548. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  549. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false);
  550. RID decal_atlas_get_texture() const;
  551. RID decal_atlas_get_texture_srgb() const;
  552. _FORCE_INLINE_ Rect2 decal_atlas_get_texture_rect(RID p_texture) {
  553. DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);
  554. if (!t) {
  555. return Rect2();
  556. }
  557. return t->uv_rect;
  558. }
  559. //internal usage
  560. _FORCE_INLINE_ RID texture_get_rd_texture(RID p_texture, bool p_srgb = false) {
  561. if (p_texture.is_null()) {
  562. return RID();
  563. }
  564. Texture *tex = texture_owner.getornull(p_texture);
  565. if (!tex) {
  566. return RID();
  567. }
  568. return (p_srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  569. }
  570. _FORCE_INLINE_ Size2i texture_2d_get_size(RID p_texture) {
  571. if (p_texture.is_null()) {
  572. return Size2i();
  573. }
  574. Texture *tex = texture_owner.getornull(p_texture);
  575. if (!tex) {
  576. return Size2i();
  577. }
  578. return Size2i(tex->width_2d, tex->height_2d);
  579. }
  580. _FORCE_INLINE_ RID texture_rd_get_default(DefaultRDTexture p_texture) {
  581. return default_rd_textures[p_texture];
  582. }
  583. _FORCE_INLINE_ RID sampler_rd_get_default(RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat) {
  584. return default_rd_samplers[p_filter][p_repeat];
  585. }
  586. /* SHADER API */
  587. RID shader_create();
  588. void shader_set_code(RID p_shader, const String &p_code);
  589. String shader_get_code(RID p_shader) const;
  590. void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  591. void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture);
  592. RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const;
  593. Variant shader_get_param_default(RID p_shader, const StringName &p_param) const;
  594. void shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function);
  595. /* COMMON MATERIAL API */
  596. RID material_create();
  597. void material_set_shader(RID p_material, RID p_shader);
  598. void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value);
  599. Variant material_get_param(RID p_material, const StringName &p_param) const;
  600. void material_set_next_pass(RID p_material, RID p_next_material);
  601. void material_set_render_priority(RID p_material, int priority);
  602. bool material_is_animated(RID p_material);
  603. bool material_casts_shadows(RID p_material);
  604. void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters);
  605. void material_update_dependency(RID p_material, RasterizerScene::InstanceBase *p_instance);
  606. void material_force_update_textures(RID p_material, ShaderType p_shader_type);
  607. void material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function);
  608. _FORCE_INLINE_ MaterialData *material_get_data(RID p_material, ShaderType p_shader_type) {
  609. Material *material = material_owner.getornull(p_material);
  610. if (!material || material->shader_type != p_shader_type) {
  611. return nullptr;
  612. } else {
  613. return material->data;
  614. }
  615. }
  616. /* MESH API */
  617. virtual RID mesh_create();
  618. /// Return stride
  619. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface);
  620. virtual int mesh_get_blend_shape_count(RID p_mesh) const;
  621. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode);
  622. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const;
  623. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data);
  624. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material);
  625. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  626. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const;
  627. virtual int mesh_get_surface_count(RID p_mesh) const;
  628. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb);
  629. virtual AABB mesh_get_custom_aabb(RID p_mesh) const;
  630. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID());
  631. virtual void mesh_clear(RID p_mesh);
  632. _FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) {
  633. Mesh *mesh = mesh_owner.getornull(p_mesh);
  634. ERR_FAIL_COND_V(!mesh, nullptr);
  635. r_surface_count = mesh->surface_count;
  636. if (r_surface_count == 0) {
  637. return nullptr;
  638. }
  639. if (mesh->material_cache.empty()) {
  640. mesh->material_cache.resize(mesh->surface_count);
  641. for (uint32_t i = 0; i < r_surface_count; i++) {
  642. mesh->material_cache.write[i] = mesh->surfaces[i]->material;
  643. }
  644. }
  645. return mesh->material_cache.ptr();
  646. }
  647. _FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(RID p_mesh, uint32_t p_surface_index) {
  648. Mesh *mesh = mesh_owner.getornull(p_mesh);
  649. ERR_FAIL_COND_V(!mesh, RS::PRIMITIVE_MAX);
  650. ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, RS::PRIMITIVE_MAX);
  651. return mesh->surfaces[p_surface_index]->primitive;
  652. }
  653. _FORCE_INLINE_ void mesh_surface_get_arrays_and_format(RID p_mesh, uint32_t p_surface_index, uint32_t p_input_mask, RID &r_vertex_array_rd, RID &r_index_array_rd, RD::VertexFormatID &r_vertex_format) {
  654. Mesh *mesh = mesh_owner.getornull(p_mesh);
  655. ERR_FAIL_COND(!mesh);
  656. ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count);
  657. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  658. r_index_array_rd = s->index_array;
  659. s->version_lock.lock();
  660. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  661. for (uint32_t i = 0; i < s->version_count; i++) {
  662. if (s->versions[i].input_mask != p_input_mask) {
  663. continue;
  664. }
  665. //we have this version, hooray
  666. r_vertex_format = s->versions[i].vertex_format;
  667. r_vertex_array_rd = s->versions[i].vertex_array;
  668. s->version_lock.unlock();
  669. return;
  670. }
  671. uint32_t version = s->version_count; //gets added at the end
  672. _mesh_surface_generate_version_for_input_mask(s, p_input_mask);
  673. r_vertex_format = s->versions[version].vertex_format;
  674. r_vertex_array_rd = s->versions[version].vertex_array;
  675. s->version_lock.unlock();
  676. }
  677. _FORCE_INLINE_ RID mesh_get_default_rd_buffer(DefaultRDBuffer p_buffer) {
  678. ERR_FAIL_INDEX_V(p_buffer, DEFAULT_RD_BUFFER_MAX, RID());
  679. return mesh_default_rd_buffers[p_buffer];
  680. }
  681. _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) {
  682. Mesh *mesh = mesh_owner.getornull(p_mesh);
  683. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  684. if (s->render_pass != p_render_pass) {
  685. (*r_index)++;
  686. s->render_pass = p_render_pass;
  687. s->render_index = *r_index;
  688. }
  689. return s->render_index;
  690. }
  691. _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) {
  692. Mesh *mesh = mesh_owner.getornull(p_mesh);
  693. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  694. if (s->multimesh_render_pass != p_render_pass) {
  695. (*r_index)++;
  696. s->multimesh_render_pass = p_render_pass;
  697. s->multimesh_render_index = *r_index;
  698. }
  699. return s->multimesh_render_index;
  700. }
  701. /* MULTIMESH API */
  702. RID multimesh_create();
  703. void multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false);
  704. int multimesh_get_instance_count(RID p_multimesh) const;
  705. void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  706. void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform);
  707. void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  708. void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  709. void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color);
  710. RID multimesh_get_mesh(RID p_multimesh) const;
  711. Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  712. Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  713. Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  714. Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  715. void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer);
  716. Vector<float> multimesh_get_buffer(RID p_multimesh) const;
  717. void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  718. int multimesh_get_visible_instances(RID p_multimesh) const;
  719. AABB multimesh_get_aabb(RID p_multimesh) const;
  720. _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const {
  721. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  722. return multimesh->xform_format;
  723. }
  724. _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const {
  725. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  726. return multimesh->uses_colors;
  727. }
  728. _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const {
  729. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  730. return multimesh->uses_custom_data;
  731. }
  732. _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const {
  733. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  734. if (multimesh->visible_instances >= 0) {
  735. return multimesh->visible_instances;
  736. }
  737. return multimesh->instances;
  738. }
  739. _FORCE_INLINE_ RID multimesh_get_3d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const {
  740. MultiMesh *multimesh = multimesh_owner.getornull(p_multimesh);
  741. if (!multimesh->uniform_set_3d.is_valid()) {
  742. Vector<RD::Uniform> uniforms;
  743. RD::Uniform u;
  744. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  745. u.binding = 0;
  746. u.ids.push_back(multimesh->buffer);
  747. uniforms.push_back(u);
  748. multimesh->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  749. }
  750. return multimesh->uniform_set_3d;
  751. }
  752. /* IMMEDIATE API */
  753. RID immediate_create() { return RID(); }
  754. void immediate_begin(RID p_immediate, RS::PrimitiveType p_rimitive, RID p_texture = RID()) {}
  755. void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) {}
  756. void immediate_normal(RID p_immediate, const Vector3 &p_normal) {}
  757. void immediate_tangent(RID p_immediate, const Plane &p_tangent) {}
  758. void immediate_color(RID p_immediate, const Color &p_color) {}
  759. void immediate_uv(RID p_immediate, const Vector2 &tex_uv) {}
  760. void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) {}
  761. void immediate_end(RID p_immediate) {}
  762. void immediate_clear(RID p_immediate) {}
  763. void immediate_set_material(RID p_immediate, RID p_material) {}
  764. RID immediate_get_material(RID p_immediate) const { return RID(); }
  765. AABB immediate_get_aabb(RID p_immediate) const { return AABB(); }
  766. /* SKELETON API */
  767. RID skeleton_create();
  768. void skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton = false);
  769. void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform);
  770. void skeleton_set_world_transform(RID p_skeleton, bool p_enable, const Transform &p_world_transform);
  771. int skeleton_get_bone_count(RID p_skeleton) const;
  772. void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform);
  773. Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const;
  774. void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform);
  775. Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const;
  776. _FORCE_INLINE_ RID skeleton_get_3d_uniform_set(RID p_skeleton, RID p_shader, uint32_t p_set) const {
  777. Skeleton *skeleton = skeleton_owner.getornull(p_skeleton);
  778. ERR_FAIL_COND_V(!skeleton, RID());
  779. ERR_FAIL_COND_V(skeleton->size == 0, RID());
  780. if (skeleton->use_2d) {
  781. return RID();
  782. }
  783. if (!skeleton->uniform_set_3d.is_valid()) {
  784. Vector<RD::Uniform> uniforms;
  785. RD::Uniform u;
  786. u.type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  787. u.binding = 0;
  788. u.ids.push_back(skeleton->buffer);
  789. uniforms.push_back(u);
  790. skeleton->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set);
  791. }
  792. return skeleton->uniform_set_3d;
  793. }
  794. /* Light API */
  795. RID light_create(RS::LightType p_type);
  796. RID directional_light_create() { return light_create(RS::LIGHT_DIRECTIONAL); }
  797. RID omni_light_create() { return light_create(RS::LIGHT_OMNI); }
  798. RID spot_light_create() { return light_create(RS::LIGHT_SPOT); }
  799. void light_set_color(RID p_light, const Color &p_color);
  800. void light_set_param(RID p_light, RS::LightParam p_param, float p_value);
  801. void light_set_shadow(RID p_light, bool p_enabled);
  802. void light_set_shadow_color(RID p_light, const Color &p_color);
  803. void light_set_projector(RID p_light, RID p_texture);
  804. void light_set_negative(RID p_light, bool p_enable);
  805. void light_set_cull_mask(RID p_light, uint32_t p_mask);
  806. void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled);
  807. void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode);
  808. void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade);
  809. void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode);
  810. void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode);
  811. void light_directional_set_blend_splits(RID p_light, bool p_enable);
  812. bool light_directional_get_blend_splits(RID p_light) const;
  813. void light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode);
  814. RS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const;
  815. RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light);
  816. RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light);
  817. _FORCE_INLINE_ RS::LightType light_get_type(RID p_light) const {
  818. const Light *light = light_owner.getornull(p_light);
  819. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  820. return light->type;
  821. }
  822. AABB light_get_aabb(RID p_light) const;
  823. _FORCE_INLINE_ float light_get_param(RID p_light, RS::LightParam p_param) {
  824. const Light *light = light_owner.getornull(p_light);
  825. ERR_FAIL_COND_V(!light, 0);
  826. return light->param[p_param];
  827. }
  828. _FORCE_INLINE_ RID light_get_projector(RID p_light) {
  829. const Light *light = light_owner.getornull(p_light);
  830. ERR_FAIL_COND_V(!light, RID());
  831. return light->projector;
  832. }
  833. _FORCE_INLINE_ Color light_get_color(RID p_light) {
  834. const Light *light = light_owner.getornull(p_light);
  835. ERR_FAIL_COND_V(!light, Color());
  836. return light->color;
  837. }
  838. _FORCE_INLINE_ Color light_get_shadow_color(RID p_light) {
  839. const Light *light = light_owner.getornull(p_light);
  840. ERR_FAIL_COND_V(!light, Color());
  841. return light->shadow_color;
  842. }
  843. _FORCE_INLINE_ uint32_t light_get_cull_mask(RID p_light) {
  844. const Light *light = light_owner.getornull(p_light);
  845. ERR_FAIL_COND_V(!light, 0);
  846. return light->cull_mask;
  847. }
  848. _FORCE_INLINE_ bool light_has_shadow(RID p_light) const {
  849. const Light *light = light_owner.getornull(p_light);
  850. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  851. return light->shadow;
  852. }
  853. _FORCE_INLINE_ bool light_is_negative(RID p_light) const {
  854. const Light *light = light_owner.getornull(p_light);
  855. ERR_FAIL_COND_V(!light, RS::LIGHT_DIRECTIONAL);
  856. return light->negative;
  857. }
  858. _FORCE_INLINE_ float light_get_transmittance_bias(RID p_light) const {
  859. const Light *light = light_owner.getornull(p_light);
  860. ERR_FAIL_COND_V(!light, 0.0);
  861. return light->param[RS::LIGHT_PARAM_TRANSMITTANCE_BIAS];
  862. }
  863. RS::LightBakeMode light_get_bake_mode(RID p_light);
  864. uint32_t light_get_max_sdfgi_cascade(RID p_light);
  865. uint64_t light_get_version(RID p_light) const;
  866. /* PROBE API */
  867. RID reflection_probe_create();
  868. void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode);
  869. void reflection_probe_set_intensity(RID p_probe, float p_intensity);
  870. void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode);
  871. void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color);
  872. void reflection_probe_set_ambient_energy(RID p_probe, float p_energy);
  873. void reflection_probe_set_max_distance(RID p_probe, float p_distance);
  874. void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents);
  875. void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset);
  876. void reflection_probe_set_as_interior(RID p_probe, bool p_enable);
  877. void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable);
  878. void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable);
  879. void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers);
  880. void reflection_probe_set_resolution(RID p_probe, int p_resolution);
  881. AABB reflection_probe_get_aabb(RID p_probe) const;
  882. RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const;
  883. uint32_t reflection_probe_get_cull_mask(RID p_probe) const;
  884. Vector3 reflection_probe_get_extents(RID p_probe) const;
  885. Vector3 reflection_probe_get_origin_offset(RID p_probe) const;
  886. float reflection_probe_get_origin_max_distance(RID p_probe) const;
  887. int reflection_probe_get_resolution(RID p_probe) const;
  888. bool reflection_probe_renders_shadows(RID p_probe) const;
  889. float reflection_probe_get_intensity(RID p_probe) const;
  890. bool reflection_probe_is_interior(RID p_probe) const;
  891. bool reflection_probe_is_box_projection(RID p_probe) const;
  892. RS::ReflectionProbeAmbientMode reflection_probe_get_ambient_mode(RID p_probe) const;
  893. Color reflection_probe_get_ambient_color(RID p_probe) const;
  894. float reflection_probe_get_ambient_color_energy(RID p_probe) const;
  895. void base_update_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance);
  896. void skeleton_update_dependency(RID p_skeleton, RasterizerScene::InstanceBase *p_instance);
  897. /* DECAL API */
  898. virtual RID decal_create();
  899. virtual void decal_set_extents(RID p_decal, const Vector3 &p_extents);
  900. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture);
  901. virtual void decal_set_emission_energy(RID p_decal, float p_energy);
  902. virtual void decal_set_albedo_mix(RID p_decal, float p_mix);
  903. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate);
  904. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers);
  905. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length);
  906. virtual void decal_set_fade(RID p_decal, float p_above, float p_below);
  907. virtual void decal_set_normal_fade(RID p_decal, float p_fade);
  908. _FORCE_INLINE_ Vector3 decal_get_extents(RID p_decal) {
  909. const Decal *decal = decal_owner.getornull(p_decal);
  910. return decal->extents;
  911. }
  912. _FORCE_INLINE_ RID decal_get_texture(RID p_decal, RS::DecalTexture p_texture) {
  913. const Decal *decal = decal_owner.getornull(p_decal);
  914. return decal->textures[p_texture];
  915. }
  916. _FORCE_INLINE_ Color decal_get_modulate(RID p_decal) {
  917. const Decal *decal = decal_owner.getornull(p_decal);
  918. return decal->modulate;
  919. }
  920. _FORCE_INLINE_ float decal_get_emission_energy(RID p_decal) {
  921. const Decal *decal = decal_owner.getornull(p_decal);
  922. return decal->emission_energy;
  923. }
  924. _FORCE_INLINE_ float decal_get_albedo_mix(RID p_decal) {
  925. const Decal *decal = decal_owner.getornull(p_decal);
  926. return decal->albedo_mix;
  927. }
  928. _FORCE_INLINE_ uint32_t decal_get_cull_mask(RID p_decal) {
  929. const Decal *decal = decal_owner.getornull(p_decal);
  930. return decal->cull_mask;
  931. }
  932. _FORCE_INLINE_ float decal_get_upper_fade(RID p_decal) {
  933. const Decal *decal = decal_owner.getornull(p_decal);
  934. return decal->upper_fade;
  935. }
  936. _FORCE_INLINE_ float decal_get_lower_fade(RID p_decal) {
  937. const Decal *decal = decal_owner.getornull(p_decal);
  938. return decal->lower_fade;
  939. }
  940. _FORCE_INLINE_ float decal_get_normal_fade(RID p_decal) {
  941. const Decal *decal = decal_owner.getornull(p_decal);
  942. return decal->normal_fade;
  943. }
  944. _FORCE_INLINE_ bool decal_is_distance_fade_enabled(RID p_decal) {
  945. const Decal *decal = decal_owner.getornull(p_decal);
  946. return decal->distance_fade;
  947. }
  948. _FORCE_INLINE_ float decal_get_distance_fade_begin(RID p_decal) {
  949. const Decal *decal = decal_owner.getornull(p_decal);
  950. return decal->distance_fade_begin;
  951. }
  952. _FORCE_INLINE_ float decal_get_distance_fade_length(RID p_decal) {
  953. const Decal *decal = decal_owner.getornull(p_decal);
  954. return decal->distance_fade_length;
  955. }
  956. virtual AABB decal_get_aabb(RID p_decal) const;
  957. /* GI PROBE API */
  958. RID gi_probe_create();
  959. void gi_probe_allocate(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);
  960. AABB gi_probe_get_bounds(RID p_gi_probe) const;
  961. Vector3i gi_probe_get_octree_size(RID p_gi_probe) const;
  962. Vector<uint8_t> gi_probe_get_octree_cells(RID p_gi_probe) const;
  963. Vector<uint8_t> gi_probe_get_data_cells(RID p_gi_probe) const;
  964. Vector<uint8_t> gi_probe_get_distance_field(RID p_gi_probe) const;
  965. Vector<int> gi_probe_get_level_counts(RID p_gi_probe) const;
  966. Transform gi_probe_get_to_cell_xform(RID p_gi_probe) const;
  967. void gi_probe_set_dynamic_range(RID p_gi_probe, float p_range);
  968. float gi_probe_get_dynamic_range(RID p_gi_probe) const;
  969. void gi_probe_set_propagation(RID p_gi_probe, float p_range);
  970. float gi_probe_get_propagation(RID p_gi_probe) const;
  971. void gi_probe_set_energy(RID p_gi_probe, float p_energy);
  972. float gi_probe_get_energy(RID p_gi_probe) const;
  973. void gi_probe_set_ao(RID p_gi_probe, float p_ao);
  974. float gi_probe_get_ao(RID p_gi_probe) const;
  975. void gi_probe_set_ao_size(RID p_gi_probe, float p_strength);
  976. float gi_probe_get_ao_size(RID p_gi_probe) const;
  977. void gi_probe_set_bias(RID p_gi_probe, float p_bias);
  978. float gi_probe_get_bias(RID p_gi_probe) const;
  979. void gi_probe_set_normal_bias(RID p_gi_probe, float p_range);
  980. float gi_probe_get_normal_bias(RID p_gi_probe) const;
  981. void gi_probe_set_interior(RID p_gi_probe, bool p_enable);
  982. bool gi_probe_is_interior(RID p_gi_probe) const;
  983. void gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable);
  984. bool gi_probe_is_using_two_bounces(RID p_gi_probe) const;
  985. void gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength);
  986. float gi_probe_get_anisotropy_strength(RID p_gi_probe) const;
  987. uint32_t gi_probe_get_version(RID p_probe);
  988. uint32_t gi_probe_get_data_version(RID p_probe);
  989. RID gi_probe_get_octree_buffer(RID p_gi_probe) const;
  990. RID gi_probe_get_data_buffer(RID p_gi_probe) const;
  991. RID gi_probe_get_sdf_texture(RID p_gi_probe);
  992. /* LIGHTMAP CAPTURE */
  993. virtual RID lightmap_create();
  994. virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics);
  995. virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds);
  996. virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior);
  997. 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);
  998. virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const;
  999. virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const;
  1000. virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const;
  1001. virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const;
  1002. virtual AABB lightmap_get_aabb(RID p_lightmap) const;
  1003. virtual bool lightmap_is_interior(RID p_lightmap) const;
  1004. virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh);
  1005. virtual void lightmap_set_probe_capture_update_speed(float p_speed);
  1006. _FORCE_INLINE_ float lightmap_get_probe_capture_update_speed() const {
  1007. return lightmap_probe_capture_update_speed;
  1008. }
  1009. _FORCE_INLINE_ int32_t lightmap_get_array_index(RID p_lightmap) const {
  1010. ERR_FAIL_COND_V(!using_lightmap_array, -1); //only for arrays
  1011. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  1012. return lm->array_index;
  1013. }
  1014. _FORCE_INLINE_ bool lightmap_uses_spherical_harmonics(RID p_lightmap) const {
  1015. ERR_FAIL_COND_V(!using_lightmap_array, false); //only for arrays
  1016. const Lightmap *lm = lightmap_owner.getornull(p_lightmap);
  1017. return lm->uses_spherical_harmonics;
  1018. }
  1019. _FORCE_INLINE_ uint64_t lightmap_array_get_version() const {
  1020. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1021. return lightmap_array_version;
  1022. }
  1023. _FORCE_INLINE_ int lightmap_array_get_size() const {
  1024. ERR_FAIL_COND_V(!using_lightmap_array, 0); //only for arrays
  1025. return lightmap_textures.size();
  1026. }
  1027. _FORCE_INLINE_ const Vector<RID> &lightmap_array_get_textures() const {
  1028. ERR_FAIL_COND_V(!using_lightmap_array, lightmap_textures); //only for arrays
  1029. return lightmap_textures;
  1030. }
  1031. /* PARTICLES */
  1032. RID particles_create() { return RID(); }
  1033. void particles_set_emitting(RID p_particles, bool p_emitting) {}
  1034. void particles_set_amount(RID p_particles, int p_amount) {}
  1035. void particles_set_lifetime(RID p_particles, float p_lifetime) {}
  1036. void particles_set_one_shot(RID p_particles, bool p_one_shot) {}
  1037. void particles_set_pre_process_time(RID p_particles, float p_time) {}
  1038. void particles_set_explosiveness_ratio(RID p_particles, float p_ratio) {}
  1039. void particles_set_randomness_ratio(RID p_particles, float p_ratio) {}
  1040. void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) {}
  1041. void particles_set_speed_scale(RID p_particles, float p_scale) {}
  1042. void particles_set_use_local_coordinates(RID p_particles, bool p_enable) {}
  1043. void particles_set_process_material(RID p_particles, RID p_material) {}
  1044. void particles_set_fixed_fps(RID p_particles, int p_fps) {}
  1045. void particles_set_fractional_delta(RID p_particles, bool p_enable) {}
  1046. void particles_restart(RID p_particles) {}
  1047. void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) {}
  1048. void particles_set_draw_passes(RID p_particles, int p_count) {}
  1049. void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) {}
  1050. void particles_request_process(RID p_particles) {}
  1051. AABB particles_get_current_aabb(RID p_particles) { return AABB(); }
  1052. AABB particles_get_aabb(RID p_particles) const { return AABB(); }
  1053. void particles_set_emission_transform(RID p_particles, const Transform &p_transform) {}
  1054. bool particles_get_emitting(RID p_particles) { return false; }
  1055. int particles_get_draw_passes(RID p_particles) const { return 0; }
  1056. RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const { return RID(); }
  1057. virtual bool particles_is_inactive(RID p_particles) const { return false; }
  1058. /* GLOBAL VARIABLES API */
  1059. virtual void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value);
  1060. virtual void global_variable_remove(const StringName &p_name);
  1061. virtual Vector<StringName> global_variable_get_list() const;
  1062. virtual void global_variable_set(const StringName &p_name, const Variant &p_value);
  1063. virtual void global_variable_set_override(const StringName &p_name, const Variant &p_value);
  1064. virtual Variant global_variable_get(const StringName &p_name) const;
  1065. virtual RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const;
  1066. RS::GlobalVariableType global_variable_get_type_internal(const StringName &p_name) const;
  1067. virtual void global_variables_load_settings(bool p_load_textures = true);
  1068. virtual void global_variables_clear();
  1069. virtual int32_t global_variables_instance_allocate(RID p_instance);
  1070. virtual void global_variables_instance_free(RID p_instance);
  1071. virtual void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value);
  1072. RID global_variables_get_storage_buffer() const;
  1073. /* RENDER TARGET API */
  1074. RID render_target_create();
  1075. void render_target_set_position(RID p_render_target, int p_x, int p_y);
  1076. void render_target_set_size(RID p_render_target, int p_width, int p_height);
  1077. RID render_target_get_texture(RID p_render_target);
  1078. void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id);
  1079. void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value);
  1080. bool render_target_was_used(RID p_render_target);
  1081. void render_target_set_as_unused(RID p_render_target);
  1082. void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region);
  1083. RID render_target_get_back_buffer_uniform_set(RID p_render_target, RID p_base_shader);
  1084. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color);
  1085. virtual bool render_target_is_clear_requested(RID p_render_target);
  1086. virtual Color render_target_get_clear_request_color(RID p_render_target);
  1087. virtual void render_target_disable_clear_request(RID p_render_target);
  1088. virtual void render_target_do_clear_request(RID p_render_target);
  1089. Size2 render_target_get_size(RID p_render_target);
  1090. RID render_target_get_rd_framebuffer(RID p_render_target);
  1091. RID render_target_get_rd_texture(RID p_render_target);
  1092. RS::InstanceType get_base_type(RID p_rid) const;
  1093. bool free(RID p_rid);
  1094. bool has_os_feature(const String &p_feature) const;
  1095. void update_dirty_resources();
  1096. void set_debug_generate_wireframes(bool p_generate) {}
  1097. void render_info_begin_capture() {}
  1098. void render_info_end_capture() {}
  1099. int get_captured_render_info(RS::RenderInfo p_info) { return 0; }
  1100. int get_render_info(RS::RenderInfo p_info) { return 0; }
  1101. String get_video_adapter_name() const { return String(); }
  1102. String get_video_adapter_vendor() const { return String(); }
  1103. virtual void capture_timestamps_begin();
  1104. virtual void capture_timestamp(const String &p_name);
  1105. virtual uint32_t get_captured_timestamps_count() const;
  1106. virtual uint64_t get_captured_timestamps_frame() const;
  1107. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const;
  1108. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const;
  1109. virtual String get_captured_timestamp_name(uint32_t p_index) const;
  1110. static RasterizerStorageRD *base_singleton;
  1111. RasterizerEffectsRD *get_effects();
  1112. RasterizerStorageRD();
  1113. ~RasterizerStorageRD();
  1114. };
  1115. #endif // RASTERIZER_STORAGE_RD_H