rasterizer.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /**************************************************************************/
  2. /* rasterizer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_H
  31. #define RASTERIZER_H
  32. #include "core/math/camera_matrix.h"
  33. #include "core/math/transform_interpolator.h"
  34. #include "servers/visual_server.h"
  35. #include "core/self_list.h"
  36. class RasterizerScene {
  37. public:
  38. /* SHADOW ATLAS API */
  39. virtual RID shadow_atlas_create() = 0;
  40. virtual void shadow_atlas_set_size(RID p_atlas, int p_size) = 0;
  41. virtual void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) = 0;
  42. virtual bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) = 0;
  43. virtual int get_directional_light_shadow_size(RID p_light_intance) = 0;
  44. virtual void set_directional_shadow_count(int p_count) = 0;
  45. /* ENVIRONMENT API */
  46. virtual RID environment_create() = 0;
  47. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg) = 0;
  48. virtual void environment_set_sky(RID p_env, RID p_sky) = 0;
  49. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale) = 0;
  50. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation) = 0;
  51. virtual void environment_set_bg_color(RID p_env, const Color &p_color) = 0;
  52. virtual void environment_set_bg_energy(RID p_env, float p_energy) = 0;
  53. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer) = 0;
  54. virtual void environment_set_ambient_light(RID p_env, const Color &p_color, float p_energy = 1.0, float p_sky_contribution = 0.0) = 0;
  55. virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) = 0;
  56. virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) = 0;
  57. virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) = 0;
  58. virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale, bool p_high_quality) = 0;
  59. virtual void environment_set_glow_map(RID p_env, float p_glow_map_strength, RID p_glow_map) = 0;
  60. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) = 0;
  61. virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance, bool p_roughness) = 0;
  62. virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_radius2, float p_intensity2, float p_bias, float p_light_affect, float p_ao_channel_affect, const Color &p_color, VS::EnvironmentSSAOQuality p_quality, VS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) = 0;
  63. virtual void environment_set_tonemap(RID p_env, VS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white, bool p_auto_exposure, float p_min_luminance, float p_max_luminance, float p_auto_exp_speed, float p_auto_exp_scale) = 0;
  64. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) = 0;
  65. virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) = 0;
  66. virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) = 0;
  67. virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) = 0;
  68. virtual bool is_environment(RID p_env) = 0;
  69. virtual VS::EnvironmentBG environment_get_background(RID p_env) = 0;
  70. virtual int environment_get_canvas_max_layer(RID p_env) = 0;
  71. struct InstanceBase : RID_Data {
  72. VS::InstanceType base_type;
  73. RID base;
  74. RID skeleton;
  75. RID material_override;
  76. RID material_overlay;
  77. // This is the main transform to be drawn with ..
  78. // This will either be the interpolated transform (when using fixed timestep interpolation)
  79. // or the ONLY transform (when not using FTI).
  80. Transform transform;
  81. // for interpolation we store the current transform (this physics tick)
  82. // and the transform in the previous tick
  83. Transform transform_curr;
  84. Transform transform_prev;
  85. int depth_layer;
  86. uint32_t layer_mask;
  87. //RID sampled_light;
  88. Vector<RID> materials;
  89. Vector<RID> light_instances;
  90. Vector<RID> reflection_probe_instances;
  91. Vector<RID> gi_probe_instances;
  92. PoolVector<float> blend_values;
  93. VS::ShadowCastingSetting cast_shadows;
  94. //fit in 32 bits
  95. bool mirror : 1;
  96. bool receive_shadows : 1;
  97. bool visible : 1;
  98. bool baked_light : 1; //this flag is only to know if it actually did use baked light
  99. bool redraw_if_visible : 1;
  100. bool on_interpolate_list : 1;
  101. bool on_interpolate_transform_list : 1;
  102. bool interpolated : 1;
  103. TransformInterpolator::Method interpolation_method : 3;
  104. // For fixed timestep interpolation.
  105. // Note 32 bits is plenty for checksum, no need for real_t
  106. float transform_checksum_curr;
  107. float transform_checksum_prev;
  108. float depth; //used for sorting
  109. SelfList<InstanceBase> dependency_item;
  110. InstanceBase *lightmap_capture;
  111. RID lightmap;
  112. Vector<Color> lightmap_capture_data; //in a array (12 values) to avoid wasting space if unused. Alpha is unused, but needed to send to shader
  113. int lightmap_slice;
  114. Rect2 lightmap_uv_rect;
  115. virtual void base_removed() = 0;
  116. virtual void base_changed(bool p_aabb, bool p_materials) = 0;
  117. InstanceBase() :
  118. dependency_item(this) {
  119. base_type = VS::INSTANCE_NONE;
  120. cast_shadows = VS::SHADOW_CASTING_SETTING_ON;
  121. receive_shadows = true;
  122. visible = true;
  123. depth_layer = 0;
  124. layer_mask = 1;
  125. baked_light = false;
  126. redraw_if_visible = false;
  127. lightmap_capture = nullptr;
  128. lightmap_slice = -1;
  129. lightmap_uv_rect = Rect2(0, 0, 1, 1);
  130. on_interpolate_list = false;
  131. on_interpolate_transform_list = false;
  132. interpolated = true;
  133. interpolation_method = TransformInterpolator::INTERP_LERP;
  134. transform_checksum_curr = 0.0;
  135. transform_checksum_prev = 0.0;
  136. }
  137. };
  138. virtual RID light_instance_create(RID p_light) = 0;
  139. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform) = 0;
  140. virtual void light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale = 1.0) = 0;
  141. virtual void light_instance_mark_visible(RID p_light_instance) = 0;
  142. virtual bool light_instances_can_render_shadow_cube() const { return true; }
  143. virtual RID reflection_atlas_create() = 0;
  144. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_size) = 0;
  145. virtual void reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv) = 0;
  146. virtual RID reflection_probe_instance_create(RID p_probe) = 0;
  147. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) = 0;
  148. virtual void reflection_probe_release_atlas_index(RID p_instance) = 0;
  149. virtual bool reflection_probe_instance_needs_redraw(RID p_instance) = 0;
  150. virtual bool reflection_probe_instance_has_reflection(RID p_instance) = 0;
  151. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) = 0;
  152. virtual bool reflection_probe_instance_postprocess_step(RID p_instance) = 0;
  153. virtual RID gi_probe_instance_create() = 0;
  154. virtual void gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data) = 0;
  155. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) = 0;
  156. virtual void gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds) = 0;
  157. virtual void render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, const int p_eye, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) = 0;
  158. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) = 0;
  159. virtual void set_scene_pass(uint64_t p_pass) = 0;
  160. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) = 0;
  161. virtual bool free(RID p_rid) = 0;
  162. virtual ~RasterizerScene() {}
  163. };
  164. class RasterizerStorage {
  165. public:
  166. /* TEXTURE API */
  167. virtual RID texture_create() = 0;
  168. virtual void texture_allocate(RID p_texture,
  169. int p_width,
  170. int p_height,
  171. int p_depth_3d,
  172. Image::Format p_format,
  173. VS::TextureType p_type,
  174. uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT) = 0;
  175. virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_level = 0) = 0;
  176. virtual void texture_set_data_partial(RID p_texture,
  177. const Ref<Image> &p_image,
  178. int src_x, int src_y,
  179. int src_w, int src_h,
  180. int dst_x, int dst_y,
  181. int p_dst_mip,
  182. int p_level = 0) = 0;
  183. virtual Ref<Image> texture_get_data(RID p_texture, int p_level = 0) const = 0;
  184. virtual void texture_set_flags(RID p_texture, uint32_t p_flags) = 0;
  185. virtual uint32_t texture_get_flags(RID p_texture) const = 0;
  186. virtual Image::Format texture_get_format(RID p_texture) const = 0;
  187. virtual VS::TextureType texture_get_type(RID p_texture) const = 0;
  188. virtual uint32_t texture_get_texid(RID p_texture) const = 0;
  189. virtual uint32_t texture_get_width(RID p_texture) const = 0;
  190. virtual uint32_t texture_get_height(RID p_texture) const = 0;
  191. virtual uint32_t texture_get_depth(RID p_texture) const = 0;
  192. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth_3d) = 0;
  193. virtual void texture_bind(RID p_texture, uint32_t p_texture_no) = 0;
  194. virtual void texture_set_path(RID p_texture, const String &p_path) = 0;
  195. virtual String texture_get_path(RID p_texture) const = 0;
  196. virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable) = 0;
  197. virtual void texture_debug_usage(List<VS::TextureInfo> *r_info) = 0;
  198. virtual RID texture_create_radiance_cubemap(RID p_source, int p_resolution = -1) const = 0;
  199. virtual void texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  200. virtual void texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  201. virtual void texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  202. virtual void textures_keep_original(bool p_enable) = 0;
  203. virtual void texture_set_proxy(RID p_proxy, RID p_base) = 0;
  204. virtual Size2 texture_size_with_proxy(RID p_texture) const = 0;
  205. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) = 0;
  206. /* SKY API */
  207. virtual RID sky_create() = 0;
  208. virtual void sky_set_texture(RID p_sky, RID p_cube_map, int p_radiance_size) = 0;
  209. /* SHADER API */
  210. virtual RID shader_create() = 0;
  211. virtual void shader_set_code(RID p_shader, const String &p_code) = 0;
  212. virtual String shader_get_code(RID p_shader) const = 0;
  213. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const = 0;
  214. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) = 0;
  215. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const = 0;
  216. virtual void shader_add_custom_define(RID p_shader, const String &p_define) = 0;
  217. virtual void shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const = 0;
  218. virtual void shader_remove_custom_define(RID p_shader, const String &p_define) = 0;
  219. virtual void set_shader_async_hidden_forbidden(bool p_forbidden) = 0;
  220. virtual bool is_shader_async_hidden_forbidden() = 0;
  221. /* COMMON MATERIAL API */
  222. virtual RID material_create() = 0;
  223. virtual void material_set_render_priority(RID p_material, int priority) = 0;
  224. virtual void material_set_shader(RID p_shader_material, RID p_shader) = 0;
  225. virtual RID material_get_shader(RID p_shader_material) const = 0;
  226. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0;
  227. virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0;
  228. virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const = 0;
  229. virtual void material_set_line_width(RID p_material, float p_width) = 0;
  230. virtual void material_set_next_pass(RID p_material, RID p_next_material) = 0;
  231. virtual bool material_is_animated(RID p_material) = 0;
  232. virtual bool material_casts_shadows(RID p_material) = 0;
  233. virtual bool material_uses_tangents(RID p_material);
  234. virtual bool material_uses_ensure_correct_normals(RID p_material);
  235. virtual void material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) = 0;
  236. virtual void material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) = 0;
  237. /* MESH API */
  238. virtual RID mesh_create() = 0;
  239. virtual void mesh_add_surface(RID p_mesh, uint32_t p_format, VS::PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes = Vector<PoolVector<uint8_t>>(), const Vector<AABB> &p_bone_aabbs = Vector<AABB>()) = 0;
  240. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_amount) = 0;
  241. virtual int mesh_get_blend_shape_count(RID p_mesh) const = 0;
  242. virtual void mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) = 0;
  243. virtual VS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const = 0;
  244. virtual void mesh_set_blend_shape_values(RID p_mesh, PoolVector<float> p_values) = 0;
  245. virtual PoolVector<float> mesh_get_blend_shape_values(RID p_mesh) const = 0;
  246. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) = 0;
  247. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
  248. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;
  249. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const = 0;
  250. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const = 0;
  251. virtual PoolVector<uint8_t> mesh_surface_get_array(RID p_mesh, int p_surface) const = 0;
  252. virtual PoolVector<uint8_t> mesh_surface_get_index_array(RID p_mesh, int p_surface) const = 0;
  253. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const = 0;
  254. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const = 0;
  255. virtual AABB mesh_surface_get_aabb(RID p_mesh, int p_surface) const = 0;
  256. virtual Vector<PoolVector<uint8_t>> mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const = 0;
  257. virtual Vector<AABB> mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const = 0;
  258. virtual void mesh_remove_surface(RID p_mesh, int p_index) = 0;
  259. virtual int mesh_get_surface_count(RID p_mesh) const = 0;
  260. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
  261. virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;
  262. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton) const = 0;
  263. virtual void mesh_clear(RID p_mesh) = 0;
  264. /* MULTIMESH API */
  265. struct MMInterpolator {
  266. VS::MultimeshTransformFormat _transform_format = VS::MULTIMESH_TRANSFORM_3D;
  267. VS::MultimeshColorFormat _color_format = VS::MULTIMESH_COLOR_NONE;
  268. VS::MultimeshCustomDataFormat _data_format = VS::MULTIMESH_CUSTOM_DATA_NONE;
  269. // in floats
  270. int _stride = 0;
  271. // Vertex format sizes in floats
  272. int _vf_size_xform = 0;
  273. int _vf_size_color = 0;
  274. int _vf_size_data = 0;
  275. // Set by allocate, can be used to prevent indexing out of range.
  276. int _num_instances = 0;
  277. // Quality determines whether to use lerp or slerp etc.
  278. int quality = 0;
  279. bool interpolated = false;
  280. bool on_interpolate_update_list = false;
  281. bool on_transform_update_list = false;
  282. PoolVector<float> _data_prev;
  283. PoolVector<float> _data_curr;
  284. PoolVector<float> _data_interpolated;
  285. };
  286. virtual RID multimesh_create();
  287. virtual void multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data = VS::MULTIMESH_CUSTOM_DATA_NONE);
  288. virtual int multimesh_get_instance_count(RID p_multimesh) const;
  289. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh);
  290. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform);
  291. virtual void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform);
  292. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color);
  293. virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color);
  294. virtual RID multimesh_get_mesh(RID p_multimesh) const;
  295. virtual Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const;
  296. virtual Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const;
  297. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const;
  298. virtual Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const;
  299. virtual void multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array);
  300. virtual void multimesh_set_as_bulk_array_interpolated(RID p_multimesh, const PoolVector<float> &p_array, const PoolVector<float> &p_array_prev);
  301. virtual void multimesh_set_physics_interpolated(RID p_multimesh, bool p_interpolated);
  302. virtual void multimesh_set_physics_interpolation_quality(RID p_multimesh, VS::MultimeshPhysicsInterpolationQuality p_quality);
  303. virtual void multimesh_instance_reset_physics_interpolation(RID p_multimesh, int p_index);
  304. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible);
  305. virtual int multimesh_get_visible_instances(RID p_multimesh) const;
  306. virtual AABB multimesh_get_aabb(RID p_multimesh) const;
  307. virtual void multimesh_attach_canvas_item(RID p_multimesh, RID p_canvas_item, bool p_attach) = 0;
  308. virtual RID _multimesh_create() = 0;
  309. virtual void _multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data = VS::MULTIMESH_CUSTOM_DATA_NONE) = 0;
  310. virtual int _multimesh_get_instance_count(RID p_multimesh) const = 0;
  311. virtual void _multimesh_set_mesh(RID p_multimesh, RID p_mesh) = 0;
  312. virtual void _multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) = 0;
  313. virtual void _multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) = 0;
  314. virtual void _multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) = 0;
  315. virtual void _multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) = 0;
  316. virtual RID _multimesh_get_mesh(RID p_multimesh) const = 0;
  317. virtual Transform _multimesh_instance_get_transform(RID p_multimesh, int p_index) const = 0;
  318. virtual Transform2D _multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const = 0;
  319. virtual Color _multimesh_instance_get_color(RID p_multimesh, int p_index) const = 0;
  320. virtual Color _multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const = 0;
  321. virtual void _multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) = 0;
  322. virtual void _multimesh_set_visible_instances(RID p_multimesh, int p_visible) = 0;
  323. virtual int _multimesh_get_visible_instances(RID p_multimesh) const = 0;
  324. virtual AABB _multimesh_get_aabb(RID p_multimesh) const = 0;
  325. // Multimesh is responsible for allocating / destroying an MMInterpolator object.
  326. // This allows shared functionality for interpolation across backends.
  327. virtual MMInterpolator *_multimesh_get_interpolator(RID p_multimesh) const = 0;
  328. private:
  329. void _multimesh_add_to_interpolation_lists(RID p_multimesh, MMInterpolator &r_mmi);
  330. public:
  331. /* IMMEDIATE API */
  332. virtual RID immediate_create() = 0;
  333. virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID()) = 0;
  334. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) = 0;
  335. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal) = 0;
  336. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent) = 0;
  337. virtual void immediate_color(RID p_immediate, const Color &p_color) = 0;
  338. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv) = 0;
  339. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) = 0;
  340. virtual void immediate_end(RID p_immediate) = 0;
  341. virtual void immediate_clear(RID p_immediate) = 0;
  342. virtual void immediate_set_material(RID p_immediate, RID p_material) = 0;
  343. virtual RID immediate_get_material(RID p_immediate) const = 0;
  344. virtual AABB immediate_get_aabb(RID p_immediate) const = 0;
  345. /* SKELETON API */
  346. virtual RID skeleton_create() = 0;
  347. virtual void skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) = 0;
  348. virtual int skeleton_get_bone_count(RID p_skeleton) const = 0;
  349. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) = 0;
  350. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const = 0;
  351. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) = 0;
  352. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const = 0;
  353. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) = 0;
  354. virtual uint32_t skeleton_get_revision(RID p_skeleton) const = 0;
  355. virtual void skeleton_attach_canvas_item(RID p_skeleton, RID p_canvas_item, bool p_attach) = 0;
  356. /* Light API */
  357. virtual RID light_create(VS::LightType p_type) = 0;
  358. RID directional_light_create() { return light_create(VS::LIGHT_DIRECTIONAL); }
  359. RID omni_light_create() { return light_create(VS::LIGHT_OMNI); }
  360. RID spot_light_create() { return light_create(VS::LIGHT_SPOT); }
  361. virtual void light_set_color(RID p_light, const Color &p_color) = 0;
  362. virtual void light_set_param(RID p_light, VS::LightParam p_param, float p_value) = 0;
  363. virtual void light_set_shadow(RID p_light, bool p_enabled) = 0;
  364. virtual void light_set_shadow_color(RID p_light, const Color &p_color) = 0;
  365. virtual void light_set_projector(RID p_light, RID p_texture) = 0;
  366. virtual void light_set_negative(RID p_light, bool p_enable) = 0;
  367. virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) = 0;
  368. virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) = 0;
  369. virtual void light_set_use_gi(RID p_light, bool p_enable) = 0;
  370. virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) = 0;
  371. virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) = 0;
  372. virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) = 0;
  373. virtual void light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) = 0;
  374. virtual void light_directional_set_blend_splits(RID p_light, bool p_enable) = 0;
  375. virtual bool light_directional_get_blend_splits(RID p_light) const = 0;
  376. virtual void light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) = 0;
  377. virtual VS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const = 0;
  378. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) = 0;
  379. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) = 0;
  380. virtual bool light_has_shadow(RID p_light) const = 0;
  381. virtual VS::LightType light_get_type(RID p_light) const = 0;
  382. virtual AABB light_get_aabb(RID p_light) const = 0;
  383. virtual float light_get_param(RID p_light, VS::LightParam p_param) = 0;
  384. virtual Color light_get_color(RID p_light) = 0;
  385. virtual bool light_get_use_gi(RID p_light) = 0;
  386. virtual VS::LightBakeMode light_get_bake_mode(RID p_light) = 0;
  387. virtual uint64_t light_get_version(RID p_light) const = 0;
  388. /* PROBE API */
  389. virtual RID reflection_probe_create() = 0;
  390. virtual void reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) = 0;
  391. virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution) = 0;
  392. virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity) = 0;
  393. virtual void reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) = 0;
  394. virtual void reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) = 0;
  395. virtual void reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) = 0;
  396. virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance) = 0;
  397. virtual void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) = 0;
  398. virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) = 0;
  399. virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable) = 0;
  400. virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) = 0;
  401. virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) = 0;
  402. virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) = 0;
  403. virtual AABB reflection_probe_get_aabb(RID p_probe) const = 0;
  404. virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const = 0;
  405. virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const = 0;
  406. virtual Vector3 reflection_probe_get_extents(RID p_probe) const = 0;
  407. virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const = 0;
  408. virtual float reflection_probe_get_origin_max_distance(RID p_probe) const = 0;
  409. virtual bool reflection_probe_renders_shadows(RID p_probe) const = 0;
  410. virtual void instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) = 0;
  411. virtual void instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) = 0;
  412. virtual void instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  413. virtual void instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  414. /* GI PROBE API */
  415. virtual RID gi_probe_create() = 0;
  416. virtual void gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) = 0;
  417. virtual AABB gi_probe_get_bounds(RID p_probe) const = 0;
  418. virtual void gi_probe_set_cell_size(RID p_probe, float p_range) = 0;
  419. virtual float gi_probe_get_cell_size(RID p_probe) const = 0;
  420. virtual void gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) = 0;
  421. virtual Transform gi_probe_get_to_cell_xform(RID p_probe) const = 0;
  422. virtual void gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) = 0;
  423. virtual PoolVector<int> gi_probe_get_dynamic_data(RID p_probe) const = 0;
  424. virtual void gi_probe_set_dynamic_range(RID p_probe, int p_range) = 0;
  425. virtual int gi_probe_get_dynamic_range(RID p_probe) const = 0;
  426. virtual void gi_probe_set_energy(RID p_probe, float p_range) = 0;
  427. virtual float gi_probe_get_energy(RID p_probe) const = 0;
  428. virtual void gi_probe_set_bias(RID p_probe, float p_range) = 0;
  429. virtual float gi_probe_get_bias(RID p_probe) const = 0;
  430. virtual void gi_probe_set_normal_bias(RID p_probe, float p_range) = 0;
  431. virtual float gi_probe_get_normal_bias(RID p_probe) const = 0;
  432. virtual void gi_probe_set_propagation(RID p_probe, float p_range) = 0;
  433. virtual float gi_probe_get_propagation(RID p_probe) const = 0;
  434. virtual void gi_probe_set_interior(RID p_probe, bool p_enable) = 0;
  435. virtual bool gi_probe_is_interior(RID p_probe) const = 0;
  436. virtual void gi_probe_set_compress(RID p_probe, bool p_enable) = 0;
  437. virtual bool gi_probe_is_compressed(RID p_probe) const = 0;
  438. virtual uint32_t gi_probe_get_version(RID p_probe) = 0;
  439. enum GIProbeCompression {
  440. GI_PROBE_UNCOMPRESSED,
  441. GI_PROBE_S3TC,
  442. GI_PROBE_ETC2
  443. };
  444. virtual RID gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) = 0;
  445. virtual void gi_probe_dynamic_data_update(RID p_gi_probe_data, int p_depth_slice, int p_slice_count, int p_mipmap, const void *p_data) = 0;
  446. /* LIGHTMAP CAPTURE */
  447. struct LightmapCaptureOctree {
  448. enum {
  449. CHILD_EMPTY = 0xFFFFFFFF
  450. };
  451. uint16_t light[6][3]; //anisotropic light
  452. float alpha;
  453. uint32_t children[8];
  454. };
  455. virtual RID lightmap_capture_create() = 0;
  456. virtual void lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) = 0;
  457. virtual AABB lightmap_capture_get_bounds(RID p_capture) const = 0;
  458. virtual void lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) = 0;
  459. virtual PoolVector<uint8_t> lightmap_capture_get_octree(RID p_capture) const = 0;
  460. virtual void lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) = 0;
  461. virtual Transform lightmap_capture_get_octree_cell_transform(RID p_capture) const = 0;
  462. virtual void lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) = 0;
  463. virtual int lightmap_capture_get_octree_cell_subdiv(RID p_capture) const = 0;
  464. virtual void lightmap_capture_set_energy(RID p_capture, float p_energy) = 0;
  465. virtual float lightmap_capture_get_energy(RID p_capture) const = 0;
  466. virtual void lightmap_capture_set_interior(RID p_capture, bool p_interior) = 0;
  467. virtual bool lightmap_capture_is_interior(RID p_capture) const = 0;
  468. virtual const PoolVector<LightmapCaptureOctree> *lightmap_capture_get_octree_ptr(RID p_capture) const = 0;
  469. /* PARTICLES */
  470. virtual RID particles_create() = 0;
  471. virtual void particles_set_emitting(RID p_particles, bool p_emitting) = 0;
  472. virtual bool particles_get_emitting(RID p_particles) = 0;
  473. virtual void particles_set_amount(RID p_particles, int p_amount) = 0;
  474. virtual void particles_set_lifetime(RID p_particles, float p_lifetime) = 0;
  475. virtual void particles_set_one_shot(RID p_particles, bool p_one_shot) = 0;
  476. virtual void particles_set_pre_process_time(RID p_particles, float p_time) = 0;
  477. virtual void particles_set_explosiveness_ratio(RID p_particles, float p_ratio) = 0;
  478. virtual void particles_set_randomness_ratio(RID p_particles, float p_ratio) = 0;
  479. virtual void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) = 0;
  480. virtual void particles_set_speed_scale(RID p_particles, float p_scale) = 0;
  481. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable) = 0;
  482. virtual void particles_set_process_material(RID p_particles, RID p_material) = 0;
  483. virtual void particles_set_fixed_fps(RID p_particles, int p_fps) = 0;
  484. virtual void particles_set_fractional_delta(RID p_particles, bool p_enable) = 0;
  485. virtual void particles_restart(RID p_particles) = 0;
  486. virtual bool particles_is_inactive(RID p_particles) const = 0;
  487. virtual void particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) = 0;
  488. virtual void particles_set_draw_passes(RID p_particles, int p_count) = 0;
  489. virtual void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) = 0;
  490. virtual void particles_request_process(RID p_particles) = 0;
  491. virtual AABB particles_get_current_aabb(RID p_particles) = 0;
  492. virtual AABB particles_get_aabb(RID p_particles) const = 0;
  493. virtual void particles_set_emission_transform(RID p_particles, const Transform &p_transform) = 0;
  494. virtual int particles_get_draw_passes(RID p_particles) const = 0;
  495. virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const = 0;
  496. /* RENDER TARGET */
  497. enum RenderTargetFlags {
  498. RENDER_TARGET_VFLIP,
  499. RENDER_TARGET_TRANSPARENT,
  500. RENDER_TARGET_NO_3D_EFFECTS,
  501. RENDER_TARGET_NO_3D,
  502. RENDER_TARGET_NO_SAMPLING,
  503. RENDER_TARGET_HDR,
  504. RENDER_TARGET_KEEP_3D_LINEAR,
  505. RENDER_TARGET_DIRECT_TO_SCREEN,
  506. RENDER_TARGET_USE_32_BPC_DEPTH,
  507. RENDER_TARGET_FLAG_MAX
  508. };
  509. virtual RID render_target_create() = 0;
  510. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) = 0;
  511. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height) = 0;
  512. virtual RID render_target_get_texture(RID p_render_target) const = 0;
  513. virtual uint32_t render_target_get_depth_texture_id(RID p_render_target) const = 0;
  514. virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id, unsigned int p_depth_id) = 0;
  515. virtual void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) = 0;
  516. virtual bool render_target_was_used(RID p_render_target) = 0;
  517. virtual void render_target_clear_used(RID p_render_target) = 0;
  518. virtual void render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) = 0;
  519. virtual void render_target_set_use_fxaa(RID p_render_target, bool p_fxaa) = 0;
  520. virtual void render_target_set_use_debanding(RID p_render_target, bool p_debanding) = 0;
  521. virtual void render_target_set_sharpen_intensity(RID p_render_target, float p_intensity) = 0;
  522. /* CANVAS SHADOW */
  523. virtual RID canvas_light_shadow_buffer_create(int p_width) = 0;
  524. /* LIGHT SHADOW MAPPING */
  525. virtual RID canvas_light_occluder_create() = 0;
  526. virtual void canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) = 0;
  527. /* INTERPOLATION */
  528. struct InterpolationData {
  529. void notify_free_multimesh(RID p_rid);
  530. LocalVector<RID> multimesh_interpolate_update_list;
  531. LocalVector<RID> multimesh_transform_update_lists[2];
  532. LocalVector<RID> *multimesh_transform_update_list_curr = &multimesh_transform_update_lists[0];
  533. LocalVector<RID> *multimesh_transform_update_list_prev = &multimesh_transform_update_lists[1];
  534. } _interpolation_data;
  535. void update_interpolation_tick(bool p_process = true);
  536. void update_interpolation_frame(bool p_process = true);
  537. private:
  538. _FORCE_INLINE_ void _interpolate_RGBA8(const uint8_t *p_a, const uint8_t *p_b, uint8_t *r_dest, float p_f) const;
  539. public:
  540. virtual VS::InstanceType get_base_type(RID p_rid) const = 0;
  541. virtual bool free(RID p_rid) = 0;
  542. virtual bool has_os_feature(const String &p_feature) const = 0;
  543. virtual void update_dirty_resources() = 0;
  544. virtual void set_debug_generate_wireframes(bool p_generate) = 0;
  545. virtual void render_info_begin_capture() = 0;
  546. virtual void render_info_end_capture() = 0;
  547. virtual int get_captured_render_info(VS::RenderInfo p_info) = 0;
  548. virtual uint64_t get_render_info(VS::RenderInfo p_info) = 0;
  549. virtual String get_video_adapter_name() const = 0;
  550. virtual String get_video_adapter_vendor() const = 0;
  551. static RasterizerStorage *base_singleton;
  552. RasterizerStorage();
  553. virtual ~RasterizerStorage() {}
  554. };
  555. class RasterizerCanvas {
  556. public:
  557. enum CanvasRectFlags {
  558. CANVAS_RECT_REGION = 1,
  559. CANVAS_RECT_TILE = 2,
  560. CANVAS_RECT_FLIP_H = 4,
  561. CANVAS_RECT_FLIP_V = 8,
  562. CANVAS_RECT_TRANSPOSE = 16,
  563. CANVAS_RECT_CLIP_UV = 32
  564. };
  565. struct Light : public RID_Data {
  566. bool enabled : 1;
  567. bool on_interpolate_transform_list : 1;
  568. bool interpolated : 1;
  569. Color color;
  570. Transform2D xform_curr;
  571. Transform2D xform_prev;
  572. float height;
  573. float energy;
  574. float scale;
  575. int z_min;
  576. int z_max;
  577. int layer_min;
  578. int layer_max;
  579. int item_mask;
  580. int item_shadow_mask;
  581. VS::CanvasLightMode mode;
  582. RID texture;
  583. Vector2 texture_offset;
  584. RID canvas;
  585. RID shadow_buffer;
  586. int shadow_buffer_size;
  587. float shadow_gradient_length;
  588. VS::CanvasLightShadowFilter shadow_filter;
  589. Color shadow_color;
  590. float shadow_smooth;
  591. void *texture_cache; // implementation dependent
  592. Rect2 rect_cache;
  593. Transform2D xform_cache;
  594. float radius_cache; //used for shadow far plane
  595. CameraMatrix shadow_matrix_cache;
  596. Transform2D light_shader_xform;
  597. Vector2 light_shader_pos;
  598. Light *shadows_next_ptr;
  599. Light *filter_next_ptr;
  600. Light *next_ptr;
  601. Light *mask_next_ptr;
  602. RID light_internal;
  603. Light() {
  604. enabled = true;
  605. on_interpolate_transform_list = false;
  606. interpolated = true;
  607. color = Color(1, 1, 1);
  608. shadow_color = Color(0, 0, 0, 0);
  609. height = 0;
  610. z_min = -1024;
  611. z_max = 1024;
  612. layer_min = 0;
  613. layer_max = 0;
  614. item_mask = 1;
  615. scale = 1.0;
  616. energy = 1.0;
  617. item_shadow_mask = 1;
  618. mode = VS::CANVAS_LIGHT_MODE_ADD;
  619. texture_cache = nullptr;
  620. next_ptr = nullptr;
  621. mask_next_ptr = nullptr;
  622. filter_next_ptr = nullptr;
  623. shadow_buffer_size = 2048;
  624. shadow_gradient_length = 0;
  625. shadow_filter = VS::CANVAS_LIGHT_FILTER_NONE;
  626. shadow_smooth = 0.0;
  627. }
  628. };
  629. virtual RID light_internal_create() = 0;
  630. virtual void light_internal_update(RID p_rid, Light *p_light) = 0;
  631. virtual void light_internal_free(RID p_rid) = 0;
  632. struct Item : public RID_Data {
  633. struct Command {
  634. enum Type {
  635. TYPE_LINE,
  636. TYPE_POLYLINE,
  637. TYPE_RECT,
  638. TYPE_NINEPATCH,
  639. TYPE_PRIMITIVE,
  640. TYPE_POLYGON,
  641. TYPE_MESH,
  642. TYPE_MULTIMESH,
  643. TYPE_PARTICLES,
  644. TYPE_CIRCLE,
  645. TYPE_TRANSFORM,
  646. TYPE_CLIP_IGNORE,
  647. TYPE_MULTIRECT,
  648. };
  649. virtual bool contains_reference(const RID &p_rid) const { return false; }
  650. Type type;
  651. virtual ~Command() {}
  652. };
  653. struct CommandLine : public Command {
  654. Point2 from, to;
  655. Color color;
  656. float width;
  657. bool antialiased;
  658. CommandLine() { type = TYPE_LINE; }
  659. };
  660. struct CommandPolyLine : public Command {
  661. bool antialiased;
  662. bool multiline;
  663. Vector<Point2> triangles;
  664. Vector<Color> triangle_colors;
  665. Vector<Point2> lines;
  666. Vector<Color> line_colors;
  667. CommandPolyLine() {
  668. type = TYPE_POLYLINE;
  669. antialiased = false;
  670. multiline = false;
  671. }
  672. };
  673. struct CommandRect : public Command {
  674. Rect2 rect;
  675. RID texture;
  676. RID normal_map;
  677. Color modulate;
  678. Rect2 source;
  679. uint8_t flags;
  680. CommandRect() {
  681. flags = 0;
  682. type = TYPE_RECT;
  683. }
  684. };
  685. struct CommandMultiRect : public Command {
  686. RID texture;
  687. RID normal_map;
  688. Color modulate;
  689. Vector<Rect2> rects;
  690. Vector<Rect2> sources;
  691. uint8_t flags;
  692. CommandMultiRect() {
  693. flags = 0;
  694. type = TYPE_MULTIRECT;
  695. }
  696. };
  697. struct CommandNinePatch : public Command {
  698. Rect2 rect;
  699. Rect2 source;
  700. RID texture;
  701. RID normal_map;
  702. float margin[4];
  703. bool draw_center;
  704. Color color;
  705. VS::NinePatchAxisMode axis_x;
  706. VS::NinePatchAxisMode axis_y;
  707. CommandNinePatch() {
  708. draw_center = true;
  709. type = TYPE_NINEPATCH;
  710. }
  711. };
  712. struct CommandPrimitive : public Command {
  713. Vector<Point2> points;
  714. Vector<Point2> uvs;
  715. Vector<Color> colors;
  716. RID texture;
  717. RID normal_map;
  718. float width;
  719. CommandPrimitive() {
  720. type = TYPE_PRIMITIVE;
  721. width = 1;
  722. }
  723. };
  724. struct CommandPolygon : public Command {
  725. Vector<int> indices;
  726. Vector<Point2> points;
  727. Vector<Point2> uvs;
  728. Vector<Color> colors;
  729. Vector<int> bones;
  730. Vector<float> weights;
  731. RID texture;
  732. RID normal_map;
  733. int count;
  734. bool antialiased;
  735. bool antialiasing_use_indices;
  736. struct SkinningData {
  737. bool dirty = true;
  738. LocalVector<Rect2> active_bounds;
  739. LocalVector<uint16_t> active_bone_ids;
  740. Rect2 untransformed_bound;
  741. };
  742. mutable SkinningData *skinning_data = nullptr;
  743. CommandPolygon() {
  744. type = TYPE_POLYGON;
  745. count = 0;
  746. }
  747. virtual ~CommandPolygon() {
  748. if (skinning_data) {
  749. memdelete(skinning_data);
  750. skinning_data = nullptr;
  751. }
  752. }
  753. };
  754. struct CommandMesh : public Command {
  755. RID mesh;
  756. RID texture;
  757. RID normal_map;
  758. Transform2D transform;
  759. Color modulate;
  760. CommandMesh() { type = TYPE_MESH; }
  761. };
  762. struct CommandMultiMesh : public Command {
  763. RID multimesh;
  764. RID texture;
  765. RID normal_map;
  766. RID canvas_item;
  767. virtual bool contains_reference(const RID &p_rid) const { return multimesh == p_rid; }
  768. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  769. virtual ~CommandMultiMesh() {
  770. // Remove any backlinks from multimesh to canvas item.
  771. if (multimesh.is_valid()) {
  772. RasterizerStorage::base_singleton->multimesh_attach_canvas_item(multimesh, canvas_item, false);
  773. }
  774. }
  775. };
  776. struct CommandParticles : public Command {
  777. RID particles;
  778. RID texture;
  779. RID normal_map;
  780. CommandParticles() { type = TYPE_PARTICLES; }
  781. };
  782. struct CommandCircle : public Command {
  783. Point2 pos;
  784. float radius;
  785. Color color;
  786. CommandCircle() { type = TYPE_CIRCLE; }
  787. };
  788. struct CommandTransform : public Command {
  789. Transform2D xform;
  790. CommandTransform() { type = TYPE_TRANSFORM; }
  791. };
  792. struct CommandClipIgnore : public Command {
  793. bool ignore;
  794. CommandClipIgnore() {
  795. type = TYPE_CLIP_IGNORE;
  796. ignore = false;
  797. }
  798. };
  799. struct ViewportRender {
  800. VisualServer *owner;
  801. void *udata;
  802. Rect2 rect;
  803. };
  804. // For interpolation we store the current local xform,
  805. // and the previous xform from the previous tick.
  806. Transform2D xform_curr;
  807. Transform2D xform_prev;
  808. bool clip : 1;
  809. bool visible : 1;
  810. bool behind : 1;
  811. bool update_when_visible : 1;
  812. bool distance_field : 1;
  813. bool light_masked : 1;
  814. bool on_interpolate_transform_list : 1;
  815. bool interpolated : 1;
  816. bool use_identity_xform : 1;
  817. mutable bool custom_rect : 1;
  818. mutable bool rect_dirty : 1;
  819. mutable bool bound_dirty : 1;
  820. Vector<Command *> commands;
  821. mutable Rect2 rect;
  822. RID material;
  823. RID skeleton;
  824. //VS::MaterialBlendMode blend_mode;
  825. int32_t light_mask;
  826. mutable uint32_t skeleton_revision;
  827. Item *next;
  828. struct SkinningData {
  829. Transform2D skeleton_relative_xform;
  830. Transform2D skeleton_relative_xform_inv;
  831. };
  832. SkinningData *skinning_data = nullptr;
  833. struct CopyBackBuffer {
  834. Rect2 rect;
  835. Rect2 screen_rect;
  836. bool full;
  837. };
  838. CopyBackBuffer *copy_back_buffer;
  839. Color final_modulate;
  840. Transform2D final_transform;
  841. Rect2 final_clip_rect;
  842. Item *final_clip_owner;
  843. Item *material_owner;
  844. ViewportRender *vp_render;
  845. Rect2 global_rect_cache;
  846. private:
  847. Rect2 calculate_polygon_bounds(const Item::CommandPolygon &p_polygon) const;
  848. void precalculate_polygon_bone_bounds(const Item::CommandPolygon &p_polygon) const;
  849. public:
  850. // the rect containing this item and all children,
  851. // in local space.
  852. Rect2 local_bound;
  853. // When using interpolation, the local bound for culling
  854. // should be a combined bound of the previous and current.
  855. // To keep this up to date, we need to keep track of the previous
  856. // bound separately rather than just the combined bound.
  857. Rect2 local_bound_prev;
  858. uint32_t local_bound_last_update_tick;
  859. const Rect2 &get_rect() const {
  860. if (custom_rect) {
  861. return rect;
  862. }
  863. if (!rect_dirty && !update_when_visible) {
  864. if (skeleton == RID()) {
  865. return rect;
  866. } else {
  867. // special case for skeletons
  868. uint32_t rev = RasterizerStorage::base_singleton->skeleton_get_revision(skeleton);
  869. if (rev == skeleton_revision) {
  870. // no change to the skeleton since we last calculated the bounding rect
  871. return rect;
  872. } else {
  873. // We need to recalculate.
  874. // Mark as done for next time.
  875. skeleton_revision = rev;
  876. }
  877. }
  878. }
  879. //must update rect
  880. int s = commands.size();
  881. if (s == 0) {
  882. rect = Rect2();
  883. rect_dirty = false;
  884. return rect;
  885. }
  886. Transform2D xf;
  887. bool found_xform = false;
  888. bool first = true;
  889. const Item::Command *const *cmd = &commands[0];
  890. for (int i = 0; i < s; i++) {
  891. const Item::Command *c = cmd[i];
  892. Rect2 r;
  893. switch (c->type) {
  894. case Item::Command::TYPE_LINE: {
  895. const Item::CommandLine *line = static_cast<const Item::CommandLine *>(c);
  896. r.position = line->from;
  897. r.expand_to(line->to);
  898. } break;
  899. case Item::Command::TYPE_POLYLINE: {
  900. const Item::CommandPolyLine *pline = static_cast<const Item::CommandPolyLine *>(c);
  901. if (pline->triangles.size()) {
  902. for (int j = 0; j < pline->triangles.size(); j++) {
  903. if (j == 0) {
  904. r.position = pline->triangles[j];
  905. } else {
  906. r.expand_to(pline->triangles[j]);
  907. }
  908. }
  909. } else {
  910. for (int j = 0; j < pline->lines.size(); j++) {
  911. if (j == 0) {
  912. r.position = pline->lines[j];
  913. } else {
  914. r.expand_to(pline->lines[j]);
  915. }
  916. }
  917. }
  918. } break;
  919. case Item::Command::TYPE_RECT: {
  920. const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
  921. r = crect->rect;
  922. } break;
  923. case Item::Command::TYPE_MULTIRECT: {
  924. const Item::CommandMultiRect *mrect = static_cast<const Item::CommandMultiRect *>(c);
  925. int num_rects = mrect->rects.size();
  926. if (num_rects) {
  927. r = mrect->rects[0];
  928. for (int n = 1; n < num_rects; n++) {
  929. r = mrect->rects[n].merge(r);
  930. }
  931. }
  932. } break;
  933. case Item::Command::TYPE_NINEPATCH: {
  934. const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
  935. r = style->rect;
  936. } break;
  937. case Item::Command::TYPE_PRIMITIVE: {
  938. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  939. r.position = primitive->points[0];
  940. for (int j = 1; j < primitive->points.size(); j++) {
  941. r.expand_to(primitive->points[j]);
  942. }
  943. } break;
  944. case Item::Command::TYPE_POLYGON: {
  945. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  946. DEV_ASSERT(polygon);
  947. r = calculate_polygon_bounds(*polygon);
  948. } break;
  949. case Item::Command::TYPE_MESH: {
  950. const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
  951. AABB aabb = RasterizerStorage::base_singleton->mesh_get_aabb(mesh->mesh, RID());
  952. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  953. } break;
  954. case Item::Command::TYPE_MULTIMESH: {
  955. const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
  956. AABB aabb = RasterizerStorage::base_singleton->multimesh_get_aabb(multimesh->multimesh);
  957. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  958. } break;
  959. case Item::Command::TYPE_PARTICLES: {
  960. const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
  961. if (particles_cmd->particles.is_valid()) {
  962. AABB aabb = RasterizerStorage::base_singleton->particles_get_aabb(particles_cmd->particles);
  963. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  964. }
  965. } break;
  966. case Item::Command::TYPE_CIRCLE: {
  967. const Item::CommandCircle *circle = static_cast<const Item::CommandCircle *>(c);
  968. r.position = Point2(-circle->radius, -circle->radius) + circle->pos;
  969. r.size = Point2(circle->radius * 2.0, circle->radius * 2.0);
  970. } break;
  971. case Item::Command::TYPE_TRANSFORM: {
  972. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  973. xf = transform->xform;
  974. found_xform = true;
  975. continue;
  976. } break;
  977. case Item::Command::TYPE_CLIP_IGNORE: {
  978. } break;
  979. }
  980. if (found_xform) {
  981. r = xf.xform(r);
  982. }
  983. if (first) {
  984. rect = r;
  985. first = false;
  986. } else {
  987. rect = rect.merge(r);
  988. }
  989. }
  990. rect_dirty = false;
  991. return rect;
  992. }
  993. void remove_references(const RID &p_rid) {
  994. for (int i = commands.size() - 1; i >= 0; i--) {
  995. if (commands[i]->contains_reference(p_rid)) {
  996. memdelete(commands[i]);
  997. // This could possibly be unordered if occurring close
  998. // to canvas_item deletion, but is
  999. // unlikely to make much performance difference,
  1000. // and is safer.
  1001. commands.remove(i);
  1002. }
  1003. }
  1004. }
  1005. void clear() {
  1006. for (int i = 0; i < commands.size(); i++) {
  1007. memdelete(commands[i]);
  1008. }
  1009. commands.clear();
  1010. clip = false;
  1011. rect_dirty = true;
  1012. final_clip_owner = nullptr;
  1013. material_owner = nullptr;
  1014. light_masked = false;
  1015. if (skinning_data) {
  1016. memdelete(skinning_data);
  1017. skinning_data = nullptr;
  1018. }
  1019. on_interpolate_transform_list = false;
  1020. }
  1021. Item() {
  1022. light_mask = 1;
  1023. skeleton_revision = 0;
  1024. vp_render = nullptr;
  1025. next = nullptr;
  1026. final_clip_owner = nullptr;
  1027. clip = false;
  1028. final_modulate = Color(1, 1, 1, 1);
  1029. visible = true;
  1030. rect_dirty = true;
  1031. bound_dirty = true;
  1032. custom_rect = false;
  1033. behind = false;
  1034. material_owner = nullptr;
  1035. copy_back_buffer = nullptr;
  1036. distance_field = false;
  1037. light_masked = false;
  1038. update_when_visible = false;
  1039. on_interpolate_transform_list = false;
  1040. interpolated = true;
  1041. use_identity_xform = false;
  1042. local_bound_last_update_tick = 0;
  1043. }
  1044. virtual ~Item() {
  1045. clear();
  1046. if (copy_back_buffer) {
  1047. memdelete(copy_back_buffer);
  1048. }
  1049. }
  1050. };
  1051. virtual void canvas_begin() = 0;
  1052. virtual void canvas_end() = 0;
  1053. virtual void canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {}
  1054. virtual void canvas_render_items_end() {}
  1055. virtual void canvas_render_items(Item *p_item_list, int p_z, const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) = 0;
  1056. virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0;
  1057. struct LightOccluderInstance : public RID_Data {
  1058. bool enabled : 1;
  1059. bool on_interpolate_transform_list : 1;
  1060. bool interpolated : 1;
  1061. RID canvas;
  1062. RID polygon;
  1063. RID polygon_buffer;
  1064. Rect2 aabb_cache;
  1065. Transform2D xform_curr;
  1066. Transform2D xform_prev;
  1067. Transform2D xform_cache;
  1068. int light_mask;
  1069. VS::CanvasOccluderPolygonCullMode cull_cache;
  1070. LightOccluderInstance *next;
  1071. LightOccluderInstance() {
  1072. enabled = true;
  1073. next = nullptr;
  1074. light_mask = 1;
  1075. cull_cache = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1076. on_interpolate_transform_list = false;
  1077. interpolated = true;
  1078. }
  1079. };
  1080. virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) = 0;
  1081. virtual void reset_canvas() = 0;
  1082. virtual void draw_window_margins(int *p_margins, RID *p_margin_textures) = 0;
  1083. virtual ~RasterizerCanvas() {}
  1084. };
  1085. class Rasterizer {
  1086. protected:
  1087. static Rasterizer *(*_create_func)();
  1088. public:
  1089. static Rasterizer *create();
  1090. virtual RasterizerStorage *get_storage() = 0;
  1091. virtual RasterizerCanvas *get_canvas() = 0;
  1092. virtual RasterizerScene *get_scene() = 0;
  1093. virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0;
  1094. virtual void set_shader_time_scale(float p_scale) = 0;
  1095. virtual void initialize() = 0;
  1096. virtual void begin_frame(double frame_step) = 0;
  1097. virtual void set_current_render_target(RID p_render_target) = 0;
  1098. virtual void restore_render_target(bool p_3d) = 0;
  1099. virtual void clear_render_target(const Color &p_color) = 0;
  1100. virtual void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0) = 0;
  1101. virtual void output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) = 0;
  1102. virtual void end_frame(bool p_swap_buffers) = 0;
  1103. virtual void finalize() = 0;
  1104. virtual bool is_low_end() const = 0;
  1105. virtual ~Rasterizer() {}
  1106. };
  1107. // Use float rather than real_t as cheaper and no need for 64 bit.
  1108. _FORCE_INLINE_ void RasterizerStorage::_interpolate_RGBA8(const uint8_t *p_a, const uint8_t *p_b, uint8_t *r_dest, float p_f) const {
  1109. // Todo, jiggle these values and test for correctness.
  1110. // Integer interpolation is finicky.. :)
  1111. p_f *= 256.0f;
  1112. int32_t mult = CLAMP(int32_t(p_f), 0, 255);
  1113. for (int n = 0; n < 4; n++) {
  1114. int32_t a = p_a[n];
  1115. int32_t b = p_b[n];
  1116. int32_t diff = b - a;
  1117. diff *= mult;
  1118. diff /= 255;
  1119. int32_t res = a + diff;
  1120. // may not be needed
  1121. res = CLAMP(res, 0, 255);
  1122. r_dest[n] = res;
  1123. }
  1124. }
  1125. #endif // RASTERIZER_H