2
0

rasterizer.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /*************************************************************************/
  2. /* rasterizer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. /**
  33. @author Juan Linietsky <[email protected]>
  34. */
  35. #include "camera_matrix.h"
  36. #include "map.h"
  37. #include "self_list.h"
  38. #include "servers/visual_server.h"
  39. class Rasterizer {
  40. protected:
  41. typedef void (*CanvasItemDrawViewportFunc)(VisualServer *owner, void *ud, const Rect2 &p_rect);
  42. RID create_default_material();
  43. RID create_overdraw_debug_material();
  44. /* Fixed Material Shader API */
  45. union FixedMaterialShaderKey {
  46. struct {
  47. uint16_t texcoord_mask;
  48. uint8_t texture_mask;
  49. uint8_t light_shader : 2;
  50. bool use_alpha : 1;
  51. bool use_color_array : 1;
  52. bool use_pointsize : 1;
  53. bool discard_alpha : 1;
  54. bool use_xy_normalmap : 1;
  55. bool valid : 1;
  56. };
  57. uint32_t key;
  58. _FORCE_INLINE_ bool operator<(const FixedMaterialShaderKey &p_key) const { return key < p_key.key; }
  59. };
  60. struct FixedMaterialShader {
  61. int refcount;
  62. RID shader;
  63. };
  64. Map<FixedMaterialShaderKey, FixedMaterialShader> fixed_material_shaders;
  65. RID _create_shader(const FixedMaterialShaderKey &p_key);
  66. void _free_shader(const FixedMaterialShaderKey &p_key);
  67. struct FixedMaterial {
  68. RID self;
  69. bool use_alpha;
  70. bool use_color_array;
  71. bool discard_alpha;
  72. bool use_pointsize;
  73. bool use_xy_normalmap;
  74. float point_size;
  75. Transform uv_xform;
  76. VS::FixedMaterialLightShader light_shader;
  77. RID texture[VS::FIXED_MATERIAL_PARAM_MAX];
  78. Variant param[VS::FIXED_MATERIAL_PARAM_MAX];
  79. VS::FixedMaterialTexCoordMode texture_tc[VS::FIXED_MATERIAL_PARAM_MAX];
  80. SelfList<FixedMaterial> dirty_list;
  81. FixedMaterialShaderKey current_key;
  82. _FORCE_INLINE_ FixedMaterialShaderKey get_key() const {
  83. FixedMaterialShaderKey k;
  84. k.key = 0;
  85. k.use_alpha = use_alpha;
  86. k.use_color_array = use_color_array;
  87. k.use_pointsize = use_pointsize;
  88. k.use_xy_normalmap = use_xy_normalmap;
  89. k.discard_alpha = discard_alpha;
  90. k.light_shader = light_shader;
  91. k.valid = true;
  92. for (int i = 0; i < VS::FIXED_MATERIAL_PARAM_MAX; i++) {
  93. if (texture[i].is_valid()) {
  94. //print_line("valid: "+itos(i));
  95. k.texture_mask |= (1 << i);
  96. k.texcoord_mask |= (texture_tc[i]) << (i * 2);
  97. }
  98. }
  99. return k;
  100. }
  101. FixedMaterial() :
  102. dirty_list(this) {
  103. use_alpha = false;
  104. use_color_array = false;
  105. use_pointsize = false;
  106. discard_alpha = false;
  107. use_xy_normalmap = false;
  108. point_size = 1.0;
  109. light_shader = VS::FIXED_MATERIAL_LIGHT_SHADER_LAMBERT;
  110. for (int i = 0; i < VS::FIXED_MATERIAL_PARAM_MAX; i++) {
  111. texture_tc[i] = VS::FIXED_MATERIAL_TEXCOORD_UV;
  112. }
  113. param[VS::FIXED_MATERIAL_PARAM_DIFFUSE] = Color(1, 1, 1);
  114. param[VS::FIXED_MATERIAL_PARAM_DETAIL] = 1.0;
  115. param[VS::FIXED_MATERIAL_PARAM_EMISSION] = Color(0, 0, 0);
  116. param[VS::FIXED_MATERIAL_PARAM_GLOW] = 0;
  117. param[VS::FIXED_MATERIAL_PARAM_SHADE_PARAM] = 0;
  118. param[VS::FIXED_MATERIAL_PARAM_SPECULAR] = Color(0.0, 0.0, 0.0);
  119. param[VS::FIXED_MATERIAL_PARAM_SPECULAR_EXP] = 40;
  120. param[VS::FIXED_MATERIAL_PARAM_NORMAL] = 1;
  121. current_key.key = 0;
  122. }
  123. };
  124. StringName _fixed_material_param_names[VS::FIXED_MATERIAL_PARAM_MAX];
  125. StringName _fixed_material_tex_names[VS::FIXED_MATERIAL_PARAM_MAX];
  126. StringName _fixed_material_uv_xform_name;
  127. StringName _fixed_material_point_size_name;
  128. Map<RID, FixedMaterial *> fixed_materials;
  129. SelfList<FixedMaterial>::List fixed_material_dirty_list;
  130. protected:
  131. void _update_fixed_materials();
  132. void _free_fixed_material(const RID &p_material);
  133. public:
  134. enum ShadowFilterTechnique {
  135. SHADOW_FILTER_NONE,
  136. SHADOW_FILTER_PCF5,
  137. SHADOW_FILTER_PCF13,
  138. SHADOW_FILTER_ESM,
  139. SHADOW_FILTER_VSM,
  140. };
  141. /* TEXTURE API */
  142. virtual RID texture_create() = 0;
  143. RID texture_create_from_image(const Image &p_image, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT); // helper
  144. virtual void texture_allocate(RID p_texture, int p_width, int p_height, Image::Format p_format, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT) = 0;
  145. virtual void texture_set_data(RID p_texture, const Image &p_image, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) = 0;
  146. virtual Image texture_get_data(RID p_texture, VS::CubeMapSide p_cube_side = VS::CUBEMAP_LEFT) const = 0;
  147. virtual void texture_set_flags(RID p_texture, uint32_t p_flags) = 0;
  148. virtual uint32_t texture_get_flags(RID p_texture) const = 0;
  149. virtual Image::Format texture_get_format(RID p_texture) const = 0;
  150. virtual uint32_t texture_get_width(RID p_texture) const = 0;
  151. virtual uint32_t texture_get_height(RID p_texture) const = 0;
  152. virtual bool texture_has_alpha(RID p_texture) const = 0;
  153. virtual void texture_set_size_override(RID p_texture, int p_width, int p_height) = 0;
  154. virtual void texture_set_reload_hook(RID p_texture, ObjectID p_owner, const StringName &p_function) const = 0;
  155. virtual void texture_set_path(RID p_texture, const String &p_path) = 0;
  156. virtual String texture_get_path(RID p_texture) const = 0;
  157. virtual void texture_debug_usage(List<VS::TextureInfo> *r_info) = 0;
  158. virtual void texture_set_shrink_all_x2_on_set_data(bool p_enable) = 0;
  159. /* SHADER API */
  160. virtual RID shader_create(VS::ShaderMode p_mode = VS::SHADER_MATERIAL) = 0;
  161. virtual void shader_set_mode(RID p_shader, VS::ShaderMode p_mode) = 0;
  162. virtual VS::ShaderMode shader_get_mode(RID p_shader) const = 0;
  163. virtual void shader_set_code(RID p_shader, const String &p_vertex, const String &p_fragment, const String &p_light, int p_vertex_ofs = 0, int p_fragment_ofs = 0, int p_light_ofs = 0) = 0;
  164. virtual String shader_get_fragment_code(RID p_shader) const = 0;
  165. virtual String shader_get_vertex_code(RID p_shader) const = 0;
  166. virtual String shader_get_light_code(RID p_shader) const = 0;
  167. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const = 0;
  168. virtual void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) = 0;
  169. virtual RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const = 0;
  170. virtual Variant shader_get_default_param(RID p_shader, const StringName &p_name) = 0;
  171. /* COMMON MATERIAL API */
  172. virtual RID material_create() = 0;
  173. virtual void material_set_shader(RID p_shader_material, RID p_shader) = 0;
  174. virtual RID material_get_shader(RID p_shader_material) const = 0;
  175. virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) = 0;
  176. virtual Variant material_get_param(RID p_material, const StringName &p_param) const = 0;
  177. virtual void material_set_flag(RID p_material, VS::MaterialFlag p_flag, bool p_enabled) = 0;
  178. virtual bool material_get_flag(RID p_material, VS::MaterialFlag p_flag) const = 0;
  179. virtual void material_set_depth_draw_mode(RID p_material, VS::MaterialDepthDrawMode p_mode) = 0;
  180. virtual VS::MaterialDepthDrawMode material_get_depth_draw_mode(RID p_material) const = 0;
  181. virtual void material_set_blend_mode(RID p_material, VS::MaterialBlendMode p_mode) = 0;
  182. virtual VS::MaterialBlendMode material_get_blend_mode(RID p_material) const = 0;
  183. virtual void material_set_line_width(RID p_material, float p_line_width) = 0;
  184. virtual float material_get_line_width(RID p_material) const = 0;
  185. /* FIXED MATERIAL */
  186. virtual RID fixed_material_create();
  187. virtual void fixed_material_set_flag(RID p_material, VS::FixedMaterialFlags p_flag, bool p_enabled);
  188. virtual bool fixed_material_get_flag(RID p_material, VS::FixedMaterialFlags p_flag) const;
  189. virtual void fixed_material_set_parameter(RID p_material, VS::FixedMaterialParam p_parameter, const Variant &p_value);
  190. virtual Variant fixed_material_get_parameter(RID p_material, VS::FixedMaterialParam p_parameter) const;
  191. virtual void fixed_material_set_texture(RID p_material, VS::FixedMaterialParam p_parameter, RID p_texture);
  192. virtual RID fixed_material_get_texture(RID p_material, VS::FixedMaterialParam p_parameter) const;
  193. virtual void fixed_material_set_texcoord_mode(RID p_material, VS::FixedMaterialParam p_parameter, VS::FixedMaterialTexCoordMode p_mode);
  194. virtual VS::FixedMaterialTexCoordMode fixed_material_get_texcoord_mode(RID p_material, VS::FixedMaterialParam p_parameter) const;
  195. virtual void fixed_material_set_uv_transform(RID p_material, const Transform &p_transform);
  196. virtual Transform fixed_material_get_uv_transform(RID p_material) const;
  197. virtual void fixed_material_set_light_shader(RID p_material, VS::FixedMaterialLightShader p_shader);
  198. virtual VS::FixedMaterialLightShader fixed_material_get_light_shader(RID p_material) const;
  199. virtual void fixed_material_set_point_size(RID p_material, float p_size);
  200. virtual float fixed_material_get_point_size(RID p_material) const;
  201. /* MESH API */
  202. virtual RID mesh_create() = 0;
  203. virtual void mesh_add_surface(RID p_mesh, VS::PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes = Array(), bool p_alpha_sort = false) = 0;
  204. virtual Array mesh_get_surface_arrays(RID p_mesh, int p_surface) const = 0;
  205. virtual Array mesh_get_surface_morph_arrays(RID p_mesh, int p_surface) const = 0;
  206. virtual void mesh_add_custom_surface(RID p_mesh, const Variant &p_dat) = 0;
  207. virtual void mesh_set_morph_target_count(RID p_mesh, int p_amount) = 0;
  208. virtual int mesh_get_morph_target_count(RID p_mesh) const = 0;
  209. virtual void mesh_set_morph_target_mode(RID p_mesh, VS::MorphTargetMode p_mode) = 0;
  210. virtual VS::MorphTargetMode mesh_get_morph_target_mode(RID p_mesh) const = 0;
  211. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material, bool p_owned = false) = 0;
  212. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const = 0;
  213. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const = 0;
  214. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const = 0;
  215. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const = 0;
  216. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const = 0;
  217. virtual void mesh_remove_surface(RID p_mesh, int p_index) = 0;
  218. virtual int mesh_get_surface_count(RID p_mesh) const = 0;
  219. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) const = 0;
  220. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
  221. virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;
  222. /* MULTIMESH API */
  223. virtual RID multimesh_create() = 0;
  224. virtual void multimesh_set_instance_count(RID p_multimesh, int p_count) = 0;
  225. virtual int multimesh_get_instance_count(RID p_multimesh) const = 0;
  226. virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh) = 0;
  227. virtual void multimesh_set_aabb(RID p_multimesh, const AABB &p_aabb) = 0;
  228. virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) = 0;
  229. virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) = 0;
  230. virtual RID multimesh_get_mesh(RID p_multimesh) const = 0;
  231. virtual AABB multimesh_get_aabb(RID p_multimesh) const = 0;
  232. virtual Transform multimesh_instance_get_transform(RID p_multimesh, int p_index) const = 0;
  233. virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const = 0;
  234. virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible) = 0;
  235. virtual int multimesh_get_visible_instances(RID p_multimesh) const = 0;
  236. /* BAKED LIGHT */
  237. /* IMMEDIATE API */
  238. virtual RID immediate_create() = 0;
  239. virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID()) = 0;
  240. virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex) = 0;
  241. virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal) = 0;
  242. virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent) = 0;
  243. virtual void immediate_color(RID p_immediate, const Color &p_color) = 0;
  244. virtual void immediate_uv(RID p_immediate, const Vector2 &tex_uv) = 0;
  245. virtual void immediate_uv2(RID p_immediate, const Vector2 &tex_uv) = 0;
  246. virtual void immediate_end(RID p_immediate) = 0;
  247. virtual void immediate_clear(RID p_immediate) = 0;
  248. virtual AABB immediate_get_aabb(RID p_immediate) const = 0;
  249. virtual void immediate_set_material(RID p_immediate, RID p_material) = 0;
  250. virtual RID immediate_get_material(RID p_immediate) const = 0;
  251. /* PARTICLES API */
  252. virtual RID particles_create() = 0;
  253. virtual void particles_set_amount(RID p_particles, int p_amount) = 0;
  254. virtual int particles_get_amount(RID p_particles) const = 0;
  255. virtual void particles_set_emitting(RID p_particles, bool p_emitting) = 0;
  256. virtual bool particles_is_emitting(RID p_particles) const = 0;
  257. virtual void particles_set_visibility_aabb(RID p_particles, const AABB &p_visibility) = 0;
  258. virtual AABB particles_get_visibility_aabb(RID p_particles) const = 0;
  259. virtual void particles_set_emission_half_extents(RID p_particles, const Vector3 &p_half_extents) = 0;
  260. virtual Vector3 particles_get_emission_half_extents(RID p_particles) const = 0;
  261. virtual void particles_set_emission_base_velocity(RID p_particles, const Vector3 &p_base_velocity) = 0;
  262. virtual Vector3 particles_get_emission_base_velocity(RID p_particles) const = 0;
  263. virtual void particles_set_emission_points(RID p_particles, const DVector<Vector3> &p_points) = 0;
  264. virtual DVector<Vector3> particles_get_emission_points(RID p_particles) const = 0;
  265. virtual void particles_set_gravity_normal(RID p_particles, const Vector3 &p_normal) = 0;
  266. virtual Vector3 particles_get_gravity_normal(RID p_particles) const = 0;
  267. virtual void particles_set_variable(RID p_particles, VS::ParticleVariable p_variable, float p_value) = 0;
  268. virtual float particles_get_variable(RID p_particles, VS::ParticleVariable p_variable) const = 0;
  269. virtual void particles_set_randomness(RID p_particles, VS::ParticleVariable p_variable, float p_randomness) = 0;
  270. virtual float particles_get_randomness(RID p_particles, VS::ParticleVariable p_variable) const = 0;
  271. virtual void particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) = 0;
  272. virtual float particles_get_color_phase_pos(RID p_particles, int p_phase) const = 0;
  273. virtual void particles_set_color_phases(RID p_particles, int p_phases) = 0;
  274. virtual int particles_get_color_phases(RID p_particles) const = 0;
  275. virtual void particles_set_color_phase_color(RID p_particles, int p_phase, const Color &p_color) = 0;
  276. virtual Color particles_get_color_phase_color(RID p_particles, int p_phase) const = 0;
  277. virtual void particles_set_attractors(RID p_particles, int p_attractors) = 0;
  278. virtual int particles_get_attractors(RID p_particles) const = 0;
  279. virtual void particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3 &p_pos) = 0;
  280. virtual Vector3 particles_get_attractor_pos(RID p_particles, int p_attractor) const = 0;
  281. virtual void particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) = 0;
  282. virtual float particles_get_attractor_strength(RID p_particles, int p_attractor) const = 0;
  283. virtual void particles_set_material(RID p_particles, RID p_material, bool p_owned = false) = 0;
  284. virtual RID particles_get_material(RID p_particles) const = 0;
  285. virtual AABB particles_get_aabb(RID p_particles) const = 0;
  286. virtual void particles_set_height_from_velocity(RID p_particles, bool p_enable) = 0;
  287. virtual bool particles_has_height_from_velocity(RID p_particles) const = 0;
  288. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable) = 0;
  289. virtual bool particles_is_using_local_coordinates(RID p_particles) const = 0;
  290. /* SKELETON API */
  291. virtual RID skeleton_create() = 0;
  292. virtual void skeleton_resize(RID p_skeleton, int p_bones) = 0;
  293. virtual int skeleton_get_bone_count(RID p_skeleton) const = 0;
  294. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform &p_transform) = 0;
  295. virtual Transform skeleton_bone_get_transform(RID p_skeleton, int p_bone) = 0;
  296. /* LIGHT API */
  297. virtual RID light_create(VS::LightType p_type) = 0;
  298. virtual VS::LightType light_get_type(RID p_light) const = 0;
  299. virtual void light_set_color(RID p_light, VS::LightColor p_type, const Color &p_color) = 0;
  300. virtual Color light_get_color(RID p_light, VS::LightColor p_type) const = 0;
  301. virtual void light_set_shadow(RID p_light, bool p_enabled) = 0;
  302. virtual bool light_has_shadow(RID p_light) const = 0;
  303. virtual void light_set_volumetric(RID p_light, bool p_enabled) = 0;
  304. virtual bool light_is_volumetric(RID p_light) const = 0;
  305. virtual void light_set_projector(RID p_light, RID p_texture) = 0;
  306. virtual RID light_get_projector(RID p_light) const = 0;
  307. virtual void light_set_var(RID p_light, VS::LightParam p_var, float p_value) = 0;
  308. virtual float light_get_var(RID p_light, VS::LightParam p_var) const = 0;
  309. virtual void light_set_operator(RID p_light, VS::LightOp p_op) = 0;
  310. virtual VS::LightOp light_get_operator(RID p_light) const = 0;
  311. virtual void light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) = 0;
  312. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) const = 0;
  313. virtual void light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) = 0;
  314. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) const = 0;
  315. virtual void light_directional_set_shadow_param(RID p_light, VS::LightDirectionalShadowParam p_param, float p_value) = 0;
  316. virtual float light_directional_get_shadow_param(RID p_light, VS::LightDirectionalShadowParam p_param) const = 0;
  317. virtual AABB light_get_aabb(RID p_poly) const = 0;
  318. virtual RID light_instance_create(RID p_light) = 0;
  319. virtual void light_instance_set_transform(RID p_light_instance, const Transform &p_transform) = 0;
  320. enum ShadowType {
  321. SHADOW_NONE,
  322. SHADOW_SIMPLE,
  323. SHADOW_ORTHOGONAL,
  324. SHADOW_DUAL_PARABOLOID,
  325. SHADOW_CUBE,
  326. SHADOW_PSSM, //parallel split shadow map
  327. SHADOW_PSM //perspective shadow map
  328. };
  329. enum ShadowPass {
  330. PASS_DUAL_PARABOLOID_FRONT = 0,
  331. PASS_DUAL_PARABOLOID_BACK = 1,
  332. PASS_CUBE_FRONT = 0,
  333. PASS_CUBE_BACK = 1,
  334. PASS_CUBE_TOP = 2,
  335. PASS_CUBE_BOTTOM = 3,
  336. PASS_CUBE_LEFT = 4,
  337. PASS_CUBE_RIGHT = 5,
  338. };
  339. virtual ShadowType light_instance_get_shadow_type(RID p_light_instance, bool p_far = false) const = 0;
  340. virtual int light_instance_get_shadow_passes(RID p_light_instance) const = 0;
  341. virtual void light_instance_set_shadow_transform(RID p_light_instance, int p_index, const CameraMatrix &p_camera, const Transform &p_transform, float p_split_near = 0, float p_split_far = 0) = 0;
  342. virtual int light_instance_get_shadow_size(RID p_light_instance, int p_index = 0) const = 0;
  343. virtual bool light_instance_get_pssm_shadow_overlap(RID p_light_instance) const = 0;
  344. /* SHADOWS */
  345. virtual void shadow_clear_near() = 0;
  346. virtual bool shadow_allocate_near(RID p_light) = 0; //true on successful alloc
  347. virtual bool shadow_allocate_far(RID p_light) = 0; //true on successful alloc
  348. /* PARTICLES INSTANCE */
  349. virtual RID particles_instance_create(RID p_particles) = 0;
  350. virtual void particles_instance_set_transform(RID p_particles_instance, const Transform &p_transform) = 0;
  351. /* RENDER API */
  352. /* all calls (inside begin/end shadow) are always warranted to be in the following order: */
  353. /* VIEWPORT API */
  354. virtual RID viewport_data_create() = 0;
  355. virtual RID render_target_create() = 0;
  356. virtual void render_target_set_size(RID p_render_target, int p_width, int p_height) = 0;
  357. virtual RID render_target_get_texture(RID p_render_target) const = 0;
  358. virtual bool render_target_renedered_in_frame(RID p_render_target) = 0;
  359. virtual void begin_frame() = 0;
  360. virtual void set_viewport(const VS::ViewportRect &p_viewport) = 0;
  361. virtual void set_render_target(RID p_render_target, bool p_transparent_bg = false, bool p_vflip = false) = 0;
  362. virtual void clear_viewport(const Color &p_color) = 0;
  363. virtual void capture_viewport(Image *r_capture) = 0;
  364. virtual void begin_scene(RID p_viewport_data, RID p_env, VS::ScenarioDebugMode p_debug) = 0;
  365. virtual void begin_shadow_map(RID p_light_instance, int p_shadow_pass) = 0;
  366. virtual void set_camera(const Transform &p_world, const CameraMatrix &p_projection, bool p_ortho_hint) = 0;
  367. virtual void add_light(RID p_light_instance) = 0; ///< all "add_light" calls happen before add_geometry calls
  368. typedef Map<StringName, Variant> ParamOverrideMap;
  369. struct BakedLightData {
  370. VS::BakedLightMode mode;
  371. RID octree_texture;
  372. RID light_texture;
  373. float color_multiplier; //used for both lightmaps and octree
  374. Transform octree_transform;
  375. Map<int, RID> lightmaps;
  376. //cache
  377. float octree_lattice_size;
  378. float octree_lattice_divide;
  379. float texture_multiplier;
  380. float lightmap_multiplier;
  381. int octree_steps;
  382. Vector2 octree_tex_pixel_size;
  383. Vector2 light_tex_pixel_size;
  384. bool realtime_color_enabled;
  385. Color realtime_color;
  386. float realtime_energy;
  387. };
  388. struct InstanceData {
  389. Transform transform;
  390. RID skeleton;
  391. RID material_override;
  392. RID sampled_light;
  393. Vector<RID> materials;
  394. Vector<RID> light_instances;
  395. Vector<float> morph_values;
  396. BakedLightData *baked_light;
  397. VS::ShadowCastingSetting cast_shadows;
  398. Transform *baked_light_octree_xform;
  399. int baked_lightmap_id;
  400. bool mirror : 8;
  401. bool depth_scale : 8;
  402. bool billboard : 8;
  403. bool billboard_y : 8;
  404. bool receive_shadows : 8;
  405. };
  406. virtual void add_mesh(const RID &p_mesh, const InstanceData *p_data) = 0;
  407. virtual void add_multimesh(const RID &p_multimesh, const InstanceData *p_data) = 0;
  408. virtual void add_immediate(const RID &p_immediate, const InstanceData *p_data) = 0;
  409. virtual void add_particles(const RID &p_particle_instance, const InstanceData *p_data) = 0;
  410. virtual void end_scene() = 0;
  411. virtual void end_shadow_map() = 0;
  412. virtual void end_frame() = 0;
  413. virtual void flush_frame(); //not necesary in most cases
  414. /* CANVAS API */
  415. enum CanvasRectFlags {
  416. CANVAS_RECT_REGION = 1,
  417. CANVAS_RECT_TILE = 2,
  418. CANVAS_RECT_FLIP_H = 4,
  419. CANVAS_RECT_FLIP_V = 8,
  420. CANVAS_RECT_TRANSPOSE = 16
  421. };
  422. struct CanvasLight {
  423. bool enabled;
  424. Color color;
  425. Matrix32 xform;
  426. float height;
  427. float energy;
  428. float scale;
  429. int z_min;
  430. int z_max;
  431. int layer_min;
  432. int layer_max;
  433. int item_mask;
  434. int item_shadow_mask;
  435. VS::CanvasLightMode mode;
  436. RID texture;
  437. Vector2 texture_offset;
  438. RID canvas;
  439. RID shadow_buffer;
  440. int shadow_buffer_size;
  441. float shadow_esm_mult;
  442. Color shadow_color;
  443. void *texture_cache; // implementation dependent
  444. Rect2 rect_cache;
  445. Matrix32 xform_cache;
  446. float radius_cache; //used for shadow far plane
  447. CameraMatrix shadow_matrix_cache;
  448. Matrix32 light_shader_xform;
  449. Vector2 light_shader_pos;
  450. CanvasLight *shadows_next_ptr;
  451. CanvasLight *filter_next_ptr;
  452. CanvasLight *next_ptr;
  453. CanvasLight *mask_next_ptr;
  454. CanvasLight() {
  455. enabled = true;
  456. color = Color(1, 1, 1);
  457. shadow_color = Color(0, 0, 0, 0);
  458. height = 0;
  459. z_min = -1024;
  460. z_max = 1024;
  461. layer_min = 0;
  462. layer_max = 0;
  463. item_mask = 1;
  464. scale = 1.0;
  465. energy = 1.0;
  466. item_shadow_mask = -1;
  467. mode = VS::CANVAS_LIGHT_MODE_ADD;
  468. texture_cache = NULL;
  469. next_ptr = NULL;
  470. mask_next_ptr = NULL;
  471. filter_next_ptr = NULL;
  472. shadow_buffer_size = 2048;
  473. shadow_esm_mult = 80;
  474. }
  475. };
  476. struct CanvasItem;
  477. struct CanvasItemMaterial {
  478. RID shader;
  479. Map<StringName, Variant> shader_param;
  480. uint32_t shader_version;
  481. Set<CanvasItem *> owners;
  482. VS::CanvasItemShadingMode shading_mode;
  483. CanvasItemMaterial() {
  484. shading_mode = VS::CANVAS_ITEM_SHADING_NORMAL;
  485. shader_version = 0;
  486. }
  487. };
  488. struct CanvasItem {
  489. struct Command {
  490. enum Type {
  491. TYPE_LINE,
  492. TYPE_RECT,
  493. TYPE_STYLE,
  494. TYPE_PRIMITIVE,
  495. TYPE_POLYGON,
  496. TYPE_POLYGON_PTR,
  497. TYPE_CIRCLE,
  498. TYPE_TRANSFORM,
  499. TYPE_BLEND_MODE,
  500. TYPE_CLIP_IGNORE,
  501. };
  502. Type type;
  503. virtual ~Command() {}
  504. };
  505. struct CommandLine : public Command {
  506. Point2 from, to;
  507. Color color;
  508. float width;
  509. CommandLine() { type = TYPE_LINE; }
  510. };
  511. struct CommandRect : public Command {
  512. Rect2 rect;
  513. RID texture;
  514. Color modulate;
  515. Rect2 source;
  516. uint8_t flags;
  517. CommandRect() {
  518. flags = 0;
  519. type = TYPE_RECT;
  520. }
  521. };
  522. struct CommandStyle : public Command {
  523. Rect2 rect;
  524. Rect2 source;
  525. RID texture;
  526. float margin[4];
  527. bool draw_center;
  528. Color color;
  529. CommandStyle() {
  530. draw_center = true;
  531. type = TYPE_STYLE;
  532. }
  533. };
  534. struct CommandPrimitive : public Command {
  535. Vector<Point2> points;
  536. Vector<Point2> uvs;
  537. Vector<Color> colors;
  538. RID texture;
  539. float width;
  540. CommandPrimitive() {
  541. type = TYPE_PRIMITIVE;
  542. width = 1;
  543. }
  544. };
  545. struct CommandPolygon : public Command {
  546. Vector<int> indices;
  547. Vector<Point2> points;
  548. Vector<Point2> uvs;
  549. Vector<Color> colors;
  550. RID texture;
  551. int count;
  552. CommandPolygon() {
  553. type = TYPE_POLYGON;
  554. count = 0;
  555. }
  556. };
  557. struct CommandPolygonPtr : public Command {
  558. const int *indices;
  559. const Point2 *points;
  560. const Point2 *uvs;
  561. const Color *colors;
  562. RID texture;
  563. int count;
  564. CommandPolygonPtr() {
  565. type = TYPE_POLYGON_PTR;
  566. count = 0;
  567. }
  568. };
  569. struct CommandCircle : public Command {
  570. Point2 pos;
  571. float radius;
  572. Color color;
  573. CommandCircle() { type = TYPE_CIRCLE; }
  574. };
  575. struct CommandTransform : public Command {
  576. Matrix32 xform;
  577. CommandTransform() { type = TYPE_TRANSFORM; }
  578. };
  579. struct CommandBlendMode : public Command {
  580. VS::MaterialBlendMode blend_mode;
  581. CommandBlendMode() {
  582. type = TYPE_BLEND_MODE;
  583. blend_mode = VS::MATERIAL_BLEND_MODE_MIX;
  584. }
  585. };
  586. struct CommandClipIgnore : public Command {
  587. bool ignore;
  588. CommandClipIgnore() {
  589. type = TYPE_CLIP_IGNORE;
  590. ignore = false;
  591. }
  592. };
  593. struct ViewportRender {
  594. VisualServer *owner;
  595. void *udata;
  596. Rect2 rect;
  597. };
  598. Matrix32 xform;
  599. bool clip;
  600. bool visible;
  601. bool ontop;
  602. VS::MaterialBlendMode blend_mode;
  603. int light_mask;
  604. Vector<Command *> commands;
  605. mutable bool custom_rect;
  606. mutable bool rect_dirty;
  607. mutable Rect2 rect;
  608. CanvasItem *next;
  609. CanvasItemMaterial *material;
  610. struct CopyBackBuffer {
  611. Rect2 rect;
  612. Rect2 screen_rect;
  613. bool full;
  614. };
  615. CopyBackBuffer *copy_back_buffer;
  616. float final_opacity;
  617. Matrix32 final_transform;
  618. Rect2 final_clip_rect;
  619. CanvasItem *final_clip_owner;
  620. CanvasItem *material_owner;
  621. ViewportRender *vp_render;
  622. bool distance_field;
  623. bool light_masked;
  624. Rect2 global_rect_cache;
  625. const Rect2 &get_rect() const {
  626. if (custom_rect || !rect_dirty)
  627. return rect;
  628. //must update rect
  629. int s = commands.size();
  630. if (s == 0) {
  631. rect = Rect2();
  632. rect_dirty = false;
  633. return rect;
  634. }
  635. Matrix32 xf;
  636. bool found_xform = false;
  637. bool first = true;
  638. const CanvasItem::Command *const *cmd = &commands[0];
  639. for (int i = 0; i < s; i++) {
  640. const CanvasItem::Command *c = cmd[i];
  641. Rect2 r;
  642. switch (c->type) {
  643. case CanvasItem::Command::TYPE_LINE: {
  644. const CanvasItem::CommandLine *line = static_cast<const CanvasItem::CommandLine *>(c);
  645. r.pos = line->from;
  646. r.expand_to(line->to);
  647. } break;
  648. case CanvasItem::Command::TYPE_RECT: {
  649. const CanvasItem::CommandRect *crect = static_cast<const CanvasItem::CommandRect *>(c);
  650. r = crect->rect;
  651. } break;
  652. case CanvasItem::Command::TYPE_STYLE: {
  653. const CanvasItem::CommandStyle *style = static_cast<const CanvasItem::CommandStyle *>(c);
  654. r = style->rect;
  655. } break;
  656. case CanvasItem::Command::TYPE_PRIMITIVE: {
  657. const CanvasItem::CommandPrimitive *primitive = static_cast<const CanvasItem::CommandPrimitive *>(c);
  658. r.pos = primitive->points[0];
  659. for (int i = 1; i < primitive->points.size(); i++) {
  660. r.expand_to(primitive->points[i]);
  661. }
  662. } break;
  663. case CanvasItem::Command::TYPE_POLYGON: {
  664. const CanvasItem::CommandPolygon *polygon = static_cast<const CanvasItem::CommandPolygon *>(c);
  665. int l = polygon->points.size();
  666. const Point2 *pp = &polygon->points[0];
  667. r.pos = pp[0];
  668. for (int i = 1; i < l; i++) {
  669. r.expand_to(pp[i]);
  670. }
  671. } break;
  672. case CanvasItem::Command::TYPE_POLYGON_PTR: {
  673. const CanvasItem::CommandPolygonPtr *polygon = static_cast<const CanvasItem::CommandPolygonPtr *>(c);
  674. int l = polygon->count;
  675. if (polygon->indices != NULL) {
  676. r.pos = polygon->points[polygon->indices[0]];
  677. for (int i = 1; i < l; i++) {
  678. r.expand_to(polygon->points[polygon->indices[i]]);
  679. }
  680. } else {
  681. r.pos = polygon->points[0];
  682. for (int i = 1; i < l; i++) {
  683. r.expand_to(polygon->points[i]);
  684. }
  685. }
  686. } break;
  687. case CanvasItem::Command::TYPE_CIRCLE: {
  688. const CanvasItem::CommandCircle *circle = static_cast<const CanvasItem::CommandCircle *>(c);
  689. r.pos = Point2(-circle->radius, -circle->radius) + circle->pos;
  690. r.size = Point2(circle->radius * 2.0, circle->radius * 2.0);
  691. } break;
  692. case CanvasItem::Command::TYPE_TRANSFORM: {
  693. const CanvasItem::CommandTransform *transform = static_cast<const CanvasItem::CommandTransform *>(c);
  694. xf = transform->xform;
  695. found_xform = true;
  696. continue;
  697. } break;
  698. case CanvasItem::Command::TYPE_BLEND_MODE: {
  699. } break;
  700. case CanvasItem::Command::TYPE_CLIP_IGNORE: {
  701. } break;
  702. }
  703. if (found_xform) {
  704. r = xf.xform(r);
  705. found_xform = false;
  706. }
  707. if (first) {
  708. rect = r;
  709. first = false;
  710. } else
  711. rect = rect.merge(r);
  712. }
  713. rect_dirty = false;
  714. return rect;
  715. }
  716. void clear() {
  717. for (int i = 0; i < commands.size(); i++)
  718. memdelete(commands[i]);
  719. commands.clear();
  720. clip = false;
  721. rect_dirty = true;
  722. final_clip_owner = NULL;
  723. material_owner = NULL;
  724. light_masked = false;
  725. }
  726. CanvasItem() {
  727. light_mask = 1;
  728. vp_render = NULL;
  729. next = NULL;
  730. final_clip_owner = NULL;
  731. clip = false;
  732. final_opacity = 1;
  733. blend_mode = VS::MATERIAL_BLEND_MODE_MIX;
  734. visible = true;
  735. rect_dirty = true;
  736. custom_rect = false;
  737. ontop = true;
  738. material_owner = NULL;
  739. material = NULL;
  740. copy_back_buffer = NULL;
  741. distance_field = false;
  742. light_masked = false;
  743. }
  744. virtual ~CanvasItem() {
  745. clear();
  746. if (copy_back_buffer) memdelete(copy_back_buffer);
  747. }
  748. };
  749. CanvasItemDrawViewportFunc draw_viewport_func;
  750. virtual void begin_canvas_bg() = 0;
  751. virtual void canvas_begin() = 0;
  752. virtual void canvas_disable_blending() = 0;
  753. virtual void canvas_set_opacity(float p_opacity) = 0;
  754. virtual void canvas_set_blend_mode(VS::MaterialBlendMode p_mode) = 0;
  755. virtual void canvas_begin_rect(const Matrix32 &p_transform) = 0;
  756. virtual void canvas_set_clip(bool p_clip, const Rect2 &p_rect) = 0;
  757. virtual void canvas_end_rect() = 0;
  758. virtual void canvas_draw_line(const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width) = 0;
  759. virtual void canvas_draw_rect(const Rect2 &p_rect, int p_flags, const Rect2 &p_source, RID p_texture, const Color &p_modulate) = 0;
  760. virtual void canvas_draw_style_box(const Rect2 &p_rect, const Rect2 &p_src_region, RID p_texture, const float *p_margins, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1)) = 0;
  761. virtual void canvas_draw_primitive(const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width) = 0;
  762. virtual void canvas_draw_polygon(int p_vertex_count, const int *p_indices, const Vector2 *p_vertices, const Vector2 *p_uvs, const Color *p_colors, const RID &p_texture, bool p_singlecolor) = 0;
  763. virtual void canvas_set_transform(const Matrix32 &p_transform) = 0;
  764. virtual void canvas_render_items(CanvasItem *p_item_list, int p_z, const Color &p_modulate, CanvasLight *p_light) = 0;
  765. virtual void canvas_debug_viewport_shadows(CanvasLight *p_lights_with_shadow) = 0;
  766. /* LIGHT SHADOW MAPPING */
  767. virtual RID canvas_light_occluder_create() = 0;
  768. virtual void canvas_light_occluder_set_polylines(RID p_occluder, const DVector<Vector2> &p_lines) = 0;
  769. virtual RID canvas_light_shadow_buffer_create(int p_width) = 0;
  770. struct CanvasLightOccluderInstance {
  771. bool enabled;
  772. RID canvas;
  773. RID polygon;
  774. RID polygon_buffer;
  775. Rect2 aabb_cache;
  776. Matrix32 xform;
  777. Matrix32 xform_cache;
  778. int light_mask;
  779. VS::CanvasOccluderPolygonCullMode cull_cache;
  780. CanvasLightOccluderInstance *next;
  781. CanvasLightOccluderInstance() {
  782. enabled = true;
  783. next = NULL;
  784. light_mask = 1;
  785. cull_cache = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  786. }
  787. };
  788. virtual void canvas_light_shadow_buffer_update(RID p_buffer, const Matrix32 &p_light_xform, int p_light_mask, float p_near, float p_far, CanvasLightOccluderInstance *p_occluders, CameraMatrix *p_xform_cache) = 0;
  789. /* ENVIRONMENT */
  790. virtual RID environment_create() = 0;
  791. virtual void environment_set_background(RID p_env, VS::EnvironmentBG p_bg) = 0;
  792. virtual VS::EnvironmentBG environment_get_background(RID p_env) const = 0;
  793. virtual void environment_set_background_param(RID p_env, VS::EnvironmentBGParam p_param, const Variant &p_value) = 0;
  794. virtual Variant environment_get_background_param(RID p_env, VS::EnvironmentBGParam p_param) const = 0;
  795. virtual void environment_set_enable_fx(RID p_env, VS::EnvironmentFx p_effect, bool p_enabled) = 0;
  796. virtual bool environment_is_fx_enabled(RID p_env, VS::EnvironmentFx p_effect) const = 0;
  797. virtual void environment_fx_set_param(RID p_env, VS::EnvironmentFxParam p_param, const Variant &p_value) = 0;
  798. virtual Variant environment_fx_get_param(RID p_env, VS::EnvironmentFxParam p_param) const = 0;
  799. /* SAMPLED LIGHT */
  800. virtual RID sampled_light_dp_create(int p_width, int p_height) = 0;
  801. virtual void sampled_light_dp_update(RID p_sampled_light, const Color *p_data, float p_multiplier) = 0;
  802. /*MISC*/
  803. virtual bool is_texture(const RID &p_rid) const = 0;
  804. virtual bool is_material(const RID &p_rid) const = 0;
  805. virtual bool is_mesh(const RID &p_rid) const = 0;
  806. virtual bool is_multimesh(const RID &p_rid) const = 0;
  807. virtual bool is_immediate(const RID &p_rid) const = 0;
  808. virtual bool is_particles(const RID &p_beam) const = 0;
  809. virtual bool is_light(const RID &p_rid) const = 0;
  810. virtual bool is_light_instance(const RID &p_rid) const = 0;
  811. virtual bool is_particles_instance(const RID &p_rid) const = 0;
  812. virtual bool is_skeleton(const RID &p_rid) const = 0;
  813. virtual bool is_environment(const RID &p_rid) const = 0;
  814. virtual bool is_shader(const RID &p_rid) const = 0;
  815. virtual bool is_canvas_light_occluder(const RID &p_rid) const = 0;
  816. virtual void set_time_scale(float p_scale) = 0;
  817. virtual void free(const RID &p_rid) = 0;
  818. virtual void init() = 0;
  819. virtual void finish() = 0;
  820. virtual bool needs_to_draw_next_frame() const = 0;
  821. virtual void reload_vram() {}
  822. virtual bool has_feature(VS::Features p_feature) const = 0;
  823. virtual void restore_framebuffer() = 0;
  824. virtual int get_render_info(VS::RenderInfo p_info) = 0;
  825. virtual void set_force_16_bits_fbo(bool p_force) {}
  826. Rasterizer();
  827. virtual ~Rasterizer() {}
  828. };
  829. #endif