rasterizer.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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/rendering_server.h"
  34. #include "core/pair.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 void directional_shadow_atlas_set_size(int p_size) = 0;
  44. virtual int get_directional_light_shadow_size(RID p_light_intance) = 0;
  45. virtual void set_directional_shadow_count(int p_count) = 0;
  46. /* SKY API */
  47. virtual RID sky_create() = 0;
  48. virtual void sky_set_radiance_size(RID p_sky, int p_radiance_size) = 0;
  49. virtual void sky_set_mode(RID p_sky, RS::SkyMode p_samples) = 0;
  50. virtual void sky_set_material(RID p_sky, RID p_material) = 0;
  51. virtual Ref<Image> sky_bake_panorama(RID p_sky, float p_energy, bool p_bake_irradiance, const Size2i &p_size) = 0;
  52. /* ENVIRONMENT API */
  53. virtual RID environment_create() = 0;
  54. virtual void environment_set_background(RID p_env, RS::EnvironmentBG p_bg) = 0;
  55. virtual void environment_set_sky(RID p_env, RID p_sky) = 0;
  56. virtual void environment_set_sky_custom_fov(RID p_env, float p_scale) = 0;
  57. virtual void environment_set_sky_orientation(RID p_env, const Basis &p_orientation) = 0;
  58. virtual void environment_set_bg_color(RID p_env, const Color &p_color) = 0;
  59. virtual void environment_set_bg_energy(RID p_env, float p_energy) = 0;
  60. virtual void environment_set_canvas_max_layer(RID p_env, int p_max_layer) = 0;
  61. virtual void environment_set_ambient_light(RID p_env, const Color &p_color, RS::EnvironmentAmbientSource p_ambient = RS::ENV_AMBIENT_SOURCE_BG, float p_energy = 1.0, float p_sky_contribution = 0.0, RS::EnvironmentReflectionSource p_reflection_source = RS::ENV_REFLECTION_SOURCE_BG, const Color &p_ao_color = Color()) = 0;
  62. // FIXME: Disabled during Vulkan refactoring, should be ported.
  63. #if 0
  64. virtual void environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) = 0;
  65. #endif
  66. virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, RS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap) = 0;
  67. virtual void environment_glow_set_use_bicubic_upscale(bool p_enable) = 0;
  68. virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) = 0;
  69. 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) = 0;
  70. virtual void environment_set_ssr_roughness_quality(RS::EnvironmentSSRRoughnessQuality p_quality) = 0;
  71. virtual void environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_bias, float p_light_affect, float p_ao_channel_affect, RS::EnvironmentSSAOBlur p_blur, float p_bilateral_sharpness) = 0;
  72. virtual void environment_set_ssao_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size) = 0;
  73. virtual void environment_set_tonemap(RID p_env, RS::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;
  74. virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) = 0;
  75. 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;
  76. 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;
  77. 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;
  78. virtual Ref<Image> environment_bake_panorama(RID p_env, bool p_bake_irradiance, const Size2i &p_size) = 0;
  79. virtual bool is_environment(RID p_env) const = 0;
  80. virtual RS::EnvironmentBG environment_get_background(RID p_env) const = 0;
  81. virtual int environment_get_canvas_max_layer(RID p_env) const = 0;
  82. virtual RID camera_effects_create() = 0;
  83. virtual void camera_effects_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter) = 0;
  84. virtual void camera_effects_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape) = 0;
  85. virtual void camera_effects_set_dof_blur(RID p_camera_effects, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount) = 0;
  86. virtual void camera_effects_set_custom_exposure(RID p_camera_effects, bool p_enable, float p_exposure) = 0;
  87. virtual void shadows_quality_set(RS::ShadowQuality p_quality) = 0;
  88. virtual void directional_shadow_quality_set(RS::ShadowQuality p_quality) = 0;
  89. struct InstanceBase;
  90. struct InstanceDependency {
  91. void instance_notify_changed(bool p_aabb, bool p_dependencies);
  92. void instance_notify_deleted(RID p_deleted);
  93. ~InstanceDependency();
  94. private:
  95. friend struct InstanceBase;
  96. Map<InstanceBase *, uint32_t> instances;
  97. };
  98. struct InstanceBase {
  99. RS::InstanceType base_type;
  100. RID base;
  101. RID skeleton;
  102. RID material_override;
  103. RID instance_data;
  104. Transform transform;
  105. int depth_layer;
  106. uint32_t layer_mask;
  107. uint32_t instance_version;
  108. //RID sampled_light;
  109. Vector<RID> materials;
  110. Vector<RID> light_instances;
  111. Vector<RID> reflection_probe_instances;
  112. Vector<RID> gi_probe_instances;
  113. Vector<float> blend_values;
  114. RS::ShadowCastingSetting cast_shadows;
  115. //fit in 32 bits
  116. bool mirror : 8;
  117. bool receive_shadows : 8;
  118. bool visible : 8;
  119. bool baked_light : 2; //this flag is only to know if it actually did use baked light
  120. bool dynamic_gi : 2; //this flag is only to know if it actually did use baked light
  121. bool redraw_if_visible : 4;
  122. float depth; //used for sorting
  123. SelfList<InstanceBase> dependency_item;
  124. InstanceBase *lightmap;
  125. Rect2 lightmap_uv_scale;
  126. int lightmap_slice_index;
  127. uint32_t lightmap_cull_index;
  128. Vector<Color> lightmap_sh; //spherical harmonic
  129. AABB aabb;
  130. AABB transformed_aabb;
  131. struct InstanceShaderParameter {
  132. int32_t index = -1;
  133. Variant value;
  134. Variant default_value;
  135. PropertyInfo info;
  136. };
  137. Map<StringName, InstanceShaderParameter> instance_shader_parameters;
  138. bool instance_allocated_shader_parameters = false;
  139. int32_t instance_allocated_shader_parameters_offset = -1;
  140. virtual void dependency_deleted(RID p_dependency) {}
  141. virtual void dependency_changed(bool p_aabb, bool p_dependencies) {}
  142. Set<InstanceDependency *> dependencies;
  143. void instance_increase_version() {
  144. instance_version++;
  145. }
  146. void update_dependency(InstanceDependency *p_dependency) {
  147. dependencies.insert(p_dependency);
  148. p_dependency->instances[this] = instance_version;
  149. }
  150. void clean_up_dependencies() {
  151. List<Pair<InstanceDependency *, Map<InstanceBase *, uint32_t>::Element *>> to_clean_up;
  152. for (Set<InstanceDependency *>::Element *E = dependencies.front(); E; E = E->next()) {
  153. InstanceDependency *dep = E->get();
  154. Map<InstanceBase *, uint32_t>::Element *F = dep->instances.find(this);
  155. ERR_CONTINUE(!F);
  156. if (F->get() != instance_version) {
  157. Pair<InstanceDependency *, Map<InstanceBase *, uint32_t>::Element *> p;
  158. p.first = dep;
  159. p.second = F;
  160. to_clean_up.push_back(p);
  161. }
  162. }
  163. while (to_clean_up.size()) {
  164. to_clean_up.front()->get().first->instances.erase(to_clean_up.front()->get().second);
  165. to_clean_up.pop_front();
  166. }
  167. }
  168. void clear_dependencies() {
  169. for (Set<InstanceDependency *>::Element *E = dependencies.front(); E; E = E->next()) {
  170. InstanceDependency *dep = E->get();
  171. dep->instances.erase(this);
  172. }
  173. dependencies.clear();
  174. }
  175. InstanceBase() :
  176. dependency_item(this) {
  177. base_type = RS::INSTANCE_NONE;
  178. cast_shadows = RS::SHADOW_CASTING_SETTING_ON;
  179. receive_shadows = true;
  180. visible = true;
  181. depth_layer = 0;
  182. layer_mask = 1;
  183. instance_version = 0;
  184. baked_light = false;
  185. dynamic_gi = false;
  186. redraw_if_visible = false;
  187. lightmap_slice_index = 0;
  188. lightmap = nullptr;
  189. lightmap_cull_index = 0;
  190. }
  191. virtual ~InstanceBase() {
  192. clear_dependencies();
  193. }
  194. };
  195. virtual RID light_instance_create(RID p_light) = 0;
  196. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform) = 0;
  197. 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_shadow_texel_size, float p_bias_scale = 1.0, float p_range_begin = 0, const Vector2 &p_uv_scale = Vector2()) = 0;
  198. virtual void light_instance_mark_visible(RID p_light_instance) = 0;
  199. virtual bool light_instances_can_render_shadow_cube() const {
  200. return true;
  201. }
  202. virtual RID reflection_atlas_create() = 0;
  203. virtual void reflection_atlas_set_size(RID p_ref_atlas, int p_reflection_size, int p_reflection_count) = 0;
  204. virtual RID reflection_probe_instance_create(RID p_probe) = 0;
  205. virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) = 0;
  206. virtual void reflection_probe_release_atlas_index(RID p_instance) = 0;
  207. virtual bool reflection_probe_instance_needs_redraw(RID p_instance) = 0;
  208. virtual bool reflection_probe_instance_has_reflection(RID p_instance) = 0;
  209. virtual bool reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) = 0;
  210. virtual bool reflection_probe_instance_postprocess_step(RID p_instance) = 0;
  211. virtual RID decal_instance_create(RID p_decal) = 0;
  212. virtual void decal_instance_set_transform(RID p_decal, const Transform &p_transform) = 0;
  213. virtual RID gi_probe_instance_create(RID p_gi_probe) = 0;
  214. virtual void gi_probe_instance_set_transform_to_data(RID p_probe, const Transform &p_xform) = 0;
  215. virtual bool gi_probe_needs_update(RID p_probe) const = 0;
  216. virtual void gi_probe_update(RID p_probe, bool p_update_light_instances, const Vector<RID> &p_light_instances, int p_dynamic_object_count, InstanceBase **p_dynamic_objects) = 0;
  217. virtual void render_scene(RID p_render_buffers, 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_gi_probe_cull_result, int p_gi_probe_cull_count, RID *p_decal_cull_result, int p_decal_cull_count, InstanceBase **p_lightmap_cull_result, int p_lightmap_cull_count, RID p_environment, RID p_camera_effects, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) = 0;
  218. virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) = 0;
  219. virtual void render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) = 0;
  220. virtual void set_scene_pass(uint64_t p_pass) = 0;
  221. virtual void set_time(double p_time, double p_step) = 0;
  222. virtual void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) = 0;
  223. virtual RID render_buffers_create() = 0;
  224. virtual void render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, RS::ViewportMSAA p_msaa, RS::ViewportScreenSpaceAA p_screen_space_aa) = 0;
  225. virtual void screen_space_roughness_limiter_set_active(bool p_enable, float p_curve) = 0;
  226. virtual bool screen_space_roughness_limiter_is_active() const = 0;
  227. virtual void sub_surface_scattering_set_quality(RS::SubSurfaceScatteringQuality p_quality) = 0;
  228. virtual void sub_surface_scattering_set_scale(float p_scale, float p_depth_scale) = 0;
  229. virtual TypedArray<Image> bake_render_uv2(RID p_base, const Vector<RID> &p_material_overrides, const Size2i &p_image_size) = 0;
  230. virtual bool free(RID p_rid) = 0;
  231. virtual void update() = 0;
  232. virtual ~RasterizerScene() {}
  233. };
  234. class RasterizerStorage {
  235. Color default_clear_color;
  236. public:
  237. /* TEXTURE API */
  238. virtual RID texture_2d_create(const Ref<Image> &p_image) = 0;
  239. virtual RID texture_2d_layered_create(const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) = 0;
  240. virtual RID texture_3d_create(const Vector<Ref<Image>> &p_slices) = 0; //all slices, then all the mipmaps, must be coherent
  241. virtual RID texture_proxy_create(RID p_base) = 0; //all slices, then all the mipmaps, must be coherent
  242. virtual void texture_2d_update_immediate(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) = 0; //mostly used for video and streaming
  243. virtual void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) = 0;
  244. virtual void texture_3d_update(RID p_texture, const Ref<Image> &p_image, int p_depth, int p_mipmap) = 0;
  245. virtual void texture_proxy_update(RID p_proxy, RID p_base) = 0;
  246. //these two APIs can be used together or in combination with the others.
  247. virtual RID texture_2d_placeholder_create() = 0;
  248. virtual RID texture_2d_layered_placeholder_create(RenderingServer::TextureLayeredType p_layered_type) = 0;
  249. virtual RID texture_3d_placeholder_create() = 0;
  250. virtual Ref<Image> texture_2d_get(RID p_texture) const = 0;
  251. virtual Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const = 0;
  252. virtual Ref<Image> texture_3d_slice_get(RID p_texture, int p_depth, int p_mipmap) const = 0;
  253. virtual void texture_replace(RID p_texture, RID p_by_texture) = 0;
  254. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) = 0;
  255. // FIXME: Disabled during Vulkan refactoring, should be ported.
  256. #if 0
  257. virtual void texture_bind(RID p_texture, uint32_t p_texture_no) = 0;
  258. #endif
  259. virtual void texture_set_path(RID p_texture, const String &p_path) = 0;
  260. virtual String texture_get_path(RID p_texture) const = 0;
  261. virtual void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) = 0;
  262. virtual void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) = 0;
  263. virtual void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) = 0;
  264. virtual void texture_debug_usage(List<RS::TextureInfo> *r_info) = 0;
  265. virtual void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) = 0;
  266. virtual Size2 texture_size_with_proxy(RID p_proxy) = 0;
  267. virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) = 0;
  268. virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) = 0;
  269. /* SHADER API */
  270. virtual RID shader_create() = 0;
  271. virtual void shader_set_code(RID p_shader, const String &p_code) = 0;
  272. virtual String shader_get_code(RID p_shader) const = 0;
  273. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const = 0;
  274. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) = 0;
  275. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const = 0;
  276. virtual Variant shader_get_param_default(RID p_material, const StringName &p_param) const = 0;
  277. /* COMMON MATERIAL API */
  278. virtual RID material_create() = 0;
  279. virtual void material_set_render_priority(RID p_material, int priority) = 0;
  280. virtual void material_set_shader(RID p_shader_material, RID p_shader) = 0;
  281. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0;
  282. virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0;
  283. virtual void material_set_next_pass(RID p_material, RID p_next_material) = 0;
  284. virtual bool material_is_animated(RID p_material) = 0;
  285. virtual bool material_casts_shadows(RID p_material) = 0;
  286. struct InstanceShaderParam {
  287. PropertyInfo info;
  288. int index;
  289. Variant default_value;
  290. };
  291. virtual void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) = 0;
  292. virtual void material_update_dependency(RID p_material, RasterizerScene::InstanceBase *p_instance) = 0;
  293. /* MESH API */
  294. virtual RID mesh_create() = 0;
  295. /// Returns stride
  296. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) = 0;
  297. virtual int mesh_get_blend_shape_count(RID p_mesh) const = 0;
  298. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) = 0;
  299. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const = 0;
  300. virtual void mesh_surface_update_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) = 0;
  301. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) = 0;
  302. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;
  303. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const = 0;
  304. virtual int mesh_get_surface_count(RID p_mesh) const = 0;
  305. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
  306. virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;
  307. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) = 0;
  308. virtual void mesh_clear(RID p_mesh) = 0;
  309. /* MULTIMESH API */
  310. virtual RID multimesh_create() = 0;
  311. virtual void multimesh_allocate(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false) = 0;
  312. virtual int multimesh_get_instance_count(RID p_multimesh) const = 0;
  313. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh) = 0;
  314. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) = 0;
  315. virtual void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) = 0;
  316. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) = 0;
  317. virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) = 0;
  318. virtual RID multimesh_get_mesh(RID p_multimesh) const = 0;
  319. virtual Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const = 0;
  320. virtual Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const = 0;
  321. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const = 0;
  322. virtual Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const = 0;
  323. virtual void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) = 0;
  324. virtual Vector<float> multimesh_get_buffer(RID p_multimesh) const = 0;
  325. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible) = 0;
  326. virtual int multimesh_get_visible_instances(RID p_multimesh) const = 0;
  327. virtual AABB multimesh_get_aabb(RID p_multimesh) const = 0;
  328. /* IMMEDIATE API */
  329. virtual RID immediate_create() = 0;
  330. virtual void immediate_begin(RID p_immediate, RS::PrimitiveType p_rimitive, RID p_texture = RID()) = 0;
  331. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) = 0;
  332. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal) = 0;
  333. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent) = 0;
  334. virtual void immediate_color(RID p_immediate, const Color &p_color) = 0;
  335. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv) = 0;
  336. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) = 0;
  337. virtual void immediate_end(RID p_immediate) = 0;
  338. virtual void immediate_clear(RID p_immediate) = 0;
  339. virtual void immediate_set_material(RID p_immediate, RID p_material) = 0;
  340. virtual RID immediate_get_material(RID p_immediate) const = 0;
  341. virtual AABB immediate_get_aabb(RID p_immediate) const = 0;
  342. /* SKELETON API */
  343. virtual RID skeleton_create() = 0;
  344. virtual void skeleton_allocate(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) = 0;
  345. virtual int skeleton_get_bone_count(RID p_skeleton) const = 0;
  346. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) = 0;
  347. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) const = 0;
  348. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) = 0;
  349. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const = 0;
  350. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) = 0;
  351. /* Light API */
  352. virtual RID light_create(RS::LightType p_type) = 0;
  353. RID directional_light_create() { return light_create(RS::LIGHT_DIRECTIONAL); }
  354. RID omni_light_create() { return light_create(RS::LIGHT_OMNI); }
  355. RID spot_light_create() { return light_create(RS::LIGHT_SPOT); }
  356. virtual void light_set_color(RID p_light, const Color &p_color) = 0;
  357. virtual void light_set_param(RID p_light, RS::LightParam p_param, float p_value) = 0;
  358. virtual void light_set_shadow(RID p_light, bool p_enabled) = 0;
  359. virtual void light_set_shadow_color(RID p_light, const Color &p_color) = 0;
  360. virtual void light_set_projector(RID p_light, RID p_texture) = 0;
  361. virtual void light_set_negative(RID p_light, bool p_enable) = 0;
  362. virtual void light_set_cull_mask(RID p_light, uint32_t p_mask) = 0;
  363. virtual void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) = 0;
  364. virtual void light_set_use_gi(RID p_light, bool p_enable) = 0;
  365. virtual void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) = 0;
  366. virtual void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) = 0;
  367. virtual void light_directional_set_blend_splits(RID p_light, bool p_enable) = 0;
  368. virtual bool light_directional_get_blend_splits(RID p_light) const = 0;
  369. virtual void light_directional_set_shadow_depth_range_mode(RID p_light, RS::LightDirectionalShadowDepthRangeMode p_range_mode) = 0;
  370. virtual RS::LightDirectionalShadowDepthRangeMode light_directional_get_shadow_depth_range_mode(RID p_light) const = 0;
  371. virtual RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) = 0;
  372. virtual RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) = 0;
  373. virtual bool light_has_shadow(RID p_light) const = 0;
  374. virtual RS::LightType light_get_type(RID p_light) const = 0;
  375. virtual AABB light_get_aabb(RID p_light) const = 0;
  376. virtual float light_get_param(RID p_light, RS::LightParam p_param) = 0;
  377. virtual Color light_get_color(RID p_light) = 0;
  378. virtual bool light_get_use_gi(RID p_light) = 0;
  379. virtual uint64_t light_get_version(RID p_light) const = 0;
  380. /* PROBE API */
  381. virtual RID reflection_probe_create() = 0;
  382. virtual void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) = 0;
  383. virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution) = 0;
  384. virtual void reflection_probe_set_intensity(RID p_probe, float p_intensity) = 0;
  385. virtual void reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) = 0;
  386. virtual void reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) = 0;
  387. virtual void reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) = 0;
  388. virtual void reflection_probe_set_max_distance(RID p_probe, float p_distance) = 0;
  389. virtual void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) = 0;
  390. virtual void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) = 0;
  391. virtual void reflection_probe_set_as_interior(RID p_probe, bool p_enable) = 0;
  392. virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) = 0;
  393. virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) = 0;
  394. virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) = 0;
  395. virtual AABB reflection_probe_get_aabb(RID p_probe) const = 0;
  396. virtual RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const = 0;
  397. virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const = 0;
  398. virtual Vector3 reflection_probe_get_extents(RID p_probe) const = 0;
  399. virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const = 0;
  400. virtual float reflection_probe_get_origin_max_distance(RID p_probe) const = 0;
  401. virtual bool reflection_probe_renders_shadows(RID p_probe) const = 0;
  402. virtual void base_update_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  403. virtual void skeleton_update_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) = 0;
  404. /* DECAL API */
  405. virtual RID decal_create() = 0;
  406. virtual void decal_set_extents(RID p_decal, const Vector3 &p_extents) = 0;
  407. virtual void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) = 0;
  408. virtual void decal_set_emission_energy(RID p_decal, float p_energy) = 0;
  409. virtual void decal_set_albedo_mix(RID p_decal, float p_mix) = 0;
  410. virtual void decal_set_modulate(RID p_decal, const Color &p_modulate) = 0;
  411. virtual void decal_set_cull_mask(RID p_decal, uint32_t p_layers) = 0;
  412. virtual void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) = 0;
  413. virtual void decal_set_fade(RID p_decal, float p_above, float p_below) = 0;
  414. virtual void decal_set_normal_fade(RID p_decal, float p_fade) = 0;
  415. virtual AABB decal_get_aabb(RID p_decal) const = 0;
  416. /* GI PROBE API */
  417. virtual RID gi_probe_create() = 0;
  418. virtual void gi_probe_allocate(RID p_gi_probe, const Transform &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) = 0;
  419. virtual AABB gi_probe_get_bounds(RID p_gi_probe) const = 0;
  420. virtual Vector3i gi_probe_get_octree_size(RID p_gi_probe) const = 0;
  421. virtual Vector<uint8_t> gi_probe_get_octree_cells(RID p_gi_probe) const = 0;
  422. virtual Vector<uint8_t> gi_probe_get_data_cells(RID p_gi_probe) const = 0;
  423. virtual Vector<uint8_t> gi_probe_get_distance_field(RID p_gi_probe) const = 0;
  424. virtual Vector<int> gi_probe_get_level_counts(RID p_gi_probe) const = 0;
  425. virtual Transform gi_probe_get_to_cell_xform(RID p_gi_probe) const = 0;
  426. virtual void gi_probe_set_dynamic_range(RID p_gi_probe, float p_range) = 0;
  427. virtual float gi_probe_get_dynamic_range(RID p_gi_probe) const = 0;
  428. virtual void gi_probe_set_propagation(RID p_gi_probe, float p_range) = 0;
  429. virtual float gi_probe_get_propagation(RID p_gi_probe) const = 0;
  430. virtual void gi_probe_set_energy(RID p_gi_probe, float p_energy) = 0;
  431. virtual float gi_probe_get_energy(RID p_gi_probe) const = 0;
  432. virtual void gi_probe_set_ao(RID p_gi_probe, float p_ao) = 0;
  433. virtual float gi_probe_get_ao(RID p_gi_probe) const = 0;
  434. virtual void gi_probe_set_ao_size(RID p_gi_probe, float p_strength) = 0;
  435. virtual float gi_probe_get_ao_size(RID p_gi_probe) const = 0;
  436. virtual void gi_probe_set_bias(RID p_gi_probe, float p_bias) = 0;
  437. virtual float gi_probe_get_bias(RID p_gi_probe) const = 0;
  438. virtual void gi_probe_set_normal_bias(RID p_gi_probe, float p_range) = 0;
  439. virtual float gi_probe_get_normal_bias(RID p_gi_probe) const = 0;
  440. virtual void gi_probe_set_interior(RID p_gi_probe, bool p_enable) = 0;
  441. virtual bool gi_probe_is_interior(RID p_gi_probe) const = 0;
  442. virtual void gi_probe_set_use_two_bounces(RID p_gi_probe, bool p_enable) = 0;
  443. virtual bool gi_probe_is_using_two_bounces(RID p_gi_probe) const = 0;
  444. virtual void gi_probe_set_anisotropy_strength(RID p_gi_probe, float p_strength) = 0;
  445. virtual float gi_probe_get_anisotropy_strength(RID p_gi_probe) const = 0;
  446. virtual uint32_t gi_probe_get_version(RID p_probe) = 0;
  447. /* LIGHTMAP CAPTURE */
  448. virtual RID lightmap_create() = 0;
  449. virtual void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) = 0;
  450. virtual void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) = 0;
  451. virtual void lightmap_set_probe_interior(RID p_lightmap, bool p_interior) = 0;
  452. virtual void lightmap_set_probe_capture_data(RID p_lightmap, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree) = 0;
  453. virtual PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const = 0;
  454. virtual PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const = 0;
  455. virtual PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const = 0;
  456. virtual PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const = 0;
  457. virtual AABB lightmap_get_aabb(RID p_lightmap) const = 0;
  458. virtual void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) = 0;
  459. virtual bool lightmap_is_interior(RID p_lightmap) const = 0;
  460. virtual void lightmap_set_probe_capture_update_speed(float p_speed) = 0;
  461. virtual float lightmap_get_probe_capture_update_speed() const = 0;
  462. /* PARTICLES */
  463. virtual RID particles_create() = 0;
  464. virtual void particles_set_emitting(RID p_particles, bool p_emitting) = 0;
  465. virtual bool particles_get_emitting(RID p_particles) = 0;
  466. virtual void particles_set_amount(RID p_particles, int p_amount) = 0;
  467. virtual void particles_set_lifetime(RID p_particles, float p_lifetime) = 0;
  468. virtual void particles_set_one_shot(RID p_particles, bool p_one_shot) = 0;
  469. virtual void particles_set_pre_process_time(RID p_particles, float p_time) = 0;
  470. virtual void particles_set_explosiveness_ratio(RID p_particles, float p_ratio) = 0;
  471. virtual void particles_set_randomness_ratio(RID p_particles, float p_ratio) = 0;
  472. virtual void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) = 0;
  473. virtual void particles_set_speed_scale(RID p_particles, float p_scale) = 0;
  474. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable) = 0;
  475. virtual void particles_set_process_material(RID p_particles, RID p_material) = 0;
  476. virtual void particles_set_fixed_fps(RID p_particles, int p_fps) = 0;
  477. virtual void particles_set_fractional_delta(RID p_particles, bool p_enable) = 0;
  478. virtual void particles_restart(RID p_particles) = 0;
  479. virtual bool particles_is_inactive(RID p_particles) const = 0;
  480. virtual void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) = 0;
  481. virtual void particles_set_draw_passes(RID p_particles, int p_count) = 0;
  482. virtual void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) = 0;
  483. virtual void particles_request_process(RID p_particles) = 0;
  484. virtual AABB particles_get_current_aabb(RID p_particles) = 0;
  485. virtual AABB particles_get_aabb(RID p_particles) const = 0;
  486. virtual void particles_set_emission_transform(RID p_particles, const Transform &p_transform) = 0;
  487. virtual int particles_get_draw_passes(RID p_particles) const = 0;
  488. virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const = 0;
  489. /* GLOBAL VARIABLES */
  490. virtual void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) = 0;
  491. virtual void global_variable_remove(const StringName &p_name) = 0;
  492. virtual Vector<StringName> global_variable_get_list() const = 0;
  493. virtual void global_variable_set(const StringName &p_name, const Variant &p_value) = 0;
  494. virtual void global_variable_set_override(const StringName &p_name, const Variant &p_value) = 0;
  495. virtual Variant global_variable_get(const StringName &p_name) const = 0;
  496. virtual RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const = 0;
  497. virtual void global_variables_load_settings(bool p_load_textures = true) = 0;
  498. virtual void global_variables_clear() = 0;
  499. virtual int32_t global_variables_instance_allocate(RID p_instance) = 0;
  500. virtual void global_variables_instance_free(RID p_instance) = 0;
  501. virtual void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) = 0;
  502. /* RENDER TARGET */
  503. enum RenderTargetFlags {
  504. RENDER_TARGET_TRANSPARENT,
  505. RENDER_TARGET_DIRECT_TO_SCREEN,
  506. RENDER_TARGET_FLAG_MAX
  507. };
  508. virtual RID render_target_create() = 0;
  509. virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) = 0;
  510. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height) = 0;
  511. virtual RID render_target_get_texture(RID p_render_target) = 0;
  512. virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) = 0;
  513. virtual void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) = 0;
  514. virtual bool render_target_was_used(RID p_render_target) = 0;
  515. virtual void render_target_set_as_unused(RID p_render_target) = 0;
  516. virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color) = 0;
  517. virtual bool render_target_is_clear_requested(RID p_render_target) = 0;
  518. virtual Color render_target_get_clear_request_color(RID p_render_target) = 0;
  519. virtual void render_target_disable_clear_request(RID p_render_target) = 0;
  520. virtual void render_target_do_clear_request(RID p_render_target) = 0;
  521. virtual RS::InstanceType get_base_type(RID p_rid) const = 0;
  522. virtual bool free(RID p_rid) = 0;
  523. virtual bool has_os_feature(const String &p_feature) const = 0;
  524. virtual void update_dirty_resources() = 0;
  525. virtual void set_debug_generate_wireframes(bool p_generate) = 0;
  526. virtual void render_info_begin_capture() = 0;
  527. virtual void render_info_end_capture() = 0;
  528. virtual int get_captured_render_info(RS::RenderInfo p_info) = 0;
  529. virtual int get_render_info(RS::RenderInfo p_info) = 0;
  530. virtual String get_video_adapter_name() const = 0;
  531. virtual String get_video_adapter_vendor() const = 0;
  532. static RasterizerStorage *base_singleton;
  533. void set_default_clear_color(const Color &p_color) {
  534. default_clear_color = p_color;
  535. }
  536. Color get_default_clear_color() const {
  537. return default_clear_color;
  538. }
  539. #define TIMESTAMP_BEGIN() \
  540. { \
  541. if (RSG::storage->capturing_timestamps) \
  542. RSG::storage->capture_timestamps_begin(); \
  543. }
  544. #define RENDER_TIMESTAMP(m_text) \
  545. { \
  546. if (RSG::storage->capturing_timestamps) \
  547. RSG::storage->capture_timestamp(m_text); \
  548. }
  549. bool capturing_timestamps = false;
  550. virtual void capture_timestamps_begin() = 0;
  551. virtual void capture_timestamp(const String &p_name) = 0;
  552. virtual uint32_t get_captured_timestamps_count() const = 0;
  553. virtual uint64_t get_captured_timestamps_frame() const = 0;
  554. virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const = 0;
  555. virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const = 0;
  556. virtual String get_captured_timestamp_name(uint32_t p_index) const = 0;
  557. RasterizerStorage();
  558. virtual ~RasterizerStorage() {}
  559. };
  560. class RasterizerCanvas {
  561. public:
  562. static RasterizerCanvas *singleton;
  563. enum CanvasRectFlags {
  564. CANVAS_RECT_REGION = 1,
  565. CANVAS_RECT_TILE = 2,
  566. CANVAS_RECT_FLIP_H = 4,
  567. CANVAS_RECT_FLIP_V = 8,
  568. CANVAS_RECT_TRANSPOSE = 16,
  569. CANVAS_RECT_CLIP_UV = 32
  570. };
  571. struct Light {
  572. bool enabled;
  573. Color color;
  574. Transform2D xform;
  575. float height;
  576. float energy;
  577. float scale;
  578. int z_min;
  579. int z_max;
  580. int layer_min;
  581. int layer_max;
  582. int item_mask;
  583. int item_shadow_mask;
  584. RS::CanvasLightMode mode;
  585. RID texture;
  586. Vector2 texture_offset;
  587. RID canvas;
  588. bool use_shadow;
  589. int shadow_buffer_size;
  590. RS::CanvasLightShadowFilter shadow_filter;
  591. Color shadow_color;
  592. float shadow_smooth;
  593. //void *texture_cache; // implementation dependent
  594. Rect2 rect_cache;
  595. Transform2D xform_cache;
  596. float radius_cache; //used for shadow far plane
  597. //CameraMatrix shadow_matrix_cache;
  598. Transform2D light_shader_xform;
  599. //Vector2 light_shader_pos;
  600. Light *shadows_next_ptr;
  601. Light *filter_next_ptr;
  602. Light *next_ptr;
  603. Light *mask_next_ptr;
  604. RID light_internal;
  605. uint64_t version;
  606. int32_t render_index_cache;
  607. Light() {
  608. version = 0;
  609. enabled = true;
  610. color = Color(1, 1, 1);
  611. shadow_color = Color(0, 0, 0, 0);
  612. height = 0;
  613. z_min = -1024;
  614. z_max = 1024;
  615. layer_min = 0;
  616. layer_max = 0;
  617. item_mask = 1;
  618. scale = 1.0;
  619. energy = 1.0;
  620. item_shadow_mask = -1;
  621. mode = RS::CANVAS_LIGHT_MODE_ADD;
  622. // texture_cache = nullptr;
  623. next_ptr = nullptr;
  624. mask_next_ptr = nullptr;
  625. filter_next_ptr = nullptr;
  626. use_shadow = false;
  627. shadow_buffer_size = 2048;
  628. shadow_filter = RS::CANVAS_LIGHT_FILTER_NONE;
  629. shadow_smooth = 0.0;
  630. render_index_cache = -1;
  631. }
  632. };
  633. typedef uint64_t TextureBindingID;
  634. virtual TextureBindingID request_texture_binding(RID p_texture, RID p_normalmap, RID p_specular, RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat, RID p_multimesh) = 0;
  635. virtual void free_texture_binding(TextureBindingID p_binding) = 0;
  636. //easier wrap to avoid mistakes
  637. struct Item;
  638. struct TextureBinding {
  639. TextureBindingID binding_id;
  640. _FORCE_INLINE_ void create(RS::CanvasItemTextureFilter p_item_filter, RS::CanvasItemTextureRepeat p_item_repeat, RID p_texture, RID p_normalmap, RID p_specular, RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat, RID p_multimesh) {
  641. if (p_filter == RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT) {
  642. p_filter = p_item_filter;
  643. }
  644. if (p_repeat == RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT) {
  645. p_repeat = p_item_repeat;
  646. }
  647. if (p_texture != RID() || p_normalmap != RID() || p_specular != RID() || p_filter != RS::CANVAS_ITEM_TEXTURE_FILTER_DEFAULT || p_repeat != RS::CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT || p_multimesh.is_valid()) {
  648. ERR_FAIL_COND(binding_id != 0);
  649. binding_id = singleton->request_texture_binding(p_texture, p_normalmap, p_specular, p_filter, p_repeat, p_multimesh);
  650. }
  651. }
  652. _FORCE_INLINE_ TextureBinding() { binding_id = 0; }
  653. _FORCE_INLINE_ ~TextureBinding() {
  654. if (binding_id)
  655. singleton->free_texture_binding(binding_id);
  656. }
  657. };
  658. typedef uint64_t PolygonID;
  659. virtual PolygonID request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>()) = 0;
  660. virtual void free_polygon(PolygonID p_polygon) = 0;
  661. //also easier to wrap to avoid mistakes
  662. struct Polygon {
  663. PolygonID polygon_id;
  664. Rect2 rect_cache;
  665. _FORCE_INLINE_ void create(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>()) {
  666. ERR_FAIL_COND(polygon_id != 0);
  667. {
  668. uint32_t pc = p_points.size();
  669. const Vector2 *v2 = p_points.ptr();
  670. rect_cache.position = *v2;
  671. for (uint32_t i = 1; i < pc; i++) {
  672. rect_cache.expand_to(v2[i]);
  673. }
  674. }
  675. polygon_id = singleton->request_polygon(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
  676. }
  677. _FORCE_INLINE_ Polygon() { polygon_id = 0; }
  678. _FORCE_INLINE_ ~Polygon() {
  679. if (polygon_id)
  680. singleton->free_polygon(polygon_id);
  681. }
  682. };
  683. //item
  684. struct Item {
  685. //commands are allocated in blocks of 4k to improve performance
  686. //and cache coherence.
  687. //blocks always grow but never shrink.
  688. struct CommandBlock {
  689. enum {
  690. MAX_SIZE = 4096
  691. };
  692. uint32_t usage;
  693. uint8_t *memory;
  694. };
  695. struct Command {
  696. enum Type {
  697. TYPE_RECT,
  698. TYPE_NINEPATCH,
  699. TYPE_POLYGON,
  700. TYPE_PRIMITIVE,
  701. TYPE_MESH,
  702. TYPE_MULTIMESH,
  703. TYPE_PARTICLES,
  704. TYPE_TRANSFORM,
  705. TYPE_CLIP_IGNORE,
  706. };
  707. Command *next;
  708. Type type;
  709. virtual ~Command() {}
  710. };
  711. struct CommandRect : public Command {
  712. Rect2 rect;
  713. Color modulate;
  714. Rect2 source;
  715. uint8_t flags;
  716. Color specular_shininess;
  717. TextureBinding texture_binding;
  718. CommandRect() {
  719. flags = 0;
  720. type = TYPE_RECT;
  721. }
  722. };
  723. struct CommandNinePatch : public Command {
  724. Rect2 rect;
  725. Rect2 source;
  726. float margin[4];
  727. bool draw_center;
  728. Color color;
  729. RS::NinePatchAxisMode axis_x;
  730. RS::NinePatchAxisMode axis_y;
  731. Color specular_shininess;
  732. TextureBinding texture_binding;
  733. CommandNinePatch() {
  734. draw_center = true;
  735. type = TYPE_NINEPATCH;
  736. }
  737. };
  738. struct CommandPolygon : public Command {
  739. RS::PrimitiveType primitive;
  740. Polygon polygon;
  741. Color specular_shininess;
  742. TextureBinding texture_binding;
  743. CommandPolygon() {
  744. type = TYPE_POLYGON;
  745. }
  746. };
  747. struct CommandPrimitive : public Command {
  748. uint32_t point_count;
  749. Vector2 points[4];
  750. Vector2 uvs[4];
  751. Color colors[4];
  752. Color specular_shininess;
  753. TextureBinding texture_binding;
  754. CommandPrimitive() {
  755. type = TYPE_PRIMITIVE;
  756. }
  757. };
  758. struct CommandMesh : public Command {
  759. RID mesh;
  760. Transform2D transform;
  761. Color modulate;
  762. Color specular_shininess;
  763. TextureBinding texture_binding;
  764. CommandMesh() { type = TYPE_MESH; }
  765. };
  766. struct CommandMultiMesh : public Command {
  767. RID multimesh;
  768. Color specular_shininess;
  769. TextureBinding texture_binding;
  770. CommandMultiMesh() { type = TYPE_MULTIMESH; }
  771. };
  772. struct CommandParticles : public Command {
  773. RID particles;
  774. Color specular_shininess;
  775. TextureBinding texture_binding;
  776. CommandParticles() { type = TYPE_PARTICLES; }
  777. };
  778. struct CommandTransform : public Command {
  779. Transform2D xform;
  780. CommandTransform() { type = TYPE_TRANSFORM; }
  781. };
  782. struct CommandClipIgnore : public Command {
  783. bool ignore;
  784. CommandClipIgnore() {
  785. type = TYPE_CLIP_IGNORE;
  786. ignore = false;
  787. }
  788. };
  789. struct ViewportRender {
  790. RenderingServer *owner;
  791. void *udata;
  792. Rect2 rect;
  793. };
  794. Transform2D xform;
  795. bool clip;
  796. bool visible;
  797. bool behind;
  798. bool update_when_visible;
  799. //RS::MaterialBlendMode blend_mode;
  800. int light_mask;
  801. int z_final;
  802. mutable bool custom_rect;
  803. mutable bool rect_dirty;
  804. mutable Rect2 rect;
  805. RID material;
  806. RID skeleton;
  807. Item *next;
  808. struct CopyBackBuffer {
  809. Rect2 rect;
  810. Rect2 screen_rect;
  811. bool full;
  812. };
  813. CopyBackBuffer *copy_back_buffer;
  814. Color final_modulate;
  815. Transform2D final_transform;
  816. Rect2 final_clip_rect;
  817. Item *final_clip_owner;
  818. Item *material_owner;
  819. ViewportRender *vp_render;
  820. bool distance_field;
  821. bool light_masked;
  822. Rect2 global_rect_cache;
  823. const Rect2 &get_rect() const {
  824. if (custom_rect || (!rect_dirty && !update_when_visible))
  825. return rect;
  826. //must update rect
  827. if (commands == nullptr) {
  828. rect = Rect2();
  829. rect_dirty = false;
  830. return rect;
  831. }
  832. Transform2D xf;
  833. bool found_xform = false;
  834. bool first = true;
  835. const Item::Command *c = commands;
  836. while (c) {
  837. Rect2 r;
  838. switch (c->type) {
  839. case Item::Command::TYPE_RECT: {
  840. const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
  841. r = crect->rect;
  842. } break;
  843. case Item::Command::TYPE_NINEPATCH: {
  844. const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
  845. r = style->rect;
  846. } break;
  847. case Item::Command::TYPE_POLYGON: {
  848. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  849. r = polygon->polygon.rect_cache;
  850. } break;
  851. case Item::Command::TYPE_PRIMITIVE: {
  852. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  853. for (uint32_t j = 0; j < primitive->point_count; j++) {
  854. if (j == 0) {
  855. r.position = primitive->points[0];
  856. } else {
  857. r.expand_to(primitive->points[j]);
  858. }
  859. }
  860. } break;
  861. case Item::Command::TYPE_MESH: {
  862. const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
  863. AABB aabb = RasterizerStorage::base_singleton->mesh_get_aabb(mesh->mesh, RID());
  864. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  865. } break;
  866. case Item::Command::TYPE_MULTIMESH: {
  867. const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
  868. AABB aabb = RasterizerStorage::base_singleton->multimesh_get_aabb(multimesh->multimesh);
  869. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  870. } break;
  871. case Item::Command::TYPE_PARTICLES: {
  872. const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
  873. if (particles_cmd->particles.is_valid()) {
  874. AABB aabb = RasterizerStorage::base_singleton->particles_get_aabb(particles_cmd->particles);
  875. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  876. }
  877. } break;
  878. case Item::Command::TYPE_TRANSFORM: {
  879. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  880. xf = transform->xform;
  881. found_xform = true;
  882. [[fallthrough]];
  883. }
  884. default: {
  885. c = c->next;
  886. continue;
  887. }
  888. }
  889. if (found_xform) {
  890. r = xf.xform(r);
  891. found_xform = false;
  892. }
  893. if (first) {
  894. rect = r;
  895. first = false;
  896. } else {
  897. rect = rect.merge(r);
  898. }
  899. c = c->next;
  900. }
  901. rect_dirty = false;
  902. return rect;
  903. }
  904. Command *commands;
  905. Command *last_command;
  906. Vector<CommandBlock> blocks;
  907. uint32_t current_block;
  908. template <class T>
  909. T *alloc_command() {
  910. T *command;
  911. if (commands == nullptr) {
  912. // As the most common use case of canvas items is to
  913. // use only one command, the first is done with it's
  914. // own allocation. The rest of them use blocks.
  915. command = memnew(T);
  916. command->next = nullptr;
  917. commands = command;
  918. last_command = command;
  919. } else {
  920. //Subsequent commands go into a block.
  921. while (true) {
  922. if (unlikely(current_block == (uint32_t)blocks.size())) {
  923. // If we need more blocks, we allocate them
  924. // (they won't be freed until this CanvasItem is
  925. // deleted, though).
  926. CommandBlock cb;
  927. cb.memory = (uint8_t *)memalloc(CommandBlock::MAX_SIZE);
  928. cb.usage = 0;
  929. blocks.push_back(cb);
  930. }
  931. CommandBlock *c = &blocks.write[current_block];
  932. size_t space_left = CommandBlock::MAX_SIZE - c->usage;
  933. if (space_left < sizeof(T)) {
  934. current_block++;
  935. continue;
  936. }
  937. //allocate block and add to the linked list
  938. void *memory = c->memory + c->usage;
  939. command = memnew_placement(memory, T);
  940. command->next = nullptr;
  941. last_command->next = command;
  942. last_command = command;
  943. c->usage += sizeof(T);
  944. break;
  945. }
  946. }
  947. rect_dirty = true;
  948. return command;
  949. }
  950. struct CustomData {
  951. virtual ~CustomData() {}
  952. };
  953. mutable CustomData *custom_data; //implementation dependent
  954. void clear() {
  955. Command *c = commands;
  956. while (c) {
  957. Command *n = c->next;
  958. if (c == commands) {
  959. memdelete(commands);
  960. commands = nullptr;
  961. } else {
  962. c->~Command();
  963. }
  964. c = n;
  965. }
  966. {
  967. uint32_t cbc = MIN((current_block + 1), (uint32_t)blocks.size());
  968. CommandBlock *blockptr = blocks.ptrw();
  969. for (uint32_t i = 0; i < cbc; i++) {
  970. blockptr[i].usage = 0;
  971. }
  972. }
  973. last_command = nullptr;
  974. commands = nullptr;
  975. current_block = 0;
  976. clip = false;
  977. rect_dirty = true;
  978. final_clip_owner = nullptr;
  979. material_owner = nullptr;
  980. light_masked = false;
  981. }
  982. Item() {
  983. commands = nullptr;
  984. last_command = nullptr;
  985. current_block = 0;
  986. light_mask = 1;
  987. vp_render = nullptr;
  988. next = nullptr;
  989. final_clip_owner = nullptr;
  990. clip = false;
  991. final_modulate = Color(1, 1, 1, 1);
  992. visible = true;
  993. rect_dirty = true;
  994. custom_rect = false;
  995. behind = false;
  996. material_owner = nullptr;
  997. copy_back_buffer = nullptr;
  998. distance_field = false;
  999. light_masked = false;
  1000. update_when_visible = false;
  1001. z_final = 0;
  1002. custom_data = nullptr;
  1003. }
  1004. virtual ~Item() {
  1005. clear();
  1006. for (int i = 0; i < blocks.size(); i++) {
  1007. memfree(blocks[i].memory);
  1008. }
  1009. if (copy_back_buffer)
  1010. memdelete(copy_back_buffer);
  1011. if (custom_data) {
  1012. memdelete(custom_data);
  1013. }
  1014. }
  1015. };
  1016. virtual void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform) = 0;
  1017. virtual void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) = 0;
  1018. struct LightOccluderInstance {
  1019. bool enabled;
  1020. RID canvas;
  1021. RID polygon;
  1022. RID occluder;
  1023. Rect2 aabb_cache;
  1024. Transform2D xform;
  1025. Transform2D xform_cache;
  1026. int light_mask;
  1027. RS::CanvasOccluderPolygonCullMode cull_cache;
  1028. LightOccluderInstance *next;
  1029. LightOccluderInstance() {
  1030. enabled = true;
  1031. next = nullptr;
  1032. light_mask = 1;
  1033. cull_cache = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  1034. }
  1035. };
  1036. virtual RID light_create() = 0;
  1037. virtual void light_set_texture(RID p_rid, RID p_texture) = 0;
  1038. virtual void light_set_use_shadow(RID p_rid, bool p_enable, int p_resolution) = 0;
  1039. virtual void light_update_shadow(RID p_rid, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) = 0;
  1040. virtual RID occluder_polygon_create() = 0;
  1041. virtual void occluder_polygon_set_shape_as_lines(RID p_occluder, const Vector<Vector2> &p_lines) = 0;
  1042. virtual void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) = 0;
  1043. virtual void draw_window_margins(int *p_margins, RID *p_margin_textures) = 0;
  1044. virtual bool free(RID p_rid) = 0;
  1045. virtual void update() = 0;
  1046. RasterizerCanvas() { singleton = this; }
  1047. virtual ~RasterizerCanvas() {}
  1048. };
  1049. class Rasterizer {
  1050. protected:
  1051. static Rasterizer *(*_create_func)();
  1052. public:
  1053. static Rasterizer *create();
  1054. virtual RasterizerStorage *get_storage() = 0;
  1055. virtual RasterizerCanvas *get_canvas() = 0;
  1056. virtual RasterizerScene *get_scene() = 0;
  1057. virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0;
  1058. virtual void initialize() = 0;
  1059. virtual void begin_frame(double frame_step) = 0;
  1060. struct BlitToScreen {
  1061. RID render_target;
  1062. Rect2i rect;
  1063. //lens distorted parameters for VR should go here
  1064. };
  1065. virtual void prepare_for_blitting_render_targets() = 0;
  1066. virtual void blit_render_targets_to_screen(DisplayServer::WindowID p_screen, const BlitToScreen *p_render_targets, int p_amount) = 0;
  1067. virtual void end_frame(bool p_swap_buffers) = 0;
  1068. virtual void finalize() = 0;
  1069. virtual uint64_t get_frame_number() const = 0;
  1070. virtual float get_frame_delta_time() const = 0;
  1071. virtual bool is_low_end() const = 0;
  1072. virtual ~Rasterizer() {}
  1073. };
  1074. #endif // RASTERIZER_H