rasterizer.h 44 KB

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