rasterizer_flash.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*************************************************/
  2. /* rasterizer_gles2.h */
  3. /*************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /*************************************************/
  7. /* Source code within this file is: */
  8. /* (c) 2007-2010 Juan Linietsky, Ariel Manzur */
  9. /* All Rights Reserved. */
  10. /*************************************************/
  11. #ifndef RASTERIZER_FLASH_H
  12. #define RASTERIZER_FLASH_H
  13. #include "servers/visual/rasterizer.h"
  14. #include "servers/visual/shader_language.h"
  15. #include "image.h"
  16. #include "rid.h"
  17. #include "servers/visual_server.h"
  18. #include "list.h"
  19. #include "map.h"
  20. #include "camera_matrix.h"
  21. #include "sort.h"
  22. #include "self_list.h"
  23. #include "platform_config.h"
  24. #include "servers/visual/particle_system_sw.h"
  25. /**
  26. @author Juan Linietsky <reduzio@gmail.com>
  27. */
  28. class RasterizerFlash : public Rasterizer {
  29. enum {
  30. MAX_SCENE_LIGHTS=2048,
  31. LIGHT_SPOT_BIT=0x80,
  32. DEFAULT_SKINNED_BUFFER_SIZE = 1024 * 1024, // 10k vertices
  33. MAX_HW_LIGHTS = 1,
  34. };
  35. uint8_t *skinned_buffer;
  36. int skinned_buffer_size;
  37. bool pvr_supported;
  38. bool s3tc_supported;
  39. bool etc_supported;
  40. bool npo2_textures_available;
  41. struct Texture {
  42. uint32_t flags;
  43. int width,height;
  44. int alloc_width, alloc_height;
  45. Image::Format format;
  46. int mipmaps;
  47. int data_size; //original data size, useful for retrieving back
  48. bool has_alpha;
  49. bool format_has_alpha;
  50. bool compressed;
  51. bool disallow_mipmaps;
  52. int total_data_size;
  53. Image image[6];
  54. bool active;
  55. bool gen_mipmap;
  56. Texture() {
  57. flags=width=height=0;
  58. data_size=0;
  59. format=Image::FORMAT_GRAYSCALE;
  60. format_has_alpha=false;
  61. has_alpha=false;
  62. active=false;
  63. disallow_mipmaps=false;
  64. gen_mipmap=true;
  65. compressed=false;
  66. total_data_size=0;
  67. }
  68. ~Texture() {
  69. }
  70. };
  71. mutable RID_Owner<Texture> texture_owner;
  72. struct Shader {
  73. String vertex_code;
  74. String fragment_code;
  75. int vertex_line;
  76. int fragment_line;
  77. VS::ShaderMode mode;
  78. uint32_t custom_code_id;
  79. uint32_t version;
  80. bool valid;
  81. bool has_alpha;
  82. Map<StringName,ShaderLanguage::Uniform> uniforms;
  83. SelfList<Shader> dirty_list;
  84. Shader() : dirty_list(this) {
  85. valid=false;
  86. custom_code_id=0;
  87. has_alpha=false;
  88. version=1;
  89. vertex_line=0;
  90. fragment_line=0;
  91. }
  92. };
  93. mutable RID_Owner<Shader> shader_owner;
  94. mutable SelfList<Shader>::List _shader_dirty_list;
  95. _FORCE_INLINE_ void _shader_make_dirty(Shader* p_shader);
  96. void _update_shader( Shader* p_shader) const;
  97. struct Material {
  98. bool flags[VS::MATERIAL_FLAG_MAX];
  99. bool hints[VS::MATERIAL_HINT_MAX];
  100. VS::MaterialShadeModel shade_model;
  101. VS::MaterialBlendMode blend_mode;
  102. float line_width;
  103. bool has_alpha;
  104. mutable uint32_t shader_version;
  105. RID shader; // shader material
  106. Shader *shader_cache;
  107. struct UniformData {
  108. bool istexture;
  109. Variant value;
  110. int index;
  111. };
  112. mutable Map<StringName,UniformData> shader_params;
  113. uint64_t last_pass;
  114. Material() {
  115. for(int i=0;i<VS::MATERIAL_FLAG_MAX;i++)
  116. flags[i]=false;
  117. flags[VS::MATERIAL_FLAG_VISIBLE]=true;
  118. for(int i=0;i<VS::MATERIAL_HINT_MAX;i++)
  119. hints[i]=false;
  120. line_width=1;
  121. has_alpha=false;
  122. blend_mode=VS::MATERIAL_BLEND_MODE_MIX;
  123. last_pass = 0;
  124. shader_version=0;
  125. shader_cache=NULL;
  126. }
  127. };
  128. _FORCE_INLINE_ void _update_material_shader_params(Material *p_material) const;
  129. mutable RID_Owner<Material> material_owner;
  130. struct Geometry {
  131. enum Type {
  132. GEOMETRY_INVALID,
  133. GEOMETRY_SURFACE,
  134. GEOMETRY_POLY,
  135. GEOMETRY_PARTICLES,
  136. GEOMETRY_MULTISURFACE,
  137. };
  138. Type type;
  139. RID material;
  140. bool has_alpha;
  141. bool material_owned;
  142. Geometry() { has_alpha=false; material_owned = false; }
  143. virtual ~Geometry() {};
  144. };
  145. struct GeometryOwner {
  146. virtual ~GeometryOwner() {}
  147. };
  148. class Mesh;
  149. struct Surface : public Geometry {
  150. struct ArrayData {
  151. uint32_t ofs,size,datatype,count;
  152. bool normalize;
  153. bool bind;
  154. ArrayData() { ofs=0; size=0; count=0; datatype=0; normalize=0; bind=false;}
  155. };
  156. Mesh *mesh;
  157. Array data;
  158. Array morph_data;
  159. ArrayData array[VS::ARRAY_MAX];
  160. // no support for the above, array in localmem.
  161. uint8_t *array_local;
  162. uint8_t *index_array_local;
  163. bool packed;
  164. struct MorphTarget {
  165. uint32_t configured_format;
  166. uint8_t *array;
  167. };
  168. MorphTarget* morph_targets_local;
  169. int morph_target_count;
  170. AABB aabb;
  171. int array_len;
  172. int index_array_len;
  173. int max_bone;
  174. float vertex_scale;
  175. float uv_scale;
  176. float uv2_scale;
  177. VS::PrimitiveType primitive;
  178. uint32_t format;
  179. uint32_t configured_format;
  180. int stride;
  181. int local_stride;
  182. uint32_t morph_format;
  183. bool active;
  184. Point2 uv_min;
  185. Point2 uv_max;
  186. Surface() {
  187. array_len=0;
  188. local_stride=0;
  189. morph_format=0;
  190. type=GEOMETRY_SURFACE;
  191. primitive=VS::PRIMITIVE_POINTS;
  192. index_array_len=0;
  193. vertex_scale=1.0;
  194. uv_scale=1.0;
  195. uv2_scale=1.0;
  196. format=0;
  197. stride=0;
  198. morph_targets_local=0;
  199. morph_target_count=0;
  200. array_local = index_array_local = 0;
  201. active=false;
  202. packed=false;
  203. }
  204. ~Surface() {
  205. }
  206. };
  207. struct Mesh {
  208. bool active;
  209. Vector<Surface*> surfaces;
  210. int morph_target_count;
  211. VS::MorphTargetMode morph_target_mode;
  212. mutable uint64_t last_pass;
  213. Mesh() {
  214. morph_target_mode=VS::MORPH_MODE_NORMALIZED;
  215. morph_target_count=0;
  216. last_pass=0;
  217. active=false;
  218. }
  219. };
  220. mutable RID_Owner<Mesh> mesh_owner;
  221. Error _surface_set_arrays(Surface *p_surface, uint8_t *p_mem,uint8_t *p_index_mem,const Array& p_arrays,bool p_main);
  222. struct MultiMesh;
  223. struct MultiMeshSurface : public Geometry {
  224. Surface *surface;
  225. MultiMeshSurface() { type=GEOMETRY_MULTISURFACE; }
  226. };
  227. struct MultiMesh : public GeometryOwner {
  228. struct Element {
  229. float matrix[16];
  230. uint8_t color[4];
  231. };
  232. AABB aabb;
  233. RID mesh;
  234. int visible;
  235. //IDirect3DVertexBuffer9* instance_buffer;
  236. Vector<Element> elements;
  237. Vector<MultiMeshSurface> cache_surfaces;
  238. mutable uint64_t last_pass;
  239. MultiMesh() {
  240. last_pass=0;
  241. visible = -1;
  242. }
  243. };
  244. mutable RID_Owner<MultiMesh> multimesh_owner;
  245. struct Particles : public Geometry {
  246. ParticleSystemSW data; // software particle system
  247. Particles() {
  248. type=GEOMETRY_PARTICLES;
  249. }
  250. };
  251. mutable RID_Owner<Particles> particles_owner;
  252. struct ParticlesInstance : public GeometryOwner {
  253. RID particles;
  254. ParticleSystemProcessSW particles_process;
  255. Transform transform;
  256. ParticlesInstance() { }
  257. };
  258. mutable RID_Owner<ParticlesInstance> particles_instance_owner;
  259. ParticleSystemDrawInfoSW particle_draw_info;
  260. struct Skeleton {
  261. Vector<Transform> bones;
  262. };
  263. mutable RID_Owner<Skeleton> skeleton_owner;
  264. struct Light {
  265. VS::LightType type;
  266. float vars[VS::LIGHT_PARAM_MAX];
  267. Color colors[3];
  268. bool shadow_enabled;
  269. RID projector;
  270. bool volumetric_enabled;
  271. Color volumetric_color;
  272. Light() {
  273. vars[VS::LIGHT_PARAM_SPOT_ATTENUATION]=1;
  274. vars[VS::LIGHT_PARAM_SPOT_ANGLE]=45;
  275. vars[VS::LIGHT_PARAM_ATTENUATION]=1.0;
  276. vars[VS::LIGHT_PARAM_ENERGY]=1.0;
  277. vars[VS::LIGHT_PARAM_RADIUS]=1.0;
  278. colors[VS::LIGHT_COLOR_AMBIENT]=Color(0,0,0);
  279. colors[VS::LIGHT_COLOR_DIFFUSE]=Color(1,1,1);
  280. colors[VS::LIGHT_COLOR_SPECULAR]=Color(1,1,1);
  281. shadow_enabled=false;
  282. volumetric_enabled=false;
  283. }
  284. };
  285. struct Environment {
  286. VS::EnvironmentBG bg_mode;
  287. Variant bg_param[VS::ENV_BG_PARAM_MAX];
  288. bool fx_enabled[VS::ENV_FX_MAX];
  289. Variant fx_param[VS::ENV_FX_PARAM_MAX];
  290. Environment() {
  291. bg_mode=VS::ENV_BG_DEFAULT_COLOR;
  292. bg_param[VS::ENV_BG_PARAM_COLOR]=Color(0,0,0);
  293. bg_param[VS::ENV_BG_PARAM_TEXTURE]=RID();
  294. bg_param[VS::ENV_BG_PARAM_CUBEMAP]=RID();
  295. bg_param[VS::ENV_BG_PARAM_ENERGY]=1.0;
  296. for(int i=0;i<VS::ENV_FX_MAX;i++)
  297. fx_enabled[i]=false;
  298. fx_param[VS::ENV_FX_PARAM_GLOW_BLUR_PASSES]=1;
  299. fx_param[VS::ENV_FX_PARAM_GLOW_BLOOM]=0.0;
  300. fx_param[VS::ENV_FX_PARAM_GLOW_BLOOM_TRESHOLD]=0.5;
  301. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_PASSES]=1;
  302. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_BEGIN]=100.0;
  303. fx_param[VS::ENV_FX_PARAM_DOF_BLUR_RANGE]=10.0;
  304. fx_param[VS::ENV_FX_PARAM_HDR_EXPOSURE]=0.4;
  305. fx_param[VS::ENV_FX_PARAM_HDR_SCALAR]=1.0;
  306. fx_param[VS::ENV_FX_PARAM_HDR_GLOW_TRESHOLD]=0.95;
  307. fx_param[VS::ENV_FX_PARAM_HDR_GLOW_SCALE]=0.2;
  308. fx_param[VS::ENV_FX_PARAM_HDR_MIN_LUMINANCE]=0.4;
  309. fx_param[VS::ENV_FX_PARAM_HDR_MAX_LUMINANCE]=8.0;
  310. fx_param[VS::ENV_FX_PARAM_HDR_EXPOSURE_ADJUST_SPEED]=0.5;
  311. fx_param[VS::ENV_FX_PARAM_FOG_BEGIN]=100.0;
  312. fx_param[VS::ENV_FX_PARAM_FOG_ATTENUATION]=1.0;
  313. fx_param[VS::ENV_FX_PARAM_FOG_BEGIN_COLOR]=Color(0,0,0);
  314. fx_param[VS::ENV_FX_PARAM_FOG_END_COLOR]=Color(0,0,0);
  315. fx_param[VS::ENV_FX_PARAM_FOG_BG]=true;
  316. fx_param[VS::ENV_FX_PARAM_BCS_BRIGHTNESS]=1.0;
  317. fx_param[VS::ENV_FX_PARAM_BCS_CONTRAST]=1.0;
  318. fx_param[VS::ENV_FX_PARAM_BCS_SATURATION]=1.0;
  319. fx_param[VS::ENV_FX_PARAM_SRGB_CONVERT]=1.0;
  320. }
  321. };
  322. mutable RID_Owner<Environment> environment_owner;
  323. struct ShadowBuffer;
  324. struct LightInstance {
  325. struct SplitInfo {
  326. CameraMatrix camera;
  327. Transform transform;
  328. float near;
  329. float far;
  330. };
  331. RID light;
  332. Light *base;
  333. Transform transform;
  334. CameraMatrix projection;
  335. Transform custom_transform;
  336. CameraMatrix custom_projection;
  337. Vector3 light_vector;
  338. Vector3 spot_vector;
  339. float linear_att;
  340. uint64_t shadow_pass;
  341. uint64_t last_pass;
  342. uint16_t sort_key;
  343. Vector<ShadowBuffer*> shadow_buffers;
  344. void clear_shadow_buffers() {
  345. for (int i=0;i<shadow_buffers.size();i++) {
  346. ShadowBuffer *sb=shadow_buffers[i];
  347. ERR_CONTINUE( sb->owner != this );
  348. sb->owner=NULL;
  349. }
  350. shadow_buffers.clear();
  351. }
  352. LightInstance() { shadow_pass=0; last_pass=0; sort_key=0; }
  353. };
  354. mutable RID_Owner<Light> light_owner;
  355. mutable RID_Owner<LightInstance> light_instance_owner;
  356. LightInstance *light_instances[MAX_SCENE_LIGHTS];
  357. LightInstance *directional_lights[4];
  358. int light_instance_count;
  359. int directional_light_count;
  360. int last_light_id;
  361. bool current_depth_test;
  362. bool current_depth_mask;
  363. bool fragment_lighting;
  364. struct RenderList {
  365. enum {
  366. MAX_ELEMENTS=4096,
  367. MAX_LIGHTS=4
  368. };
  369. struct Element {
  370. float depth;
  371. const InstanceData *instance;
  372. const Skeleton *skeleton;
  373. union {
  374. uint16_t lights[MAX_HW_LIGHTS];
  375. uint64_t light_key;
  376. };
  377. uint16_t light_count;
  378. const Geometry *geometry;
  379. const Geometry *geometry_cmp;
  380. const Material *material;
  381. const GeometryOwner *owner;
  382. bool *additive_ptr;
  383. uint8_t light_type;
  384. bool additive;
  385. bool mirror;
  386. };
  387. Element _elements[MAX_ELEMENTS];
  388. Element *elements[MAX_ELEMENTS];
  389. int element_count;
  390. void clear() {
  391. element_count=0;
  392. }
  393. struct SortZ {
  394. _FORCE_INLINE_ bool operator()(const Element* A, const Element* B ) const {
  395. return A->depth > B->depth;
  396. }
  397. };
  398. void sort_z() {
  399. SortArray<Element*,SortZ> sorter;
  400. sorter.sort(elements,element_count);
  401. }
  402. struct SortMat {
  403. _FORCE_INLINE_ bool operator()(const Element* A, const Element* B ) const {
  404. // TODO move to a single uint64 (one comparison)
  405. if (A->material->shader_cache == B->material->shader_cache) {
  406. if (A->material == B->material) {
  407. return A->light_key < B->light_key;
  408. } else {
  409. return (A->material < B->material);
  410. }
  411. } else {
  412. return B->material->shader_cache < B->material->shader_cache;
  413. }
  414. }
  415. };
  416. void sort_mat() {
  417. SortArray<Element*,SortMat> sorter;
  418. sorter.sort(elements,element_count);
  419. }
  420. struct SortMatLight {
  421. _FORCE_INLINE_ bool operator()(const Element* A, const Element* B ) const {
  422. if (A->geometry_cmp == B->geometry_cmp) {
  423. if (A->material == B->material) {
  424. return A->light_key<B->light_key;
  425. } else {
  426. return (A->material < B->material);
  427. }
  428. } else {
  429. return (A->geometry_cmp < B->geometry_cmp);
  430. }
  431. }
  432. };
  433. void sort_mat_light() {
  434. SortArray<Element*,SortMatLight> sorter;
  435. sorter.sort(elements,element_count);
  436. }
  437. struct SortMatLightType {
  438. _FORCE_INLINE_ bool operator()(const Element* A, const Element* B ) const {
  439. if (A->light_type == B->light_type) {
  440. if (A->geometry_cmp == B->geometry_cmp) {
  441. return (A->material < B->material);
  442. } else {
  443. return (A->geometry_cmp < B->geometry_cmp);
  444. }
  445. } else {
  446. return A->light_type < B->light_type;
  447. }
  448. }
  449. };
  450. void sort_mat_light_type() {
  451. SortArray<Element*,SortMatLightType> sorter;
  452. sorter.sort(elements,element_count);
  453. }
  454. _FORCE_INLINE_ Element* add_element() {
  455. if (element_count>MAX_ELEMENTS)
  456. return NULL;
  457. elements[element_count]=&_elements[element_count];
  458. return elements[element_count++];
  459. }
  460. RenderList() {
  461. element_count = 0;
  462. for (int i=0;i<MAX_ELEMENTS;i++)
  463. elements[i]=&_elements[i]; // assign elements
  464. }
  465. };
  466. RenderList opaque_render_list;
  467. RenderList alpha_render_list;
  468. RID default_material;
  469. struct FX {
  470. bool bgcolor_active;
  471. Color bgcolor;
  472. bool skybox_active;
  473. RID skybox_cubemap;
  474. bool antialias_active;
  475. float antialias_tolerance;
  476. bool glow_active;
  477. int glow_passes;
  478. float glow_attenuation;
  479. float glow_bloom;
  480. bool ssao_active;
  481. float ssao_attenuation;
  482. float ssao_radius;
  483. float ssao_max_distance;
  484. float ssao_range_max;
  485. float ssao_range_min;
  486. bool ssao_only;
  487. bool fog_active;
  488. float fog_near;
  489. float fog_far;
  490. float fog_attenuation;
  491. Color fog_color_near;
  492. Color fog_color_far;
  493. bool fog_bg;
  494. bool toon_active;
  495. float toon_treshold;
  496. float toon_soft;
  497. bool edge_active;
  498. Color edge_color;
  499. float edge_size;
  500. FX();
  501. };
  502. mutable RID_Owner<FX> fx_owner;
  503. FX *scene_fx;
  504. CameraMatrix camera_projection;
  505. Transform camera_transform;
  506. Transform camera_transform_inverse;
  507. float camera_z_near;
  508. float camera_z_far;
  509. Size2 camera_vp_size;
  510. Plane camera_plane;
  511. void _add_geometry( const Geometry* p_geometry, const InstanceData *p_instance, const Geometry *p_geometry_cmp, const GeometryOwner *p_owner);
  512. void _render_list_forward(RenderList *p_render_list,bool p_reverse_cull=false,bool p_fragment_light=false);
  513. //void _setup_light(LightInstance* p_instance, int p_idx);
  514. void _setup_lights(const uint16_t * p_lights,int p_light_count);
  515. _FORCE_INLINE_ void _setup_shader_params(const Material *p_material);
  516. bool _setup_material(const Geometry *p_geometry,const Material *p_material,bool p_vertexlit,bool p_no_const_light);
  517. Error _setup_geometry(const Geometry *p_geometry, const Material* p_material,const Skeleton *p_skeleton, const float *p_morphs);
  518. void _render(const Geometry *p_geometry,const Material *p_material, const Skeleton* p_skeleton, const GeometryOwner *p_owner);
  519. /***********/
  520. /* SHADOWS */
  521. /***********/
  522. struct ShadowBuffer {
  523. int size;
  524. int fbo;
  525. int depth;
  526. LightInstance *owner;
  527. void init(int p_size);
  528. ShadowBuffer() { size=0; depth=0; owner=NULL; }
  529. };
  530. Vector<ShadowBuffer> near_shadow_buffers;
  531. Vector<ShadowBuffer> far_shadow_buffers;
  532. LightInstance *shadow;
  533. int shadow_pass;
  534. void _init_shadow_buffers();
  535. float shadow_near_far_split_size_ratio;
  536. /***********/
  537. /* FBOs */
  538. /***********/
  539. struct FrameBuffer {
  540. int fbo;
  541. int color;
  542. int depth;
  543. int width,height;
  544. bool buff16;
  545. bool active;
  546. struct Blur {
  547. int fbo;
  548. int color;
  549. } blur[2];
  550. } framebuffer;
  551. void _update_framebuffer();
  552. void _process_glow_and_bloom();
  553. /*********/
  554. /* FRAME */
  555. /*********/
  556. struct _Rinfo {
  557. int texture_mem;
  558. int vertex_count;
  559. int object_count;
  560. int mat_change_count;
  561. int shader_change_count;
  562. } _rinfo;
  563. int white_tex;
  564. RID canvas_tex;
  565. float canvas_opacity;
  566. _FORCE_INLINE_ Texture* _bind_canvas_texture(const RID& p_texture);
  567. VS::MaterialBlendMode canvas_blend_mode;
  568. int _setup_geometry_vinfo;
  569. bool pack_arrays;
  570. bool keep_copies;
  571. bool cull_front;
  572. _FORCE_INLINE_ void _set_cull(bool p_front,bool p_reverse_cull=false);
  573. Size2 window_size;
  574. VS::ViewportRect viewport;
  575. double last_time;
  576. double time_delta;
  577. uint64_t frame;
  578. uint64_t scene_pass;
  579. void _draw_primitive(int p_points, const Vector3 *p_vertices, const Vector3 *p_normals, const Color* p_colors, const Vector3 *p_uvs,const Plane *p_tangents=NULL,int p_instanced=1);
  580. _FORCE_INLINE_ void _draw_gui_primitive(int p_points, const Vector2 *p_vertices, const Color* p_colors, const Vector2 *p_uvs);
  581. void _draw_textured_quad(const Rect2& p_rect, const Rect2& p_src_region, const Size2& p_tex_size,bool p_h_flip=false, bool p_v_flip=false );
  582. void _draw_quad(const Rect2& p_rect);
  583. public:
  584. /* TEXTURE API */
  585. virtual RID texture_create();
  586. 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,int p_mipmap_count=-1);
  587. virtual void texture_set_data(RID p_texture,const Image& p_image,VS::CubeMapSide p_cube_side=VS::CUBEMAP_LEFT);
  588. virtual Image texture_get_data(RID p_texture,VS::CubeMapSide p_cube_side=VS::CUBEMAP_LEFT) const;
  589. virtual void texture_set_flags(RID p_texture,uint32_t p_flags);
  590. virtual uint32_t texture_get_flags(RID p_texture) const;
  591. virtual Image::Format texture_get_format(RID p_texture) const;
  592. virtual uint32_t texture_get_width(RID p_texture) const;
  593. virtual uint32_t texture_get_height(RID p_texture) const;
  594. virtual bool texture_has_alpha(RID p_texture) const;
  595. virtual void texture_set_size_override(RID p_texture,int p_width, int p_height);
  596. virtual void texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const {};
  597. /* SHADER API */
  598. virtual RID shader_create(VS::ShaderMode p_mode=VS::SHADER_MATERIAL);
  599. virtual void shader_set_mode(RID p_shader,VS::ShaderMode p_mode);
  600. virtual VS::ShaderMode shader_get_mode(RID p_shader) const;
  601. virtual void shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,int p_vertex_ofs=0,int p_fragment_ofs=0);
  602. virtual String shader_get_fragment_code(RID p_shader) const;
  603. virtual String shader_get_vertex_code(RID p_shader) const;
  604. virtual void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const;
  605. /* COMMON MATERIAL API */
  606. virtual RID material_create();
  607. virtual void material_set_shader(RID p_shader_material, RID p_shader);
  608. virtual RID material_get_shader(RID p_shader_material) const;
  609. virtual void material_set_param(RID p_material, const StringName& p_param, const Variant& p_value);
  610. virtual Variant material_get_param(RID p_material, const StringName& p_param) const;
  611. virtual void material_set_flag(RID p_material, VS::MaterialFlag p_flag,bool p_enabled);
  612. virtual bool material_get_flag(RID p_material,VS::MaterialFlag p_flag) const;
  613. virtual void material_set_hint(RID p_material, VS::MaterialHint p_hint,bool p_enabled);
  614. virtual bool material_get_hint(RID p_material,VS::MaterialHint p_hint) const;
  615. virtual void material_set_shade_model(RID p_material, VS::MaterialShadeModel p_model);
  616. virtual VS::MaterialShadeModel material_get_shade_model(RID p_material) const;
  617. virtual void material_set_blend_mode(RID p_material,VS::MaterialBlendMode p_mode);
  618. virtual VS::MaterialBlendMode material_get_blend_mode(RID p_material) const;
  619. virtual void material_set_line_width(RID p_material,float p_line_width);
  620. virtual float material_get_line_width(RID p_material) const;
  621. /* MESH API */
  622. virtual RID mesh_create();
  623. virtual void mesh_add_surface(RID p_mesh,VS::PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes=Array());
  624. virtual Array mesh_get_surface_arrays(RID p_mesh,int p_surface) const;
  625. virtual Array mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const;
  626. virtual void mesh_add_custom_surface(RID p_mesh,const Variant& p_dat);
  627. virtual void mesh_set_morph_target_count(RID p_mesh,int p_amount);
  628. virtual int mesh_get_morph_target_count(RID p_mesh) const;
  629. virtual void mesh_set_morph_target_mode(RID p_mesh,VS::MorphTargetMode p_mode);
  630. virtual VS::MorphTargetMode mesh_get_morph_target_mode(RID p_mesh) const;
  631. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned=false);
  632. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const;
  633. virtual int mesh_surface_get_array_len(RID p_mesh, int p_surface) const;
  634. virtual int mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const;
  635. virtual uint32_t mesh_surface_get_format(RID p_mesh, int p_surface) const;
  636. virtual VS::PrimitiveType mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const;
  637. virtual void mesh_remove_surface(RID p_mesh,int p_index);
  638. virtual int mesh_get_surface_count(RID p_mesh) const;
  639. virtual AABB mesh_get_aabb(RID p_mesh) const;
  640. /* MULTIMESH API */
  641. virtual RID multimesh_create();
  642. virtual void multimesh_set_instance_count(RID p_multimesh,int p_count);
  643. virtual int multimesh_get_instance_count(RID p_multimesh) const;
  644. virtual void multimesh_set_mesh(RID p_multimesh,RID p_mesh);
  645. virtual void multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb);
  646. virtual void multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform);
  647. virtual void multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color);
  648. virtual RID multimesh_get_mesh(RID p_multimesh) const;
  649. virtual AABB multimesh_get_aabb(RID p_multimesh) const;;
  650. virtual Transform multimesh_instance_get_transform(RID p_multimesh,int p_index) const;
  651. virtual Color multimesh_instance_get_color(RID p_multimesh,int p_index) const;
  652. virtual void multimesh_set_visible_instances(RID p_multimesh,int p_visible);
  653. virtual int multimesh_get_visible_instances(RID p_multimesh) const;
  654. /* PARTICLES API */
  655. virtual RID particles_create();
  656. virtual void particles_set_amount(RID p_particles, int p_amount);
  657. virtual int particles_get_amount(RID p_particles) const;
  658. virtual void particles_set_emitting(RID p_particles, bool p_emitting);
  659. virtual bool particles_is_emitting(RID p_particles) const;
  660. virtual void particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility);
  661. virtual AABB particles_get_visibility_aabb(RID p_particles) const;
  662. virtual void particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents);
  663. virtual Vector3 particles_get_emission_half_extents(RID p_particles) const;
  664. virtual void particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity);
  665. virtual Vector3 particles_get_emission_base_velocity(RID p_particles) const;
  666. virtual void particles_set_emission_points(RID p_particles, const DVector<Vector3>& p_points);
  667. virtual DVector<Vector3> particles_get_emission_points(RID p_particles) const;
  668. virtual void particles_set_gravity_normal(RID p_particles, const Vector3& p_normal);
  669. virtual Vector3 particles_get_gravity_normal(RID p_particles) const;
  670. virtual void particles_set_variable(RID p_particles, VS::ParticleVariable p_variable,float p_value);
  671. virtual float particles_get_variable(RID p_particles, VS::ParticleVariable p_variable) const;
  672. virtual void particles_set_randomness(RID p_particles, VS::ParticleVariable p_variable,float p_randomness);
  673. virtual float particles_get_randomness(RID p_particles, VS::ParticleVariable p_variable) const;
  674. virtual void particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos);
  675. virtual float particles_get_color_phase_pos(RID p_particles, int p_phase) const;
  676. virtual void particles_set_color_phases(RID p_particles, int p_phases);
  677. virtual int particles_get_color_phases(RID p_particles) const;
  678. virtual void particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color);
  679. virtual Color particles_get_color_phase_color(RID p_particles, int p_phase) const;
  680. virtual void particles_set_attractors(RID p_particles, int p_attractors);
  681. virtual int particles_get_attractors(RID p_particles) const;
  682. virtual void particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos);
  683. virtual Vector3 particles_get_attractor_pos(RID p_particles,int p_attractor) const;
  684. virtual void particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force);
  685. virtual float particles_get_attractor_strength(RID p_particles,int p_attractor) const;
  686. virtual void particles_set_material(RID p_particles, RID p_material,bool p_owned=false);
  687. virtual RID particles_get_material(RID p_particles) const;
  688. virtual AABB particles_get_aabb(RID p_particles) const;
  689. virtual void particles_set_height_from_velocity(RID p_particles, bool p_enable);
  690. virtual bool particles_has_height_from_velocity(RID p_particles) const;
  691. virtual void particles_set_use_local_coordinates(RID p_particles, bool p_enable);
  692. virtual bool particles_is_using_local_coordinates(RID p_particles) const;
  693. /* SKELETON API */
  694. virtual RID skeleton_create();
  695. virtual void skeleton_resize(RID p_skeleton,int p_bones);
  696. virtual int skeleton_get_bone_count(RID p_skeleton) const;
  697. virtual void skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform);
  698. virtual Transform skeleton_bone_get_transform(RID p_skeleton,int p_bone);
  699. /* LIGHT API */
  700. virtual RID light_create(VS::LightType p_type);
  701. virtual VS::LightType light_get_type(RID p_light) const;
  702. virtual void light_set_color(RID p_light,VS::LightColor p_type, const Color& p_color);
  703. virtual Color light_get_color(RID p_light,VS::LightColor p_type) const;
  704. virtual void light_set_shadow(RID p_light,bool p_enabled);
  705. virtual bool light_has_shadow(RID p_light) const;
  706. virtual void light_set_volumetric(RID p_light,bool p_enabled);
  707. virtual bool light_is_volumetric(RID p_light) const;
  708. virtual void light_set_projector(RID p_light,RID p_texture);
  709. virtual RID light_get_projector(RID p_light) const;
  710. virtual void light_set_var(RID p_light, VS::LightParam p_var, float p_value);
  711. virtual float light_get_var(RID p_light, VS::LightParam p_var) const;
  712. virtual void light_set_operator(RID p_light,VS::LightOp p_op);
  713. virtual VS::LightOp light_get_operator(RID p_light) const;
  714. virtual void light_omni_set_shadow_mode(RID p_light,VS::LightOmniShadowMode p_mode);
  715. virtual VS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) const;
  716. virtual void light_directional_set_shadow_mode(RID p_light,VS::LightDirectionalShadowMode p_mode);
  717. virtual VS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) const;
  718. virtual void light_directional_set_shadow_max_distance(RID p_light,float p_distance);
  719. virtual float light_directional_get_shadow_max_distance(RID p_light) const;
  720. virtual void light_directional_set_pssm_split_weight(RID p_light,float p_weight);
  721. virtual float light_directional_get_pssm_split_weight(RID p_light) const;
  722. virtual void light_directional_set_shadow_param(RID p_light,VS::LightDirectionalShadowParam p_param, float p_value);
  723. virtual float light_directional_get_shadow_param(RID p_light,VS::LightDirectionalShadowParam p_param) const;
  724. virtual AABB light_get_aabb(RID p_poly) const;
  725. virtual RID light_instance_create(RID p_light);
  726. virtual void light_instance_set_transform(RID p_light_instance,const Transform& p_transform);
  727. virtual void light_instance_set_active_hint(RID p_light_instance);
  728. virtual bool light_instance_has_shadow(RID p_light_instance) const;
  729. virtual bool light_instance_assign_shadow(RID p_light_instance);
  730. virtual ShadowType light_instance_get_shadow_type(RID p_light_instance) const;
  731. virtual int light_instance_get_shadow_passes(RID p_light_instance) const;
  732. virtual void light_instance_set_custom_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);
  733. virtual int light_instance_get_shadow_size(RID p_light_instance, int p_index=0) const { return 1; }
  734. virtual ShadowType light_instance_get_shadow_type(RID p_light_instance,bool p_far=false) const { return SHADOW_NONE; };
  735. 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) {};
  736. /* SHADOWS */
  737. virtual void shadow_clear_near() { };
  738. virtual bool shadow_allocate_near(RID p_light) { return false; }; //true on successful alloc
  739. virtual bool shadow_allocate_far(RID p_light) { return false; }; //true on successful alloc
  740. /* PARTICLES INSTANCE */
  741. virtual RID particles_instance_create(RID p_particles);
  742. virtual void particles_instance_set_transform(RID p_particles_instance,const Transform& p_transform);
  743. /* RENDER API */
  744. /* all calls (inside begin/end shadow) are always warranted to be in the following order: */
  745. virtual void begin_frame();
  746. virtual void set_viewport(const VS::ViewportRect& p_viewport);
  747. virtual void clear_viewport(const Color& p_color);
  748. virtual void begin_scene(bool p_copy_bg,RID p_fx=RID(),VS::ScenarioDebugMode p_debug=VS::SCENARIO_DEBUG_DISABLED);
  749. virtual void begin_shadow_map( RID p_light_instance, int p_shadow_pass );
  750. virtual void set_camera(const Transform& p_world,const CameraMatrix& p_projection);
  751. virtual void add_light( RID p_light_instance ); ///< all "add_light" calls happen before add_geometry calls
  752. typedef Map<StringName,Variant> ParamOverrideMap;
  753. virtual void add_mesh( const RID& p_mesh, const InstanceData *p_data);
  754. virtual void add_multimesh( const RID& p_multimesh, const InstanceData *p_data);
  755. virtual void add_particles( const RID& p_particle_instance, const InstanceData *p_data);
  756. virtual void end_scene();
  757. virtual void end_shadow_map();
  758. virtual void end_frame();
  759. /* CANVAS API */
  760. virtual void canvas_begin();
  761. virtual void canvas_set_opacity(float p_opacity);
  762. virtual void canvas_set_blend_mode(VS::MaterialBlendMode p_mode);
  763. virtual void canvas_begin_rect(const Matrix32& p_transform);
  764. virtual void canvas_set_clip(bool p_clip, const Rect2& p_rect);
  765. virtual void canvas_end_rect();
  766. virtual void canvas_draw_line(const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width);
  767. virtual void canvas_draw_rect(const Rect2& p_rect, int p_flags, const Rect2& p_source,RID p_texture,const Color& p_modulate);
  768. virtual void canvas_draw_style_box(const Rect2& p_rect, RID p_texture,const float *p_margins, bool p_draw_center=true,const Color& p_modulate=Color(1,1,1));
  769. 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);
  770. 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);
  771. virtual void canvas_set_transform(const Matrix32& p_transform);
  772. /* ENVIRONMENT */
  773. virtual RID environment_create();
  774. virtual void environment_set_background(RID p_env,VS::EnvironmentBG p_bg);
  775. virtual VS::EnvironmentBG environment_get_background(RID p_env) const;
  776. virtual void environment_set_background_param(RID p_env,VS::EnvironmentBGParam p_param, const Variant& p_value);
  777. virtual Variant environment_get_background_param(RID p_env,VS::EnvironmentBGParam p_param) const;
  778. virtual void environment_set_enable_fx(RID p_env,VS::EnvironmentFx p_effect,bool p_enabled);
  779. virtual bool environment_is_fx_enabled(RID p_env,VS::EnvironmentFx p_effect) const;
  780. virtual void environment_fx_set_param(RID p_env,VS::EnvironmentFxParam p_param,const Variant& p_value);
  781. virtual Variant environment_fx_get_param(RID p_env,VS::EnvironmentFxParam p_param) const;
  782. /* FX */
  783. virtual RID fx_create();
  784. virtual void fx_get_effects(RID p_fx,List<String> *p_effects) const;
  785. virtual void fx_set_active(RID p_fx,const String& p_effect, bool p_active);
  786. virtual bool fx_is_active(RID p_fx,const String& p_effect) const;
  787. virtual void fx_get_effect_params(RID p_fx,const String& p_effect,List<PropertyInfo> *p_params) const;
  788. virtual Variant fx_get_effect_param(RID p_fx,const String& p_effect,const String& p_param) const;
  789. virtual void fx_set_effect_param(RID p_fx,const String& p_effect, const String& p_param, const Variant& p_pvalue);
  790. /*MISC*/
  791. virtual bool is_texture(const RID& p_rid) const;
  792. virtual bool is_material(const RID& p_rid) const;
  793. virtual bool is_mesh(const RID& p_rid) const;
  794. virtual bool is_multimesh(const RID& p_rid) const;
  795. virtual bool is_particles(const RID &p_beam) const;
  796. virtual bool is_light(const RID& p_rid) const;
  797. virtual bool is_light_instance(const RID& p_rid) const;
  798. virtual bool is_particles_instance(const RID& p_rid) const;
  799. virtual bool is_skeleton(const RID& p_rid) const;
  800. virtual bool is_environment(const RID& p_rid) const;
  801. virtual bool is_fx(const RID& p_rid) const;
  802. virtual bool is_shader(const RID& p_rid) const;
  803. virtual void free(const RID& p_rid);
  804. virtual void init();
  805. virtual void finish();
  806. virtual int get_render_info(VS::RenderInfo p_info);
  807. virtual void flush_frame(); //not necesary in most cases
  808. RasterizerFlash(bool p_compress_arrays=false,bool p_keep_ram_copy=true,bool p_default_fragment_lighting=true);
  809. virtual ~RasterizerFlash();
  810. };
  811. #endif