rasterizer.h 51 KB

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