rasterizer.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. /*************************************************************************/
  2. /* rasterizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_H
  31. #define RASTERIZER_H
  32. #include "core/math/camera_matrix.h"
  33. #include "servers/visual_server.h"
  34. #include "core/self_list.h"
  35. class RasterizerScene {
  36. public:
  37. /* SHADOW ATLAS API */
  38. virtual RID shadow_atlas_create() = 0;
  39. virtual void shadow_atlas_set_size(RID p_atlas, int p_size) = 0;
  40. virtual void shadow_atlas_set_quadrant_subdivision(RID p_atlas, int p_quadrant, int p_subdivision) = 0;
  41. virtual bool shadow_atlas_update_light(RID p_atlas, RID p_light_intance, float p_coverage, uint64_t p_light_version) = 0;
  42. virtual int get_directional_light_shadow_size(RID p_light_intance) = 0;
  43. virtual void set_directional_shadow_count(int p_count) = 0;
  44. /* ENVIRONMENT API */
  45. virtual RID environment_create() = 0;
  46. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg) = 0;
  47. virtual void environment_set_sky(RID p_env, RID p_sky) = 0;
  48. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale) = 0;
  49. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation) = 0;
  50. virtual void environment_set_bg_color(RID p_env, const Color &p_color) = 0;
  51. virtual void environment_set_bg_energy(RID p_env, float p_energy) = 0;
  52. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer) = 0;
  53. 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;
  54. virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) = 0;
  55. 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;
  56. 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;
  57. 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;
  58. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) = 0;
  59. 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;
  60. 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;
  61. 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;
  62. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) = 0;
  63. 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;
  64. 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;
  65. 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;
  66. virtual bool is_environment(RID p_env) = 0;
  67. virtual VS::EnvironmentBG environment_get_background(RID p_env) = 0;
  68. virtual int environment_get_canvas_max_layer(RID p_env) = 0;
  69. struct InstanceBase : RID_Data {
  70. VS::InstanceType base_type;
  71. RID base;
  72. RID skeleton;
  73. RID material_override;
  74. Transform transform;
  75. int depth_layer;
  76. uint32_t layer_mask;
  77. //RID sampled_light;
  78. Vector<RID> materials;
  79. Vector<RID> light_instances;
  80. Vector<RID> reflection_probe_instances;
  81. Vector<RID> gi_probe_instances;
  82. PoolVector<float> blend_values;
  83. VS::ShadowCastingSetting cast_shadows;
  84. //fit in 32 bits
  85. bool mirror : 8;
  86. bool receive_shadows : 8;
  87. bool visible : 8;
  88. bool baked_light : 4; //this flag is only to know if it actually did use baked light
  89. bool redraw_if_visible : 4;
  90. float depth; //used for sorting
  91. SelfList<InstanceBase> dependency_item;
  92. InstanceBase *lightmap_capture;
  93. RID lightmap;
  94. 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
  95. int lightmap_slice;
  96. Rect2 lightmap_uv_rect;
  97. virtual void base_removed() = 0;
  98. virtual void base_changed(bool p_aabb, bool p_materials) = 0;
  99. InstanceBase() :
  100. dependency_item(this) {
  101. base_type = VS::INSTANCE_NONE;
  102. cast_shadows = VS::SHADOW_CASTING_SETTING_ON;
  103. receive_shadows = true;
  104. visible = true;
  105. depth_layer = 0;
  106. layer_mask = 1;
  107. baked_light = false;
  108. redraw_if_visible = false;
  109. lightmap_capture = nullptr;
  110. lightmap_slice = -1;
  111. lightmap_uv_rect = Rect2(0, 0, 1, 1);
  112. }
  113. };
  114. virtual RID light_instance_create(RID p_light) = 0;
  115. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform) = 0;
  116. 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;
  117. virtual void light_instance_mark_visible(RID p_light_instance) = 0;
  118. virtual bool light_instances_can_render_shadow_cube() const { return true; }
  119. virtual RID reflection_atlas_create() = 0;
  120. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_size) = 0;
  121. virtual void reflection_atlas_set_subdivision(RID p_ref_atlas, int p_subdiv) = 0;
  122. virtual RID reflection_probe_instance_create(RID p_probe) = 0;
  123. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) = 0;
  124. virtual void reflection_probe_release_atlas_index(RID p_instance) = 0;
  125. virtual bool reflection_probe_instance_needs_redraw(RID p_instance) = 0;
  126. virtual bool reflection_probe_instance_has_reflection(RID p_instance) = 0;
  127. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) = 0;
  128. virtual bool reflection_probe_instance_postprocess_step(RID p_instance) = 0;
  129. virtual RID gi_probe_instance_create() = 0;
  130. virtual void gi_probe_instance_set_light_data(RID p_probe, RID p_base, RID p_data) = 0;
  131. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) = 0;
  132. virtual void gi_probe_instance_set_bounds(RID p_probe, const Vector3 &p_bounds) = 0;
  133. 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;
  134. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) = 0;
  135. virtual void set_scene_pass(uint64_t p_pass) = 0;
  136. virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) = 0;
  137. virtual bool free(RID p_rid) = 0;
  138. virtual ~RasterizerScene() {}
  139. };
  140. class RasterizerStorage {
  141. public:
  142. /* TEXTURE API */
  143. virtual RID texture_create() = 0;
  144. virtual void texture_allocate(RID p_texture,
  145. int p_width,
  146. int p_height,
  147. int p_depth_3d,
  148. Image::Format p_format,
  149. VS::TextureType p_type,
  150. uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT) = 0;
  151. virtual void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_level = 0) = 0;
  152. virtual void texture_set_data_partial(RID p_texture,
  153. const Ref<Image> &p_image,
  154. int src_x, int src_y,
  155. int src_w, int src_h,
  156. int dst_x, int dst_y,
  157. int p_dst_mip,
  158. int p_level = 0) = 0;
  159. virtual Ref<Image> texture_get_data(RID p_texture, int p_level = 0) const = 0;
  160. virtual void texture_set_flags(RID p_texture, uint32_t p_flags) = 0;
  161. virtual uint32_t texture_get_flags(RID p_texture) const = 0;
  162. virtual Image::Format texture_get_format(RID p_texture) const = 0;
  163. virtual VS::TextureType texture_get_type(RID p_texture) const = 0;
  164. virtual uint32_t texture_get_texid(RID p_texture) const = 0;
  165. virtual uint32_t texture_get_width(RID p_texture) const = 0;
  166. virtual uint32_t texture_get_height(RID p_texture) const = 0;
  167. virtual uint32_t texture_get_depth(RID p_texture) const = 0;
  168. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height, int p_depth_3d) = 0;
  169. virtual void texture_bind(RID p_texture, uint32_t p_texture_no) = 0;
  170. virtual void texture_set_path(RID p_texture, const String &p_path) = 0;
  171. virtual String texture_get_path(RID p_texture) const = 0;
  172. virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable) = 0;
  173. virtual void texture_debug_usage(List<VS::TextureInfo> *r_info) = 0;
  174. virtual RID texture_create_radiance_cubemap(RID p_source, int p_resolution = -1) const = 0;
  175. virtual void texture_set_detect_3d_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  176. virtual void texture_set_detect_srgb_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  177. virtual void texture_set_detect_normal_callback(RID p_texture, VisualServer::TextureDetectCallback p_callback, void *p_userdata) = 0;
  178. virtual void textures_keep_original(bool p_enable) = 0;
  179. virtual void texture_set_proxy(RID p_proxy, RID p_base) = 0;
  180. virtual Size2 texture_size_with_proxy(RID p_texture) const = 0;
  181. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) = 0;
  182. /* SKY API */
  183. virtual RID sky_create() = 0;
  184. virtual void sky_set_texture(RID p_sky, RID p_cube_map, int p_radiance_size) = 0;
  185. /* SHADER API */
  186. virtual RID shader_create() = 0;
  187. virtual void shader_set_code(RID p_shader, const String &p_code) = 0;
  188. virtual String shader_get_code(RID p_shader) const = 0;
  189. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const = 0;
  190. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) = 0;
  191. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const = 0;
  192. virtual void shader_add_custom_define(RID p_shader, const String &p_define) = 0;
  193. virtual void shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const = 0;
  194. virtual void shader_remove_custom_define(RID p_shader, const String &p_define) = 0;
  195. /* COMMON MATERIAL API */
  196. virtual RID material_create() = 0;
  197. virtual void material_set_render_priority(RID p_material, int priority) = 0;
  198. virtual void material_set_shader(RID p_shader_material, RID p_shader) = 0;
  199. virtual RID material_get_shader(RID p_shader_material) const = 0;
  200. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0;
  201. virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0;
  202. virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const = 0;
  203. virtual void material_set_line_width(RID p_material, float p_width) = 0;
  204. virtual void material_set_next_pass(RID p_material, RID p_next_material) = 0;
  205. virtual bool material_is_animated(RID p_material) = 0;
  206. virtual bool material_casts_shadows(RID p_material) = 0;
  207. virtual bool material_uses_tangents(RID p_material);
  208. virtual bool material_uses_ensure_correct_normals(RID p_material);
  209. virtual void material_add_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) = 0;
  210. virtual void material_remove_instance_owner(RID p_material, RasterizerScene::InstanceBase *p_instance) = 0;
  211. /* MESH API */
  212. virtual RID mesh_create() = 0;
  213. 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;
  214. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_amount) = 0;
  215. virtual int mesh_get_blend_shape_count(RID p_mesh) const = 0;
  216. virtual void mesh_set_blend_shape_mode(RID p_mesh, VS::BlendShapeMode p_mode) = 0;
  217. virtual VS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const = 0;
  218. virtual void mesh_set_blend_shape_values(RID p_mesh, PoolVector<float> p_values) = 0;
  219. virtual PoolVector<float> mesh_get_blend_shape_values(RID p_mesh) const = 0;
  220. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) = 0;
  221. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
  222. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;
  223. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const = 0;
  224. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const = 0;
  225. virtual PoolVector<uint8_t> mesh_surface_get_array(RID p_mesh, int p_surface) const = 0;
  226. virtual PoolVector<uint8_t> mesh_surface_get_index_array(RID p_mesh, int p_surface) const = 0;
  227. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const = 0;
  228. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const = 0;
  229. virtual AABB mesh_surface_get_aabb(RID p_mesh, int p_surface) const = 0;
  230. virtual Vector<PoolVector<uint8_t>> mesh_surface_get_blend_shapes(RID p_mesh, int p_surface) const = 0;
  231. virtual Vector<AABB> mesh_surface_get_skeleton_aabb(RID p_mesh, int p_surface) const = 0;
  232. virtual void mesh_remove_surface(RID p_mesh, int p_index) = 0;
  233. virtual int mesh_get_surface_count(RID p_mesh) const = 0;
  234. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
  235. virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;
  236. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton) const = 0;
  237. virtual void mesh_clear(RID p_mesh) = 0;
  238. /* MULTIMESH API */
  239. virtual RID multimesh_create() = 0;
  240. 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;
  241. virtual int multimesh_get_instance_count(RID p_multimesh) const = 0;
  242. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh) = 0;
  243. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) = 0;
  244. virtual void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) = 0;
  245. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) = 0;
  246. virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) = 0;
  247. virtual RID multimesh_get_mesh(RID p_multimesh) const = 0;
  248. virtual Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const = 0;
  249. virtual Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const = 0;
  250. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const = 0;
  251. virtual Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const = 0;
  252. virtual void multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) = 0;
  253. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible) = 0;
  254. virtual int multimesh_get_visible_instances(RID p_multimesh) const = 0;
  255. virtual AABB multimesh_get_aabb(RID p_multimesh) const = 0;
  256. /* IMMEDIATE API */
  257. virtual RID immediate_create() = 0;
  258. virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID()) = 0;
  259. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) = 0;
  260. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal) = 0;
  261. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent) = 0;
  262. virtual void immediate_color(RID p_immediate, const Color &p_color) = 0;
  263. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv) = 0;
  264. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) = 0;
  265. virtual void immediate_end(RID p_immediate) = 0;
  266. virtual void immediate_clear(RID p_immediate) = 0;
  267. virtual void immediate_set_material(RID p_immediate, RID p_material) = 0;
  268. virtual RID immediate_get_material(RID p_immediate) const = 0;
  269. virtual AABB immediate_get_aabb(RID p_immediate) const = 0;
  270. /* SKELETON API */
  271. virtual RID skeleton_create() = 0;
  272. virtual void skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) = 0;
  273. virtual int skeleton_get_bone_count(RID p_skeleton) const = 0;
  274. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) = 0;
  275. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const = 0;
  276. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) = 0;
  277. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const = 0;
  278. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) = 0;
  279. /* Light API */
  280. virtual RID light_create(VS::LightType p_type) = 0;
  281. RID directional_light_create() { return light_create(VS::LIGHT_DIRECTIONAL); }
  282. RID omni_light_create() { return light_create(VS::LIGHT_OMNI); }
  283. RID spot_light_create() { return light_create(VS::LIGHT_SPOT); }
  284. virtual void light_set_color(RID p_light, const Color &p_color) = 0;
  285. virtual void light_set_param(RID p_light, VS::LightParam p_param, float p_value) = 0;
  286. virtual void light_set_shadow(RID p_light, bool p_enabled) = 0;
  287. virtual void light_set_shadow_color(RID p_light, const Color &p_color) = 0;
  288. virtual void light_set_projector(RID p_light, RID p_texture) = 0;
  289. virtual void light_set_negative(RID p_light, bool p_enable) = 0;
  290. virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) = 0;
  291. virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) = 0;
  292. virtual void light_set_use_gi(RID p_light, bool p_enable) = 0;
  293. virtual void light_set_bake_mode(RID p_light, VS::LightBakeMode p_bake_mode) = 0;
  294. virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) = 0;
  295. virtual void light_omni_set_shadow_detail(RID p_light, VS::LightOmniShadowDetail p_detail) = 0;
  296. virtual void light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) = 0;
  297. virtual void light_directional_set_blend_splits(RID p_light, bool p_enable) = 0;
  298. virtual bool light_directional_get_blend_splits(RID p_light) const = 0;
  299. virtual void light_directional_set_shadow_depth_range_mode(RID p_light, VS::LightDirectionalShadowDepthRangeMode p_range_mode) = 0;
  300. virtual VS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const = 0;
  301. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) = 0;
  302. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) = 0;
  303. virtual bool light_has_shadow(RID p_light) const = 0;
  304. virtual VS::LightType light_get_type(RID p_light) const = 0;
  305. virtual AABB light_get_aabb(RID p_light) const = 0;
  306. virtual float light_get_param(RID p_light, VS::LightParam p_param) = 0;
  307. virtual Color light_get_color(RID p_light) = 0;
  308. virtual bool light_get_use_gi(RID p_light) = 0;
  309. virtual VS::LightBakeMode light_get_bake_mode(RID p_light) = 0;
  310. virtual uint64_t light_get_version(RID p_light) const = 0;
  311. /* PROBE API */
  312. virtual RID reflection_probe_create() = 0;
  313. virtual void reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) = 0;
  314. virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution) = 0;
  315. virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity) = 0;
  316. virtual void reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) = 0;
  317. virtual void reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) = 0;
  318. virtual void reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) = 0;
  319. virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance) = 0;
  320. virtual void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) = 0;
  321. virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) = 0;
  322. virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable) = 0;
  323. virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) = 0;
  324. virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) = 0;
  325. virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) = 0;
  326. virtual AABB reflection_probe_get_aabb(RID p_probe) const = 0;
  327. virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const = 0;
  328. virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const = 0;
  329. virtual Vector3 reflection_probe_get_extents(RID p_probe) const = 0;
  330. virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const = 0;
  331. virtual float reflection_probe_get_origin_max_distance(RID p_probe) const = 0;
  332. virtual bool reflection_probe_renders_shadows(RID p_probe) const = 0;
  333. virtual void instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) = 0;
  334. virtual void instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) = 0;
  335. virtual void instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  336. virtual void instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  337. /* GI PROBE API */
  338. virtual RID gi_probe_create() = 0;
  339. virtual void gi_probe_set_bounds(RID p_probe, const AABB &p_bounds) = 0;
  340. virtual AABB gi_probe_get_bounds(RID p_probe) const = 0;
  341. virtual void gi_probe_set_cell_size(RID p_probe, float p_range) = 0;
  342. virtual float gi_probe_get_cell_size(RID p_probe) const = 0;
  343. virtual void gi_probe_set_to_cell_xform(RID p_probe, const Transform &p_xform) = 0;
  344. virtual Transform gi_probe_get_to_cell_xform(RID p_probe) const = 0;
  345. virtual void gi_probe_set_dynamic_data(RID p_probe, const PoolVector<int> &p_data) = 0;
  346. virtual PoolVector<int> gi_probe_get_dynamic_data(RID p_probe) const = 0;
  347. virtual void gi_probe_set_dynamic_range(RID p_probe, int p_range) = 0;
  348. virtual int gi_probe_get_dynamic_range(RID p_probe) const = 0;
  349. virtual void gi_probe_set_energy(RID p_probe, float p_range) = 0;
  350. virtual float gi_probe_get_energy(RID p_probe) const = 0;
  351. virtual void gi_probe_set_bias(RID p_probe, float p_range) = 0;
  352. virtual float gi_probe_get_bias(RID p_probe) const = 0;
  353. virtual void gi_probe_set_normal_bias(RID p_probe, float p_range) = 0;
  354. virtual float gi_probe_get_normal_bias(RID p_probe) const = 0;
  355. virtual void gi_probe_set_propagation(RID p_probe, float p_range) = 0;
  356. virtual float gi_probe_get_propagation(RID p_probe) const = 0;
  357. virtual void gi_probe_set_interior(RID p_probe, bool p_enable) = 0;
  358. virtual bool gi_probe_is_interior(RID p_probe) const = 0;
  359. virtual void gi_probe_set_compress(RID p_probe, bool p_enable) = 0;
  360. virtual bool gi_probe_is_compressed(RID p_probe) const = 0;
  361. virtual uint32_t gi_probe_get_version(RID p_probe) = 0;
  362. enum GIProbeCompression {
  363. GI_PROBE_UNCOMPRESSED,
  364. GI_PROBE_S3TC,
  365. GI_PROBE_ETC2
  366. };
  367. virtual RID gi_probe_dynamic_data_create(int p_width, int p_height, int p_depth, GIProbeCompression p_compression) = 0;
  368. 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;
  369. /* LIGHTMAP CAPTURE */
  370. struct LightmapCaptureOctree {
  371. enum {
  372. CHILD_EMPTY = 0xFFFFFFFF
  373. };
  374. uint16_t light[6][3]; //anisotropic light
  375. float alpha;
  376. uint32_t children[8];
  377. };
  378. virtual RID lightmap_capture_create() = 0;
  379. virtual void lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) = 0;
  380. virtual AABB lightmap_capture_get_bounds(RID p_capture) const = 0;
  381. virtual void lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) = 0;
  382. virtual PoolVector<uint8_t> lightmap_capture_get_octree(RID p_capture) const = 0;
  383. virtual void lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) = 0;
  384. virtual Transform lightmap_capture_get_octree_cell_transform(RID p_capture) const = 0;
  385. virtual void lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) = 0;
  386. virtual int lightmap_capture_get_octree_cell_subdiv(RID p_capture) const = 0;
  387. virtual void lightmap_capture_set_energy(RID p_capture, float p_energy) = 0;
  388. virtual float lightmap_capture_get_energy(RID p_capture) const = 0;
  389. virtual void lightmap_capture_set_interior(RID p_capture, bool p_interior) = 0;
  390. virtual bool lightmap_capture_is_interior(RID p_capture) const = 0;
  391. virtual const PoolVector<LightmapCaptureOctree> *lightmap_capture_get_octree_ptr(RID p_capture) const = 0;
  392. /* PARTICLES */
  393. virtual RID particles_create() = 0;
  394. virtual void particles_set_emitting(RID p_particles, bool p_emitting) = 0;
  395. virtual bool particles_get_emitting(RID p_particles) = 0;
  396. virtual void particles_set_amount(RID p_particles, int p_amount) = 0;
  397. virtual void particles_set_lifetime(RID p_particles, float p_lifetime) = 0;
  398. virtual void particles_set_one_shot(RID p_particles, bool p_one_shot) = 0;
  399. virtual void particles_set_pre_process_time(RID p_particles, float p_time) = 0;
  400. virtual void particles_set_explosiveness_ratio(RID p_particles, float p_ratio) = 0;
  401. virtual void particles_set_randomness_ratio(RID p_particles, float p_ratio) = 0;
  402. virtual void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) = 0;
  403. virtual void particles_set_speed_scale(RID p_particles, float p_scale) = 0;
  404. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable) = 0;
  405. virtual void particles_set_process_material(RID p_particles, RID p_material) = 0;
  406. virtual void particles_set_fixed_fps(RID p_particles, int p_fps) = 0;
  407. virtual void particles_set_fractional_delta(RID p_particles, bool p_enable) = 0;
  408. virtual void particles_restart(RID p_particles) = 0;
  409. virtual bool particles_is_inactive(RID p_particles) const = 0;
  410. virtual void particles_set_draw_order(RID p_particles, VS::ParticlesDrawOrder p_order) = 0;
  411. virtual void particles_set_draw_passes(RID p_particles, int p_count) = 0;
  412. virtual void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) = 0;
  413. virtual void particles_request_process(RID p_particles) = 0;
  414. virtual AABB particles_get_current_aabb(RID p_particles) = 0;
  415. virtual AABB particles_get_aabb(RID p_particles) const = 0;
  416. virtual void particles_set_emission_transform(RID p_particles, const Transform &p_transform) = 0;
  417. virtual int particles_get_draw_passes(RID p_particles) const = 0;
  418. virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const = 0;
  419. /* RENDER TARGET */
  420. enum RenderTargetFlags {
  421. RENDER_TARGET_VFLIP,
  422. RENDER_TARGET_TRANSPARENT,
  423. RENDER_TARGET_NO_3D_EFFECTS,
  424. RENDER_TARGET_NO_3D,
  425. RENDER_TARGET_NO_SAMPLING,
  426. RENDER_TARGET_HDR,
  427. RENDER_TARGET_KEEP_3D_LINEAR,
  428. RENDER_TARGET_DIRECT_TO_SCREEN,
  429. RENDER_TARGET_FLAG_MAX
  430. };
  431. virtual RID render_target_create() = 0;
  432. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) = 0;
  433. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height) = 0;
  434. virtual RID render_target_get_texture(RID p_render_target) const = 0;
  435. virtual uint32_t render_target_get_depth_texture_id(RID p_render_target) const = 0;
  436. virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id, unsigned int p_depth_id) = 0;
  437. virtual void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) = 0;
  438. virtual bool render_target_was_used(RID p_render_target) = 0;
  439. virtual void render_target_clear_used(RID p_render_target) = 0;
  440. virtual void render_target_set_msaa(RID p_render_target, VS::ViewportMSAA p_msaa) = 0;
  441. virtual void render_target_set_use_fxaa(RID p_render_target, bool p_fxaa) = 0;
  442. virtual void render_target_set_use_debanding(RID p_render_target, bool p_debanding) = 0;
  443. virtual void render_target_set_sharpen_intensity(RID p_render_target, float p_intensity) = 0;
  444. /* CANVAS SHADOW */
  445. virtual RID canvas_light_shadow_buffer_create(int p_width) = 0;
  446. /* LIGHT SHADOW MAPPING */
  447. virtual RID canvas_light_occluder_create() = 0;
  448. virtual void canvas_light_occluder_set_polylines(RID p_occluder, const PoolVector<Vector2> &p_lines) = 0;
  449. virtual VS::InstanceType get_base_type(RID p_rid) const = 0;
  450. virtual bool free(RID p_rid) = 0;
  451. virtual bool has_os_feature(const String &p_feature) const = 0;
  452. virtual void update_dirty_resources() = 0;
  453. virtual void set_debug_generate_wireframes(bool p_generate) = 0;
  454. virtual void render_info_begin_capture() = 0;
  455. virtual void render_info_end_capture() = 0;
  456. virtual int get_captured_render_info(VS::RenderInfo p_info) = 0;
  457. virtual uint64_t get_render_info(VS::RenderInfo p_info) = 0;
  458. virtual String get_video_adapter_name() const = 0;
  459. virtual String get_video_adapter_vendor() const = 0;
  460. static RasterizerStorage *base_singleton;
  461. RasterizerStorage();
  462. virtual ~RasterizerStorage() {}
  463. };
  464. class RasterizerCanvas {
  465. public:
  466. enum CanvasRectFlags {
  467. CANVAS_RECT_REGION = 1,
  468. CANVAS_RECT_TILE = 2,
  469. CANVAS_RECT_FLIP_H = 4,
  470. CANVAS_RECT_FLIP_V = 8,
  471. CANVAS_RECT_TRANSPOSE = 16,
  472. CANVAS_RECT_CLIP_UV = 32
  473. };
  474. struct Light : public RID_Data {
  475. bool enabled;
  476. Color color;
  477. Transform2D xform;
  478. float height;
  479. float energy;
  480. float scale;
  481. int z_min;
  482. int z_max;
  483. int layer_min;
  484. int layer_max;
  485. int item_mask;
  486. int item_shadow_mask;
  487. VS::CanvasLightMode mode;
  488. RID texture;
  489. Vector2 texture_offset;
  490. RID canvas;
  491. RID shadow_buffer;
  492. int shadow_buffer_size;
  493. float shadow_gradient_length;
  494. VS::CanvasLightShadowFilter shadow_filter;
  495. Color shadow_color;
  496. float shadow_smooth;
  497. void *texture_cache; // implementation dependent
  498. Rect2 rect_cache;
  499. Transform2D xform_cache;
  500. float radius_cache; //used for shadow far plane
  501. CameraMatrix shadow_matrix_cache;
  502. Transform2D light_shader_xform;
  503. Vector2 light_shader_pos;
  504. Light *shadows_next_ptr;
  505. Light *filter_next_ptr;
  506. Light *next_ptr;
  507. Light *mask_next_ptr;
  508. RID light_internal;
  509. Light() {
  510. enabled = true;
  511. color = Color(1, 1, 1);
  512. shadow_color = Color(0, 0, 0, 0);
  513. height = 0;
  514. z_min = -1024;
  515. z_max = 1024;
  516. layer_min = 0;
  517. layer_max = 0;
  518. item_mask = 1;
  519. scale = 1.0;
  520. energy = 1.0;
  521. item_shadow_mask = 1;
  522. mode = VS::CANVAS_LIGHT_MODE_ADD;
  523. texture_cache = nullptr;
  524. next_ptr = nullptr;
  525. mask_next_ptr = nullptr;
  526. filter_next_ptr = nullptr;
  527. shadow_buffer_size = 2048;
  528. shadow_gradient_length = 0;
  529. shadow_filter = VS::CANVAS_LIGHT_FILTER_NONE;
  530. shadow_smooth = 0.0;
  531. }
  532. };
  533. virtual RID light_internal_create() = 0;
  534. virtual void light_internal_update(RID p_rid, Light *p_light) = 0;
  535. virtual void light_internal_free(RID p_rid) = 0;
  536. struct Item : public RID_Data {
  537. struct Command {
  538. enum Type {
  539. TYPE_LINE,
  540. TYPE_POLYLINE,
  541. TYPE_RECT,
  542. TYPE_NINEPATCH,
  543. TYPE_PRIMITIVE,
  544. TYPE_POLYGON,
  545. TYPE_MESH,
  546. TYPE_MULTIMESH,
  547. TYPE_PARTICLES,
  548. TYPE_CIRCLE,
  549. TYPE_TRANSFORM,
  550. TYPE_CLIP_IGNORE,
  551. };
  552. Type type;
  553. virtual ~Command() {}
  554. };
  555. struct CommandLine : public Command {
  556. Point2 from, to;
  557. Color color;
  558. float width;
  559. bool antialiased;
  560. CommandLine() { type = TYPE_LINE; }
  561. };
  562. struct CommandPolyLine : public Command {
  563. bool antialiased;
  564. bool multiline;
  565. Vector<Point2> triangles;
  566. Vector<Color> triangle_colors;
  567. Vector<Point2> lines;
  568. Vector<Color> line_colors;
  569. CommandPolyLine() {
  570. type = TYPE_POLYLINE;
  571. antialiased = false;
  572. multiline = false;
  573. }
  574. };
  575. struct CommandRect : public Command {
  576. Rect2 rect;
  577. RID texture;
  578. RID normal_map;
  579. Color modulate;
  580. Rect2 source;
  581. uint8_t flags;
  582. CommandRect() {
  583. flags = 0;
  584. type = TYPE_RECT;
  585. }
  586. };
  587. struct CommandNinePatch : public Command {
  588. Rect2 rect;
  589. Rect2 source;
  590. RID texture;
  591. RID normal_map;
  592. float margin[4];
  593. bool draw_center;
  594. Color color;
  595. VS::NinePatchAxisMode axis_x;
  596. VS::NinePatchAxisMode axis_y;
  597. CommandNinePatch() {
  598. draw_center = true;
  599. type = TYPE_NINEPATCH;
  600. }
  601. };
  602. struct CommandPrimitive : public Command {
  603. Vector<Point2> points;
  604. Vector<Point2> uvs;
  605. Vector<Color> colors;
  606. RID texture;
  607. RID normal_map;
  608. float width;
  609. CommandPrimitive() {
  610. type = TYPE_PRIMITIVE;
  611. width = 1;
  612. }
  613. };
  614. struct CommandPolygon : public Command {
  615. Vector<int> indices;
  616. Vector<Point2> points;
  617. Vector<Point2> uvs;
  618. Vector<Color> colors;
  619. Vector<int> bones;
  620. Vector<float> weights;
  621. RID texture;
  622. RID normal_map;
  623. int count;
  624. bool antialiased;
  625. bool antialiasing_use_indices;
  626. CommandPolygon() {
  627. type = TYPE_POLYGON;
  628. count = 0;
  629. }
  630. };
  631. struct CommandMesh : public Command {
  632. RID mesh;
  633. RID texture;
  634. RID normal_map;
  635. Transform2D transform;
  636. Color modulate;
  637. CommandMesh() { type = TYPE_MESH; }
  638. };
  639. struct CommandMultiMesh : public Command {
  640. RID multimesh;
  641. RID texture;
  642. RID normal_map;
  643. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  644. };
  645. struct CommandParticles : public Command {
  646. RID particles;
  647. RID texture;
  648. RID normal_map;
  649. CommandParticles() { type = TYPE_PARTICLES; }
  650. };
  651. struct CommandCircle : public Command {
  652. Point2 pos;
  653. float radius;
  654. Color color;
  655. CommandCircle() { type = TYPE_CIRCLE; }
  656. };
  657. struct CommandTransform : public Command {
  658. Transform2D xform;
  659. CommandTransform() { type = TYPE_TRANSFORM; }
  660. };
  661. struct CommandClipIgnore : public Command {
  662. bool ignore;
  663. CommandClipIgnore() {
  664. type = TYPE_CLIP_IGNORE;
  665. ignore = false;
  666. }
  667. };
  668. struct ViewportRender {
  669. VisualServer *owner;
  670. void *udata;
  671. Rect2 rect;
  672. };
  673. Transform2D xform;
  674. bool clip;
  675. bool visible;
  676. bool behind;
  677. bool update_when_visible;
  678. //VS::MaterialBlendMode blend_mode;
  679. int light_mask;
  680. Vector<Command *> commands;
  681. mutable bool custom_rect;
  682. mutable bool rect_dirty;
  683. mutable Rect2 rect;
  684. RID material;
  685. RID skeleton;
  686. Item *next;
  687. struct CopyBackBuffer {
  688. Rect2 rect;
  689. Rect2 screen_rect;
  690. bool full;
  691. };
  692. CopyBackBuffer *copy_back_buffer;
  693. Color final_modulate;
  694. Transform2D final_transform;
  695. Rect2 final_clip_rect;
  696. Item *final_clip_owner;
  697. Item *material_owner;
  698. ViewportRender *vp_render;
  699. bool distance_field;
  700. bool light_masked;
  701. Rect2 global_rect_cache;
  702. const Rect2 &get_rect() const {
  703. if (custom_rect || (!rect_dirty && !update_when_visible)) {
  704. return rect;
  705. }
  706. //must update rect
  707. int s = commands.size();
  708. if (s == 0) {
  709. rect = Rect2();
  710. rect_dirty = false;
  711. return rect;
  712. }
  713. Transform2D xf;
  714. bool found_xform = false;
  715. bool first = true;
  716. const Item::Command *const *cmd = &commands[0];
  717. for (int i = 0; i < s; i++) {
  718. const Item::Command *c = cmd[i];
  719. Rect2 r;
  720. switch (c->type) {
  721. case Item::Command::TYPE_LINE: {
  722. const Item::CommandLine *line = static_cast<const Item::CommandLine *>(c);
  723. r.position = line->from;
  724. r.expand_to(line->to);
  725. } break;
  726. case Item::Command::TYPE_POLYLINE: {
  727. const Item::CommandPolyLine *pline = static_cast<const Item::CommandPolyLine *>(c);
  728. if (pline->triangles.size()) {
  729. for (int j = 0; j < pline->triangles.size(); j++) {
  730. if (j == 0) {
  731. r.position = pline->triangles[j];
  732. } else {
  733. r.expand_to(pline->triangles[j]);
  734. }
  735. }
  736. } else {
  737. for (int j = 0; j < pline->lines.size(); j++) {
  738. if (j == 0) {
  739. r.position = pline->lines[j];
  740. } else {
  741. r.expand_to(pline->lines[j]);
  742. }
  743. }
  744. }
  745. } break;
  746. case Item::Command::TYPE_RECT: {
  747. const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
  748. r = crect->rect;
  749. } break;
  750. case Item::Command::TYPE_NINEPATCH: {
  751. const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
  752. r = style->rect;
  753. } break;
  754. case Item::Command::TYPE_PRIMITIVE: {
  755. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  756. r.position = primitive->points[0];
  757. for (int j = 1; j < primitive->points.size(); j++) {
  758. r.expand_to(primitive->points[j]);
  759. }
  760. } break;
  761. case Item::Command::TYPE_POLYGON: {
  762. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  763. int l = polygon->points.size();
  764. const Point2 *pp = &polygon->points[0];
  765. r.position = pp[0];
  766. for (int j = 1; j < l; j++) {
  767. r.expand_to(pp[j]);
  768. }
  769. if (skeleton != RID()) {
  770. // calculate bone AABBs
  771. int bone_count = RasterizerStorage::base_singleton->skeleton_get_bone_count(skeleton);
  772. Vector<Rect2> bone_aabbs;
  773. bone_aabbs.resize(bone_count);
  774. Rect2 *bptr = bone_aabbs.ptrw();
  775. for (int j = 0; j < bone_count; j++) {
  776. bptr[j].size = Vector2(-1, -1); //negative means unused
  777. }
  778. if (l && polygon->bones.size() == l * 4 && polygon->weights.size() == polygon->bones.size()) {
  779. for (int j = 0; j < l; j++) {
  780. Point2 p = pp[j];
  781. for (int k = 0; k < 4; k++) {
  782. int idx = polygon->bones[j * 4 + k];
  783. float w = polygon->weights[j * 4 + k];
  784. if (w == 0) {
  785. continue;
  786. }
  787. if (bptr[idx].size.x < 0) {
  788. //first
  789. bptr[idx] = Rect2(p, Vector2(0.00001, 0.00001));
  790. } else {
  791. bptr[idx].expand_to(p);
  792. }
  793. }
  794. }
  795. Rect2 aabb;
  796. bool first_bone = true;
  797. for (int j = 0; j < bone_count; j++) {
  798. Transform2D mtx = RasterizerStorage::base_singleton->skeleton_bone_get_transform_2d(skeleton, j);
  799. Rect2 baabb = mtx.xform(bone_aabbs[j]);
  800. if (first_bone) {
  801. aabb = baabb;
  802. first_bone = false;
  803. } else {
  804. aabb = aabb.merge(baabb);
  805. }
  806. }
  807. r = r.merge(aabb);
  808. }
  809. }
  810. } break;
  811. case Item::Command::TYPE_MESH: {
  812. const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
  813. AABB aabb = RasterizerStorage::base_singleton->mesh_get_aabb(mesh->mesh, RID());
  814. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  815. } break;
  816. case Item::Command::TYPE_MULTIMESH: {
  817. const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
  818. AABB aabb = RasterizerStorage::base_singleton->multimesh_get_aabb(multimesh->multimesh);
  819. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  820. } break;
  821. case Item::Command::TYPE_PARTICLES: {
  822. const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
  823. if (particles_cmd->particles.is_valid()) {
  824. AABB aabb = RasterizerStorage::base_singleton->particles_get_aabb(particles_cmd->particles);
  825. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  826. }
  827. } break;
  828. case Item::Command::TYPE_CIRCLE: {
  829. const Item::CommandCircle *circle = static_cast<const Item::CommandCircle *>(c);
  830. r.position = Point2(-circle->radius, -circle->radius) + circle->pos;
  831. r.size = Point2(circle->radius * 2.0, circle->radius * 2.0);
  832. } break;
  833. case Item::Command::TYPE_TRANSFORM: {
  834. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  835. xf = transform->xform;
  836. found_xform = true;
  837. continue;
  838. } break;
  839. case Item::Command::TYPE_CLIP_IGNORE: {
  840. } break;
  841. }
  842. if (found_xform) {
  843. r = xf.xform(r);
  844. }
  845. if (first) {
  846. rect = r;
  847. first = false;
  848. } else {
  849. rect = rect.merge(r);
  850. }
  851. }
  852. rect_dirty = false;
  853. return rect;
  854. }
  855. void clear() {
  856. for (int i = 0; i < commands.size(); i++) {
  857. memdelete(commands[i]);
  858. }
  859. commands.clear();
  860. clip = false;
  861. rect_dirty = true;
  862. final_clip_owner = nullptr;
  863. material_owner = nullptr;
  864. light_masked = false;
  865. }
  866. Item() {
  867. light_mask = 1;
  868. vp_render = nullptr;
  869. next = nullptr;
  870. final_clip_owner = nullptr;
  871. clip = false;
  872. final_modulate = Color(1, 1, 1, 1);
  873. visible = true;
  874. rect_dirty = true;
  875. custom_rect = false;
  876. behind = false;
  877. material_owner = nullptr;
  878. copy_back_buffer = nullptr;
  879. distance_field = false;
  880. light_masked = false;
  881. update_when_visible = false;
  882. }
  883. virtual ~Item() {
  884. clear();
  885. if (copy_back_buffer) {
  886. memdelete(copy_back_buffer);
  887. }
  888. }
  889. };
  890. virtual void canvas_begin() = 0;
  891. virtual void canvas_end() = 0;
  892. virtual void canvas_render_items_begin(const Color &p_modulate, Light *p_light, const Transform2D &p_base_transform) {}
  893. virtual void canvas_render_items_end() {}
  894. 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;
  895. virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0;
  896. struct LightOccluderInstance : public RID_Data {
  897. bool enabled;
  898. RID canvas;
  899. RID polygon;
  900. RID polygon_buffer;
  901. Rect2 aabb_cache;
  902. Transform2D xform;
  903. Transform2D xform_cache;
  904. int light_mask;
  905. VS::CanvasOccluderPolygonCullMode cull_cache;
  906. LightOccluderInstance *next;
  907. LightOccluderInstance() {
  908. enabled = true;
  909. next = nullptr;
  910. light_mask = 1;
  911. cull_cache = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  912. }
  913. };
  914. 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;
  915. virtual void reset_canvas() = 0;
  916. virtual void draw_window_margins(int *p_margins, RID *p_margin_textures) = 0;
  917. virtual ~RasterizerCanvas() {}
  918. };
  919. class Rasterizer {
  920. protected:
  921. static Rasterizer *(*_create_func)();
  922. public:
  923. static Rasterizer *create();
  924. virtual RasterizerStorage *get_storage() = 0;
  925. virtual RasterizerCanvas *get_canvas() = 0;
  926. virtual RasterizerScene *get_scene() = 0;
  927. virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0;
  928. virtual void set_shader_time_scale(float p_scale) = 0;
  929. virtual void initialize() = 0;
  930. virtual void begin_frame(double frame_step) = 0;
  931. virtual void set_current_render_target(RID p_render_target) = 0;
  932. virtual void restore_render_target(bool p_3d) = 0;
  933. virtual void clear_render_target(const Color &p_color) = 0;
  934. virtual void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0) = 0;
  935. 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;
  936. virtual void end_frame(bool p_swap_buffers) = 0;
  937. virtual void finalize() = 0;
  938. virtual bool is_low_end() const = 0;
  939. virtual ~Rasterizer() {}
  940. };
  941. #endif // RASTERIZER_H