rasterizer_storage_opengl.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /*************************************************************************/
  2. /* rasterizer_storage_opengl.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_STORAGE_OPENGL_H
  31. #define RASTERIZER_STORAGE_OPENGL_H
  32. #include "drivers/opengl/rasterizer_platforms.h"
  33. #ifdef OPENGL_BACKEND_ENABLED
  34. #include "core/templates/local_vector.h"
  35. #include "core/templates/rid_owner.h"
  36. #include "core/templates/self_list.h"
  37. #include "drivers/opengl/rasterizer_asserts.h"
  38. #include "servers/rendering/renderer_compositor.h"
  39. #include "servers/rendering/renderer_storage.h"
  40. #include "servers/rendering/shader_language.h"
  41. #include "shader_compiler_opengl.h"
  42. #include "shader_opengl.h"
  43. #include "shaders/copy.glsl.gen.h"
  44. #include "shaders/cubemap_filter.glsl.gen.h"
  45. class RasterizerCanvasOpenGL;
  46. class RasterizerSceneOpenGL;
  47. class RasterizerStorageOpenGL : public RendererStorage {
  48. friend class RasterizerOpenGL;
  49. Thread::ID _main_thread_id = 0;
  50. bool _is_main_thread();
  51. public:
  52. RasterizerCanvasOpenGL *canvas;
  53. RasterizerSceneOpenGL *scene;
  54. static GLuint system_fbo;
  55. struct Config {
  56. bool shrink_textures_x2;
  57. bool use_fast_texture_filter;
  58. bool use_skeleton_software;
  59. int max_vertex_texture_image_units;
  60. int max_texture_image_units;
  61. int max_texture_size;
  62. // TODO implement wireframe in OpenGL
  63. // bool generate_wireframes;
  64. Set<String> extensions;
  65. bool float_texture_supported;
  66. bool s3tc_supported;
  67. bool etc1_supported;
  68. bool pvrtc_supported;
  69. bool rgtc_supported;
  70. bool bptc_supported;
  71. bool keep_original_textures;
  72. bool force_vertex_shading;
  73. bool use_rgba_2d_shadows;
  74. bool use_rgba_3d_shadows;
  75. bool support_32_bits_indices;
  76. bool support_write_depth;
  77. bool support_half_float_vertices;
  78. bool support_npot_repeat_mipmap;
  79. bool support_depth_texture;
  80. bool support_depth_cubemaps;
  81. bool support_shadow_cubemaps;
  82. bool multisample_supported;
  83. bool render_to_mipmap_supported;
  84. GLuint depth_internalformat;
  85. GLuint depth_type;
  86. GLuint depth_buffer_internalformat;
  87. // in some cases the legacy render didn't orphan. We will mark these
  88. // so the user can switch orphaning off for them.
  89. bool should_orphan;
  90. } config;
  91. struct Resources {
  92. GLuint white_tex;
  93. GLuint black_tex;
  94. GLuint normal_tex;
  95. GLuint aniso_tex;
  96. GLuint mipmap_blur_fbo;
  97. GLuint mipmap_blur_color;
  98. GLuint radical_inverse_vdc_cache_tex;
  99. bool use_rgba_2d_shadows;
  100. GLuint quadie;
  101. size_t skeleton_transform_buffer_size;
  102. GLuint skeleton_transform_buffer;
  103. LocalVector<float> skeleton_transform_cpu_buffer;
  104. } resources;
  105. mutable struct Shaders {
  106. ShaderCompilerOpenGL compiler;
  107. CopyShaderOpenGL copy;
  108. CubemapFilterShaderOpenGL cubemap_filter;
  109. ShaderCompilerOpenGL::IdentifierActions actions_canvas;
  110. ShaderCompilerOpenGL::IdentifierActions actions_scene;
  111. ShaderCompilerOpenGL::IdentifierActions actions_particles;
  112. } shaders;
  113. struct Info {
  114. uint64_t texture_mem;
  115. uint64_t vertex_mem;
  116. struct Render {
  117. uint32_t object_count;
  118. uint32_t draw_call_count;
  119. uint32_t material_switch_count;
  120. uint32_t surface_switch_count;
  121. uint32_t shader_rebind_count;
  122. uint32_t vertices_count;
  123. uint32_t _2d_item_count;
  124. uint32_t _2d_draw_call_count;
  125. void reset() {
  126. object_count = 0;
  127. draw_call_count = 0;
  128. material_switch_count = 0;
  129. surface_switch_count = 0;
  130. shader_rebind_count = 0;
  131. vertices_count = 0;
  132. _2d_item_count = 0;
  133. _2d_draw_call_count = 0;
  134. }
  135. } render, render_final, snap;
  136. Info() :
  137. texture_mem(0),
  138. vertex_mem(0) {
  139. render.reset();
  140. render_final.reset();
  141. }
  142. } info;
  143. void bind_quad_array() const;
  144. /////////////////////////////////////////////////////////////////////////////////////////
  145. //////////////////////////////////DATA///////////////////////////////////////////////////
  146. /////////////////////////////////////////////////////////////////////////////////////////
  147. /*
  148. struct Instantiable {
  149. RID self;
  150. SelfList<InstanceBaseDependency>::List instance_list;
  151. _FORCE_INLINE_ void instance_change_notify(bool p_aabb, bool p_materials) {
  152. SelfList<InstanceBaseDependency> *instances = instance_list.first();
  153. while (instances) {
  154. instances->self()->base_changed(p_aabb, p_materials);
  155. instances = instances->next();
  156. }
  157. }
  158. _FORCE_INLINE_ void instance_remove_deps() {
  159. SelfList<InstanceBaseDependency> *instances = instance_list.first();
  160. while (instances) {
  161. instances->self()->base_removed();
  162. instances = instances->next();
  163. }
  164. }
  165. Instantiable() {}
  166. ~Instantiable() {}
  167. };
  168. struct GeometryOwner : public Instantiable {
  169. };
  170. struct Geometry : public Instantiable {
  171. enum Type {
  172. GEOMETRY_INVALID,
  173. GEOMETRY_SURFACE,
  174. GEOMETRY_IMMEDIATE,
  175. GEOMETRY_MULTISURFACE
  176. };
  177. Type type;
  178. RID material;
  179. uint64_t last_pass;
  180. uint32_t index;
  181. void material_changed_notify() {}
  182. Geometry() {
  183. last_pass = 0;
  184. index = 0;
  185. }
  186. };
  187. */
  188. /////////////////////////////////////////////////////////////////////////////////////////
  189. //////////////////////////////////API////////////////////////////////////////////////////
  190. /////////////////////////////////////////////////////////////////////////////////////////
  191. bool can_create_resources_async() const override;
  192. // TEXTURE API
  193. enum OpenGLTextureFlags {
  194. TEXTURE_FLAG_MIPMAPS = 1, /// Enable automatic mipmap generation - when available
  195. TEXTURE_FLAG_REPEAT = 2, /// Repeat texture (Tiling), otherwise Clamping
  196. TEXTURE_FLAG_FILTER = 4, /// Create texture with linear (or available) filter
  197. TEXTURE_FLAG_ANISOTROPIC_FILTER = 8,
  198. TEXTURE_FLAG_CONVERT_TO_LINEAR = 16,
  199. TEXTURE_FLAG_MIRRORED_REPEAT = 32, /// Repeat texture, with alternate sections mirrored
  200. TEXTURE_FLAG_USED_FOR_STREAMING = 2048,
  201. TEXTURE_FLAGS_DEFAULT = TEXTURE_FLAG_REPEAT | TEXTURE_FLAG_MIPMAPS | TEXTURE_FLAG_FILTER
  202. };
  203. struct RenderTarget;
  204. struct Texture {
  205. RID self;
  206. Texture *proxy;
  207. Set<Texture *> proxy_owners;
  208. String path;
  209. uint32_t flags;
  210. int width, height, depth;
  211. int alloc_width, alloc_height;
  212. Image::Format format;
  213. RenderingDevice::TextureType type;
  214. GLenum target;
  215. GLenum gl_format_cache;
  216. GLenum gl_internal_format_cache;
  217. GLenum gl_type_cache;
  218. int data_size;
  219. int total_data_size;
  220. bool ignore_mipmaps;
  221. bool compressed;
  222. bool srgb;
  223. int mipmaps;
  224. bool resize_to_po2;
  225. bool active;
  226. GLenum tex_id;
  227. uint16_t stored_cube_sides;
  228. RenderTarget *render_target;
  229. Vector<Ref<Image>> images;
  230. bool redraw_if_visible;
  231. RS::TextureDetectCallback detect_3d;
  232. void *detect_3d_ud;
  233. RS::TextureDetectCallback detect_srgb;
  234. void *detect_srgb_ud;
  235. RS::TextureDetectCallback detect_normal;
  236. void *detect_normal_ud;
  237. // some silly opengl shenanigans where
  238. // texture coords start from bottom left, means we need to draw render target textures upside down
  239. // to be compatible with vulkan etc.
  240. bool is_upside_down() const {
  241. if (proxy)
  242. return proxy->is_upside_down();
  243. return render_target != nullptr;
  244. }
  245. Texture() {
  246. create();
  247. }
  248. _ALWAYS_INLINE_ Texture *get_ptr() {
  249. if (proxy) {
  250. return proxy; //->get_ptr(); only one level of indirection, else not inlining possible.
  251. } else {
  252. return this;
  253. }
  254. }
  255. ~Texture() {
  256. destroy();
  257. if (tex_id != 0) {
  258. glDeleteTextures(1, &tex_id);
  259. }
  260. }
  261. void copy_from(const Texture &o) {
  262. proxy = o.proxy;
  263. flags = o.flags;
  264. width = o.width;
  265. height = o.height;
  266. alloc_width = o.alloc_width;
  267. alloc_height = o.alloc_height;
  268. format = o.format;
  269. type = o.type;
  270. target = o.target;
  271. data_size = o.data_size;
  272. total_data_size = o.total_data_size;
  273. ignore_mipmaps = o.ignore_mipmaps;
  274. compressed = o.compressed;
  275. mipmaps = o.mipmaps;
  276. resize_to_po2 = o.resize_to_po2;
  277. active = o.active;
  278. tex_id = o.tex_id;
  279. stored_cube_sides = o.stored_cube_sides;
  280. render_target = o.render_target;
  281. redraw_if_visible = o.redraw_if_visible;
  282. detect_3d = o.detect_3d;
  283. detect_3d_ud = o.detect_3d_ud;
  284. detect_srgb = o.detect_srgb;
  285. detect_srgb_ud = o.detect_srgb_ud;
  286. detect_normal = o.detect_normal;
  287. detect_normal_ud = o.detect_normal_ud;
  288. images.clear();
  289. }
  290. void create() {
  291. proxy = nullptr;
  292. flags = 0;
  293. width = 0;
  294. height = 0;
  295. alloc_width = 0;
  296. alloc_height = 0;
  297. format = Image::FORMAT_L8;
  298. type = RenderingDevice::TEXTURE_TYPE_2D;
  299. target = 0;
  300. data_size = 0;
  301. total_data_size = 0;
  302. ignore_mipmaps = false;
  303. compressed = false;
  304. mipmaps = 0;
  305. resize_to_po2 = false;
  306. active = false;
  307. tex_id = 0;
  308. stored_cube_sides = 0;
  309. render_target = nullptr;
  310. redraw_if_visible = false;
  311. detect_3d = nullptr;
  312. detect_3d_ud = nullptr;
  313. detect_srgb = nullptr;
  314. detect_srgb_ud = nullptr;
  315. detect_normal = nullptr;
  316. detect_normal_ud = nullptr;
  317. }
  318. void destroy() {
  319. images.clear();
  320. for (Set<Texture *>::Element *E = proxy_owners.front(); E; E = E->next()) {
  321. E->get()->proxy = NULL;
  322. }
  323. if (proxy) {
  324. proxy->proxy_owners.erase(this);
  325. }
  326. }
  327. // texture state
  328. void GLSetFilter(GLenum p_target, RS::CanvasItemTextureFilter p_filter) {
  329. if (p_filter == state_filter)
  330. return;
  331. state_filter = p_filter;
  332. GLint pmin = GL_LINEAR; // param min
  333. GLint pmag = GL_LINEAR; // param mag
  334. switch (state_filter) {
  335. default: {
  336. } break;
  337. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  338. pmin = GL_LINEAR_MIPMAP_LINEAR;
  339. pmag = GL_LINEAR;
  340. } break;
  341. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  342. pmin = GL_NEAREST;
  343. pmag = GL_NEAREST;
  344. } break;
  345. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  346. pmin = GL_NEAREST_MIPMAP_NEAREST;
  347. pmag = GL_NEAREST;
  348. } break;
  349. }
  350. glTexParameteri(p_target, GL_TEXTURE_MIN_FILTER, pmin);
  351. glTexParameteri(p_target, GL_TEXTURE_MAG_FILTER, pmag);
  352. }
  353. void GLSetRepeat(RS::CanvasItemTextureRepeat p_repeat) {
  354. if (p_repeat == state_repeat)
  355. return;
  356. state_repeat = p_repeat;
  357. GLint prep = GL_CLAMP_TO_EDGE; // parameter repeat
  358. switch (state_repeat) {
  359. default: {
  360. } break;
  361. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  362. prep = GL_REPEAT;
  363. } break;
  364. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  365. prep = GL_MIRRORED_REPEAT;
  366. } break;
  367. }
  368. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, prep);
  369. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, prep);
  370. }
  371. private:
  372. RS::CanvasItemTextureFilter state_filter = RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR;
  373. RS::CanvasItemTextureRepeat state_repeat = RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED;
  374. };
  375. mutable RID_PtrOwner<Texture> texture_owner;
  376. Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool p_force_decompress) const;
  377. void _texture_set_state_from_flags(Texture *p_tex);
  378. // new
  379. RID texture_allocate() override;
  380. void texture_2d_initialize(RID p_texture, const Ref<Image> &p_image) override;
  381. void texture_2d_layered_initialize(RID p_texture, const Vector<Ref<Image>> &p_layers, RS::TextureLayeredType p_layered_type) override;
  382. void texture_3d_initialize(RID p_texture, Image::Format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_data) override;
  383. void texture_proxy_initialize(RID p_texture, RID p_base) override; //all slices, then all the mipmaps, must be coherent
  384. void texture_2d_update(RID p_texture, const Ref<Image> &p_image, int p_layer = 0) override;
  385. void texture_3d_update(RID p_texture, const Vector<Ref<Image>> &p_data) override {}
  386. void texture_proxy_update(RID p_proxy, RID p_base) override {}
  387. void texture_2d_placeholder_initialize(RID p_texture) override;
  388. void texture_2d_layered_placeholder_initialize(RID p_texture, RenderingServer::TextureLayeredType p_layered_type) override;
  389. void texture_3d_placeholder_initialize(RID p_texture) override;
  390. Ref<Image> texture_2d_get(RID p_texture) const override;
  391. Ref<Image> texture_2d_layer_get(RID p_texture, int p_layer) const override { return Ref<Image>(); }
  392. Vector<Ref<Image>> texture_3d_get(RID p_texture) const override { return Vector<Ref<Image>>(); }
  393. void texture_replace(RID p_texture, RID p_by_texture) override;
  394. //void texture_set_size_override(RID p_texture, int p_width, int p_height) override {}
  395. void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  396. void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {}
  397. // old
  398. uint32_t texture_get_width(RID p_texture) const;
  399. uint32_t texture_get_height(RID p_texture) const;
  400. private:
  401. RID texture_create();
  402. //void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, RenderingDevice::TextureType p_type, uint32_t p_flags = TEXTURE_FLAGS_DEFAULT);
  403. void _texture_allocate_internal(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, RenderingDevice::TextureType p_type, uint32_t p_flags = TEXTURE_FLAGS_DEFAULT);
  404. void texture_set_data(RID p_texture, const Ref<Image> &p_image, int p_layer = 0);
  405. void texture_set_data_partial(RID p_texture, const Ref<Image> &p_image, int src_x, int src_y, int src_w, int src_h, int dst_x, int dst_y, int p_dst_mip, int p_layer = 0);
  406. //Ref<Image> texture_get_data(RID p_texture, int p_layer = 0) const;
  407. void texture_set_flags(RID p_texture, uint32_t p_flags);
  408. uint32_t texture_get_flags(RID p_texture) const;
  409. Image::Format texture_get_format(RID p_texture) const;
  410. RenderingDevice::TextureType texture_get_type(RID p_texture) const;
  411. uint32_t texture_get_texid(RID p_texture) const;
  412. uint32_t texture_get_depth(RID p_texture) const;
  413. void texture_set_size_override(RID p_texture, int p_width, int p_height) override;
  414. void texture_bind(RID p_texture, uint32_t p_texture_no);
  415. void texture_set_path(RID p_texture, const String &p_path) override;
  416. String texture_get_path(RID p_texture) const override;
  417. void texture_set_shrink_all_x2_on_set_data(bool p_enable);
  418. void texture_debug_usage(List<RS::TextureInfo> *r_info) override;
  419. RID texture_create_radiance_cubemap(RID p_source, int p_resolution = -1) const;
  420. void textures_keep_original(bool p_enable);
  421. void texture_set_proxy(RID p_texture, RID p_proxy);
  422. Size2 texture_size_with_proxy(RID p_texture) override;
  423. void texture_set_detect_3d_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  424. void texture_set_detect_srgb_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata);
  425. void texture_set_detect_normal_callback(RID p_texture, RS::TextureDetectCallback p_callback, void *p_userdata) override;
  426. void texture_set_detect_roughness_callback(RID p_texture, RS::TextureDetectRoughnessCallback p_callback, void *p_userdata) override {}
  427. void texture_set_force_redraw_if_visible(RID p_texture, bool p_enable) override;
  428. public:
  429. RID canvas_texture_allocate() override;
  430. void canvas_texture_initialize(RID p_rid) override;
  431. void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override;
  432. void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override;
  433. void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override;
  434. void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override;
  435. /* SKY API */
  436. // not sure if used in godot 4?
  437. struct Sky {
  438. RID self;
  439. RID panorama;
  440. GLuint radiance;
  441. int radiance_size;
  442. };
  443. mutable RID_PtrOwner<Sky> sky_owner;
  444. RID sky_create();
  445. void sky_set_texture(RID p_sky, RID p_panorama, int p_radiance_size);
  446. // SHADER API
  447. struct Material;
  448. struct Shader {
  449. RID self;
  450. RS::ShaderMode mode;
  451. ShaderOpenGL *shader;
  452. String code;
  453. SelfList<Material>::List materials;
  454. Map<StringName, ShaderLanguage::ShaderNode::Uniform> uniforms;
  455. uint32_t texture_count;
  456. uint32_t custom_code_id;
  457. uint32_t version;
  458. SelfList<Shader> dirty_list;
  459. Map<StringName, RID> default_textures;
  460. Vector<ShaderLanguage::ShaderNode::Uniform::Hint> texture_hints;
  461. bool valid;
  462. String path;
  463. uint32_t index;
  464. uint64_t last_pass;
  465. struct CanvasItem {
  466. enum BlendMode {
  467. BLEND_MODE_MIX,
  468. BLEND_MODE_ADD,
  469. BLEND_MODE_SUB,
  470. BLEND_MODE_MUL,
  471. BLEND_MODE_PMALPHA,
  472. };
  473. int blend_mode;
  474. enum LightMode {
  475. LIGHT_MODE_NORMAL,
  476. LIGHT_MODE_UNSHADED,
  477. LIGHT_MODE_LIGHT_ONLY
  478. };
  479. int light_mode;
  480. // these flags are specifically for batching
  481. // some of the logic is thus in rasterizer_storage.cpp
  482. // we could alternatively set bitflags for each 'uses' and test on the fly
  483. // defined in RasterizerStorageCommon::BatchFlags
  484. unsigned int batch_flags;
  485. bool uses_screen_texture;
  486. bool uses_screen_uv;
  487. bool uses_time;
  488. bool uses_modulate;
  489. bool uses_color;
  490. bool uses_vertex;
  491. // all these should disable item joining if used in a custom shader
  492. bool uses_world_matrix;
  493. bool uses_extra_matrix;
  494. bool uses_projection_matrix;
  495. bool uses_instance_custom;
  496. } canvas_item;
  497. struct Spatial {
  498. enum BlendMode {
  499. BLEND_MODE_MIX,
  500. BLEND_MODE_ADD,
  501. BLEND_MODE_SUB,
  502. BLEND_MODE_MUL,
  503. };
  504. int blend_mode;
  505. enum DepthDrawMode {
  506. DEPTH_DRAW_OPAQUE,
  507. DEPTH_DRAW_ALWAYS,
  508. DEPTH_DRAW_NEVER,
  509. DEPTH_DRAW_ALPHA_PREPASS,
  510. };
  511. int depth_draw_mode;
  512. enum CullMode {
  513. CULL_MODE_FRONT,
  514. CULL_MODE_BACK,
  515. CULL_MODE_DISABLED,
  516. };
  517. int cull_mode;
  518. bool uses_alpha;
  519. bool uses_alpha_scissor;
  520. bool unshaded;
  521. bool no_depth_test;
  522. bool uses_vertex;
  523. bool uses_discard;
  524. bool uses_sss;
  525. bool uses_screen_texture;
  526. bool uses_depth_texture;
  527. bool uses_time;
  528. bool uses_tangent;
  529. bool uses_ensure_correct_normals;
  530. bool writes_modelview_or_projection;
  531. bool uses_vertex_lighting;
  532. bool uses_world_coordinates;
  533. } spatial;
  534. struct Particles {
  535. } particles;
  536. bool uses_vertex_time;
  537. bool uses_fragment_time;
  538. Shader() :
  539. dirty_list(this) {
  540. shader = NULL;
  541. valid = false;
  542. custom_code_id = 0;
  543. version = 1;
  544. last_pass = 0;
  545. }
  546. };
  547. mutable RID_PtrOwner<Shader> shader_owner;
  548. mutable SelfList<Shader>::List _shader_dirty_list;
  549. void _shader_make_dirty(Shader *p_shader);
  550. RID shader_allocate() override;
  551. void shader_initialize(RID p_rid) override;
  552. //RID shader_create() override;
  553. void shader_set_code(RID p_shader, const String &p_code) override;
  554. String shader_get_code(RID p_shader) const override;
  555. void shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const override;
  556. void shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture) override;
  557. RID shader_get_default_texture_param(RID p_shader, const StringName &p_name) const override;
  558. RS::ShaderNativeSourceCode shader_get_native_source_code(RID p_shader) const override { return RS::ShaderNativeSourceCode(); };
  559. void shader_add_custom_define(RID p_shader, const String &p_define);
  560. void shader_get_custom_defines(RID p_shader, Vector<String> *p_defines) const;
  561. void shader_remove_custom_define(RID p_shader, const String &p_define);
  562. void _update_shader(Shader *p_shader) const;
  563. void update_dirty_shaders();
  564. // new
  565. Variant shader_get_param_default(RID p_material, const StringName &p_param) const override { return Variant(); }
  566. // COMMON MATERIAL API
  567. struct Material {
  568. RID self;
  569. Shader *shader;
  570. Map<StringName, Variant> params;
  571. SelfList<Material> list;
  572. SelfList<Material> dirty_list;
  573. Vector<Pair<StringName, RID>> textures;
  574. float line_width;
  575. int render_priority;
  576. RID next_pass;
  577. uint32_t index;
  578. uint64_t last_pass;
  579. // Map<Geometry *, int> geometry_owners;
  580. // Map<InstanceBaseDependency *, int> instance_owners;
  581. bool can_cast_shadow_cache;
  582. bool is_animated_cache;
  583. Material() :
  584. list(this),
  585. dirty_list(this) {
  586. can_cast_shadow_cache = false;
  587. is_animated_cache = false;
  588. shader = NULL;
  589. line_width = 1.0;
  590. last_pass = 0;
  591. render_priority = 0;
  592. }
  593. };
  594. mutable SelfList<Material>::List _material_dirty_list;
  595. void _material_make_dirty(Material *p_material) const;
  596. // void _material_add_geometry(RID p_material, Geometry *p_geometry);
  597. // void _material_remove_geometry(RID p_material, Geometry *p_geometry);
  598. void _update_material(Material *p_material);
  599. mutable RID_PtrOwner<Material> material_owner;
  600. // new
  601. void material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) override {}
  602. void material_update_dependency(RID p_material, DependencyTracker *p_instance) override {}
  603. // old
  604. RID material_allocate() override;
  605. void material_initialize(RID p_rid) override;
  606. //RID material_create() override;
  607. void material_set_shader(RID p_material, RID p_shader) override;
  608. RID material_get_shader(RID p_material) const;
  609. void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) override;
  610. Variant material_get_param(RID p_material, const StringName &p_param) const override;
  611. Variant material_get_param_default(RID p_material, const StringName &p_param) const;
  612. void material_set_line_width(RID p_material, float p_width);
  613. void material_set_next_pass(RID p_material, RID p_next_material) override;
  614. bool material_is_animated(RID p_material) override;
  615. bool material_casts_shadows(RID p_material) override;
  616. bool material_uses_tangents(RID p_material);
  617. bool material_uses_ensure_correct_normals(RID p_material);
  618. void material_add_instance_owner(RID p_material, DependencyTracker *p_instance);
  619. void material_remove_instance_owner(RID p_material, DependencyTracker *p_instance);
  620. void material_set_render_priority(RID p_material, int priority) override;
  621. void update_dirty_materials();
  622. /* MESH API */
  623. RID mesh_allocate() override;
  624. void mesh_initialize(RID p_rid) override;
  625. void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) override;
  626. bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) override;
  627. RID mesh_instance_create(RID p_base) override;
  628. void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) override;
  629. void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) override;
  630. void mesh_instance_check_for_update(RID p_mesh_instance) override;
  631. void update_mesh_instances() override;
  632. void reflection_probe_set_lod_threshold(RID p_probe, float p_ratio) override;
  633. float reflection_probe_get_lod_threshold(RID p_probe) const override;
  634. void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) override;
  635. int mesh_get_blend_shape_count(RID p_mesh) const override;
  636. void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) override;
  637. RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const override;
  638. void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  639. void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  640. void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  641. void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override;
  642. RID mesh_surface_get_material(RID p_mesh, int p_surface) const override;
  643. RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const override;
  644. int mesh_get_surface_count(RID p_mesh) const override;
  645. void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override;
  646. AABB mesh_get_custom_aabb(RID p_mesh) const override;
  647. AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override;
  648. void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override;
  649. void mesh_clear(RID p_mesh) override;
  650. /* MULTIMESH API */
  651. RID multimesh_allocate() override;
  652. void multimesh_initialize(RID p_rid) override;
  653. void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false) override;
  654. int multimesh_get_instance_count(RID p_multimesh) const override;
  655. void multimesh_set_mesh(RID p_multimesh, RID p_mesh) override;
  656. void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) override;
  657. void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) override;
  658. void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) override;
  659. void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override;
  660. RID multimesh_get_mesh(RID p_multimesh) const override;
  661. AABB multimesh_get_aabb(RID p_multimesh) const override;
  662. Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const override;
  663. Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const override;
  664. Color multimesh_instance_get_color(RID p_multimesh, int p_index) const override;
  665. Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const override;
  666. void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) override;
  667. Vector<float> multimesh_get_buffer(RID p_multimesh) const override;
  668. void multimesh_set_visible_instances(RID p_multimesh, int p_visible) override;
  669. int multimesh_get_visible_instances(RID p_multimesh) const override;
  670. /* SKELETON API */
  671. RID skeleton_allocate() override;
  672. void skeleton_initialize(RID p_rid) override;
  673. void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) override;
  674. void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) override;
  675. int skeleton_get_bone_count(RID p_skeleton) const override;
  676. void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) override;
  677. Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const override;
  678. void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) override;
  679. Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const override;
  680. /* Light API */
  681. RID directional_light_allocate() override;
  682. void directional_light_initialize(RID p_rid) override;
  683. RID omni_light_allocate() override;
  684. void omni_light_initialize(RID p_rid) override;
  685. RID spot_light_allocate() override;
  686. void spot_light_initialize(RID p_rid) override;
  687. RID reflection_probe_allocate() override;
  688. void reflection_probe_initialize(RID p_rid) override;
  689. void light_set_color(RID p_light, const Color &p_color) override;
  690. void light_set_param(RID p_light, RS::LightParam p_param, float p_value) override;
  691. void light_set_shadow(RID p_light, bool p_enabled) override;
  692. void light_set_shadow_color(RID p_light, const Color &p_color) override;
  693. void light_set_projector(RID p_light, RID p_texture) override;
  694. void light_set_negative(RID p_light, bool p_enable) override;
  695. void light_set_cull_mask(RID p_light, uint32_t p_mask) override;
  696. void light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) override;
  697. void light_set_bake_mode(RID p_light, RS::LightBakeMode p_bake_mode) override;
  698. void light_set_max_sdfgi_cascade(RID p_light, uint32_t p_cascade) override;
  699. void light_omni_set_shadow_mode(RID p_light, RS::LightOmniShadowMode p_mode) override;
  700. void light_directional_set_shadow_mode(RID p_light, RS::LightDirectionalShadowMode p_mode) override;
  701. void light_directional_set_blend_splits(RID p_light, bool p_enable) override;
  702. bool light_directional_get_blend_splits(RID p_light) const override;
  703. void light_directional_set_sky_only(RID p_light, bool p_sky_only) override;
  704. bool light_directional_is_sky_only(RID p_light) const override;
  705. RS::LightDirectionalShadowMode light_directional_get_shadow_mode(RID p_light) override;
  706. RS::LightOmniShadowMode light_omni_get_shadow_mode(RID p_light) override;
  707. bool light_has_shadow(RID p_light) const override;
  708. bool light_has_projector(RID p_light) const override;
  709. RS::LightType light_get_type(RID p_light) const override;
  710. AABB light_get_aabb(RID p_light) const override;
  711. float light_get_param(RID p_light, RS::LightParam p_param) override;
  712. Color light_get_color(RID p_light) override;
  713. RS::LightBakeMode light_get_bake_mode(RID p_light) override;
  714. uint32_t light_get_max_sdfgi_cascade(RID p_light) override;
  715. uint64_t light_get_version(RID p_light) const override;
  716. /* PROBE API */
  717. void reflection_probe_set_update_mode(RID p_probe, RS::ReflectionProbeUpdateMode p_mode) override;
  718. void reflection_probe_set_intensity(RID p_probe, float p_intensity) override;
  719. void reflection_probe_set_ambient_mode(RID p_probe, RS::ReflectionProbeAmbientMode p_mode) override;
  720. void reflection_probe_set_ambient_color(RID p_probe, const Color &p_color) override;
  721. void reflection_probe_set_ambient_energy(RID p_probe, float p_energy) override;
  722. void reflection_probe_set_max_distance(RID p_probe, float p_distance) override;
  723. void reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) override;
  724. void reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) override;
  725. void reflection_probe_set_as_interior(RID p_probe, bool p_enable) override;
  726. void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) override;
  727. void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) override;
  728. void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) override;
  729. void reflection_probe_set_resolution(RID p_probe, int p_resolution) override;
  730. AABB reflection_probe_get_aabb(RID p_probe) const override;
  731. RS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const override;
  732. uint32_t reflection_probe_get_cull_mask(RID p_probe) const override;
  733. Vector3 reflection_probe_get_extents(RID p_probe) const override;
  734. Vector3 reflection_probe_get_origin_offset(RID p_probe) const override;
  735. float reflection_probe_get_origin_max_distance(RID p_probe) const override;
  736. bool reflection_probe_renders_shadows(RID p_probe) const override;
  737. void base_update_dependency(RID p_base, DependencyTracker *p_instance) override;
  738. void skeleton_update_dependency(RID p_base, DependencyTracker *p_instance) override;
  739. /* DECAL API */
  740. RID decal_allocate() override;
  741. void decal_initialize(RID p_rid) override;
  742. void decal_set_extents(RID p_decal, const Vector3 &p_extents) override;
  743. void decal_set_texture(RID p_decal, RS::DecalTexture p_type, RID p_texture) override;
  744. void decal_set_emission_energy(RID p_decal, float p_energy) override;
  745. void decal_set_albedo_mix(RID p_decal, float p_mix) override;
  746. void decal_set_modulate(RID p_decal, const Color &p_modulate) override;
  747. void decal_set_cull_mask(RID p_decal, uint32_t p_layers) override;
  748. void decal_set_distance_fade(RID p_decal, bool p_enabled, float p_begin, float p_length) override;
  749. void decal_set_fade(RID p_decal, float p_above, float p_below) override;
  750. void decal_set_normal_fade(RID p_decal, float p_fade) override;
  751. AABB decal_get_aabb(RID p_decal) const override;
  752. /* VOXEL GI API */
  753. RID voxel_gi_allocate() override;
  754. void voxel_gi_initialize(RID p_rid) override;
  755. void voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &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) override;
  756. AABB voxel_gi_get_bounds(RID p_voxel_gi) const override;
  757. Vector3i voxel_gi_get_octree_size(RID p_voxel_gi) const override;
  758. Vector<uint8_t> voxel_gi_get_octree_cells(RID p_voxel_gi) const override;
  759. Vector<uint8_t> voxel_gi_get_data_cells(RID p_voxel_gi) const override;
  760. Vector<uint8_t> voxel_gi_get_distance_field(RID p_voxel_gi) const override;
  761. Vector<int> voxel_gi_get_level_counts(RID p_voxel_gi) const override;
  762. Transform3D voxel_gi_get_to_cell_xform(RID p_voxel_gi) const override;
  763. void voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range) override;
  764. float voxel_gi_get_dynamic_range(RID p_voxel_gi) const override;
  765. void voxel_gi_set_propagation(RID p_voxel_gi, float p_range) override;
  766. float voxel_gi_get_propagation(RID p_voxel_gi) const override;
  767. void voxel_gi_set_energy(RID p_voxel_gi, float p_range) override;
  768. float voxel_gi_get_energy(RID p_voxel_gi) const override;
  769. void voxel_gi_set_bias(RID p_voxel_gi, float p_range) override;
  770. float voxel_gi_get_bias(RID p_voxel_gi) const override;
  771. void voxel_gi_set_normal_bias(RID p_voxel_gi, float p_range) override;
  772. float voxel_gi_get_normal_bias(RID p_voxel_gi) const override;
  773. void voxel_gi_set_interior(RID p_voxel_gi, bool p_enable) override;
  774. bool voxel_gi_is_interior(RID p_voxel_gi) const override;
  775. void voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable) override;
  776. bool voxel_gi_is_using_two_bounces(RID p_voxel_gi) const override;
  777. void voxel_gi_set_anisotropy_strength(RID p_voxel_gi, float p_strength) override;
  778. float voxel_gi_get_anisotropy_strength(RID p_voxel_gi) const override;
  779. uint32_t voxel_gi_get_version(RID p_voxel_gi) override;
  780. /* LIGHTMAP CAPTURE */
  781. RID lightmap_allocate() override;
  782. void lightmap_initialize(RID p_rid) override;
  783. void lightmap_set_textures(RID p_lightmap, RID p_light, bool p_uses_spherical_haromics) override;
  784. void lightmap_set_probe_bounds(RID p_lightmap, const AABB &p_bounds) override;
  785. void lightmap_set_probe_interior(RID p_lightmap, bool p_interior) override;
  786. 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) override;
  787. PackedVector3Array lightmap_get_probe_capture_points(RID p_lightmap) const override;
  788. PackedColorArray lightmap_get_probe_capture_sh(RID p_lightmap) const override;
  789. PackedInt32Array lightmap_get_probe_capture_tetrahedra(RID p_lightmap) const override;
  790. PackedInt32Array lightmap_get_probe_capture_bsp_tree(RID p_lightmap) const override;
  791. AABB lightmap_get_aabb(RID p_lightmap) const override;
  792. void lightmap_tap_sh_light(RID p_lightmap, const Vector3 &p_point, Color *r_sh) override;
  793. bool lightmap_is_interior(RID p_lightmap) const override;
  794. void lightmap_set_probe_capture_update_speed(float p_speed) override;
  795. float lightmap_get_probe_capture_update_speed() const override;
  796. /* OCCLUDER */
  797. void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices);
  798. /* PARTICLES */
  799. RID particles_allocate() override;
  800. void particles_initialize(RID p_rid) override;
  801. void particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) override;
  802. void particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) override;
  803. void particles_set_emitting(RID p_particles, bool p_emitting) override;
  804. void particles_set_amount(RID p_particles, int p_amount) override;
  805. void particles_set_lifetime(RID p_particles, double p_lifetime) override;
  806. void particles_set_one_shot(RID p_particles, bool p_one_shot) override;
  807. void particles_set_pre_process_time(RID p_particles, double p_time) override;
  808. void particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) override;
  809. void particles_set_randomness_ratio(RID p_particles, real_t p_ratio) override;
  810. void particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) override;
  811. void particles_set_speed_scale(RID p_particles, double p_scale) override;
  812. void particles_set_use_local_coordinates(RID p_particles, bool p_enable) override;
  813. void particles_set_process_material(RID p_particles, RID p_material) override;
  814. void particles_set_fixed_fps(RID p_particles, int p_fps) override;
  815. void particles_set_interpolate(RID p_particles, bool p_enable) override;
  816. void particles_set_fractional_delta(RID p_particles, bool p_enable) override;
  817. void particles_set_subemitter(RID p_particles, RID p_subemitter_particles) override;
  818. void particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) override;
  819. void particles_set_collision_base_size(RID p_particles, real_t p_size) override;
  820. void particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) override;
  821. void particles_set_trails(RID p_particles, bool p_enable, double p_length) override;
  822. void particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) override;
  823. void particles_restart(RID p_particles) override;
  824. void particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) override;
  825. void particles_set_draw_passes(RID p_particles, int p_count) override;
  826. void particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) override;
  827. void particles_request_process(RID p_particles) override;
  828. AABB particles_get_current_aabb(RID p_particles) override;
  829. AABB particles_get_aabb(RID p_particles) const override;
  830. void particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) override;
  831. bool particles_get_emitting(RID p_particles) override;
  832. int particles_get_draw_passes(RID p_particles) const override;
  833. RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const override;
  834. void particles_add_collision(RID p_particles, RID p_instance) override;
  835. void particles_remove_collision(RID p_particles, RID p_instance) override;
  836. void particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, RID p_texture) override;
  837. void update_particles() override;
  838. /* PARTICLES COLLISION */
  839. RID particles_collision_allocate() override;
  840. void particles_collision_initialize(RID p_rid) override;
  841. void particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) override;
  842. void particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) override;
  843. void particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) override;
  844. void particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) override;
  845. void particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) override;
  846. void particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) override;
  847. void particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) override;
  848. void particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) override;
  849. void particles_collision_height_field_update(RID p_particles_collision) override;
  850. void particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) override;
  851. AABB particles_collision_get_aabb(RID p_particles_collision) const override;
  852. bool particles_collision_is_heightfield(RID p_particles_collision) const override;
  853. RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const override;
  854. RID particles_collision_instance_create(RID p_collision) override;
  855. void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) override;
  856. void particles_collision_instance_set_active(RID p_collision_instance, bool p_active) override;
  857. /* VISIBILITY NOTIFIER */
  858. RID visibility_notifier_allocate() override;
  859. void visibility_notifier_initialize(RID p_notifier) override;
  860. void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) override;
  861. void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) override;
  862. AABB visibility_notifier_get_aabb(RID p_notifier) const override;
  863. void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) override;
  864. /* GLOBAL VARIABLES */
  865. void global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) override;
  866. void global_variable_remove(const StringName &p_name) override;
  867. Vector<StringName> global_variable_get_list() const override;
  868. void global_variable_set(const StringName &p_name, const Variant &p_value) override;
  869. void global_variable_set_override(const StringName &p_name, const Variant &p_value) override;
  870. Variant global_variable_get(const StringName &p_name) const override;
  871. RS::GlobalVariableType global_variable_get_type(const StringName &p_name) const override;
  872. void global_variables_load_settings(bool p_load_textures = true) override;
  873. void global_variables_clear() override;
  874. int32_t global_variables_instance_allocate(RID p_instance) override;
  875. void global_variables_instance_free(RID p_instance) override;
  876. void global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) override;
  877. bool particles_is_inactive(RID p_particles) const override;
  878. // RENDER TARGET
  879. struct RenderTarget {
  880. RID self;
  881. GLuint fbo;
  882. GLuint color;
  883. GLuint depth;
  884. GLuint multisample_fbo;
  885. GLuint multisample_color;
  886. GLuint multisample_depth;
  887. bool multisample_active;
  888. struct Effect {
  889. GLuint fbo;
  890. int width;
  891. int height;
  892. GLuint color;
  893. Effect() :
  894. fbo(0),
  895. width(0),
  896. height(0),
  897. color(0) {
  898. }
  899. };
  900. Effect copy_screen_effect;
  901. struct MipMaps {
  902. struct Size {
  903. GLuint fbo;
  904. GLuint color;
  905. int width;
  906. int height;
  907. };
  908. Vector<Size> sizes;
  909. GLuint color;
  910. int levels;
  911. MipMaps() :
  912. color(0),
  913. levels(0) {
  914. }
  915. };
  916. MipMaps mip_maps[2];
  917. struct External {
  918. GLuint fbo;
  919. GLuint color;
  920. GLuint depth;
  921. RID texture;
  922. External() :
  923. fbo(0),
  924. color(0),
  925. depth(0) {
  926. }
  927. } external;
  928. int x, y, width, height;
  929. bool flags[RENDER_TARGET_FLAG_MAX];
  930. // instead of allocating sized render targets immediately,
  931. // defer this for faster startup
  932. bool allocate_is_dirty = false;
  933. bool used_in_frame;
  934. RS::ViewportMSAA msaa;
  935. bool use_fxaa;
  936. bool use_debanding;
  937. RID texture;
  938. bool used_dof_blur_near;
  939. bool mip_maps_allocated;
  940. Color clear_color;
  941. bool clear_requested;
  942. RenderTarget() :
  943. fbo(0),
  944. color(0),
  945. depth(0),
  946. multisample_fbo(0),
  947. multisample_color(0),
  948. multisample_depth(0),
  949. multisample_active(false),
  950. x(0),
  951. y(0),
  952. width(0),
  953. height(0),
  954. used_in_frame(false),
  955. msaa(RS::VIEWPORT_MSAA_DISABLED),
  956. use_fxaa(false),
  957. use_debanding(false),
  958. used_dof_blur_near(false),
  959. mip_maps_allocated(false),
  960. clear_color(Color(1, 1, 1, 1)),
  961. clear_requested(false) {
  962. for (int i = 0; i < RENDER_TARGET_FLAG_MAX; ++i) {
  963. flags[i] = false;
  964. }
  965. external.fbo = 0;
  966. }
  967. };
  968. mutable RID_PtrOwner<RenderTarget> render_target_owner;
  969. void _render_target_clear(RenderTarget *rt);
  970. void _render_target_allocate(RenderTarget *rt);
  971. void _set_current_render_target(RID p_render_target);
  972. RID render_target_create() override;
  973. void render_target_set_position(RID p_render_target, int p_x, int p_y) override;
  974. void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override;
  975. RID render_target_get_texture(RID p_render_target) override;
  976. void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) override;
  977. void render_target_set_flag(RID p_render_target, RenderTargetFlags p_flag, bool p_value) override;
  978. bool render_target_was_used(RID p_render_target) override;
  979. void render_target_clear_used(RID p_render_target);
  980. void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa);
  981. void render_target_set_use_fxaa(RID p_render_target, bool p_fxaa);
  982. void render_target_set_use_debanding(RID p_render_target, bool p_debanding);
  983. // new
  984. void render_target_set_as_unused(RID p_render_target) override {
  985. render_target_clear_used(p_render_target);
  986. }
  987. void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override;
  988. bool render_target_is_clear_requested(RID p_render_target) override;
  989. Color render_target_get_clear_request_color(RID p_render_target) override;
  990. void render_target_disable_clear_request(RID p_render_target) override;
  991. void render_target_do_clear_request(RID p_render_target) override;
  992. void render_target_set_sdf_size_and_scale(RID p_render_target, RS::ViewportSDFOversize p_size, RS::ViewportSDFScale p_scale) override;
  993. Rect2i render_target_get_sdf_rect(RID p_render_target) const override;
  994. void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override;
  995. // access from canvas
  996. // RenderTarget * render_target_get(RID p_render_target);
  997. /* CANVAS SHADOW */
  998. struct CanvasLightShadow {
  999. RID self;
  1000. int size;
  1001. int height;
  1002. GLuint fbo;
  1003. GLuint depth;
  1004. GLuint distance; //for older devices
  1005. };
  1006. RID_PtrOwner<CanvasLightShadow> canvas_light_shadow_owner;
  1007. RID canvas_light_shadow_buffer_create(int p_width);
  1008. /* LIGHT SHADOW MAPPING */
  1009. /*
  1010. struct CanvasOccluder {
  1011. RID self;
  1012. GLuint vertex_id; // 0 means, unconfigured
  1013. GLuint index_id; // 0 means, unconfigured
  1014. LocalVector<Vector2> lines;
  1015. int len;
  1016. };
  1017. RID_Owner<CanvasOccluder> canvas_occluder_owner;
  1018. RID canvas_light_occluder_create();
  1019. void canvas_light_occluder_set_polylines(RID p_occluder, const LocalVector<Vector2> &p_lines);
  1020. */
  1021. RS::InstanceType get_base_type(RID p_rid) const override;
  1022. bool free(RID p_rid) override;
  1023. struct Frame {
  1024. RenderTarget *current_rt;
  1025. // these 2 may have been superceded by the equivalents in the render target.
  1026. // these may be able to be removed.
  1027. bool clear_request;
  1028. Color clear_request_color;
  1029. float time[4];
  1030. float delta;
  1031. uint64_t count;
  1032. Frame() {
  1033. // current_rt = nullptr;
  1034. // clear_request = false;
  1035. }
  1036. } frame;
  1037. void initialize();
  1038. void finalize();
  1039. void _copy_screen();
  1040. void update_memory_info() override;
  1041. uint64_t get_rendering_info(RS::RenderingInfo p_info) override;
  1042. bool has_os_feature(const String &p_feature) const override;
  1043. void update_dirty_resources() override;
  1044. void set_debug_generate_wireframes(bool p_generate) override;
  1045. // void render_info_begin_capture() override;
  1046. // void render_info_end_capture() override;
  1047. // int get_captured_render_info(RS::RenderInfo p_info) override;
  1048. // int get_render_info(RS::RenderInfo p_info) override;
  1049. String get_video_adapter_name() const override;
  1050. String get_video_adapter_vendor() const override;
  1051. void capture_timestamps_begin() override {}
  1052. void capture_timestamp(const String &p_name) override {}
  1053. uint32_t get_captured_timestamps_count() const override {
  1054. return 0;
  1055. }
  1056. uint64_t get_captured_timestamps_frame() const override {
  1057. return 0;
  1058. }
  1059. uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const override {
  1060. return 0;
  1061. }
  1062. uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const override {
  1063. return 0;
  1064. }
  1065. String get_captured_timestamp_name(uint32_t p_index) const override {
  1066. return String();
  1067. }
  1068. // make access easier to these
  1069. struct Dimensions {
  1070. // render target
  1071. int rt_width;
  1072. int rt_height;
  1073. // window
  1074. int win_width;
  1075. int win_height;
  1076. Dimensions() {
  1077. rt_width = 0;
  1078. rt_height = 0;
  1079. win_width = 0;
  1080. win_height = 0;
  1081. }
  1082. } _dims;
  1083. void buffer_orphan_and_upload(unsigned int p_buffer_size, unsigned int p_offset, unsigned int p_data_size, const void *p_data, GLenum p_target = GL_ARRAY_BUFFER, GLenum p_usage = GL_DYNAMIC_DRAW, bool p_optional_orphan = false) const;
  1084. bool safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const;
  1085. void bind_framebuffer(GLuint framebuffer) {
  1086. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  1087. }
  1088. void bind_framebuffer_system() {
  1089. glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageOpenGL::system_fbo);
  1090. }
  1091. RasterizerStorageOpenGL();
  1092. };
  1093. inline bool RasterizerStorageOpenGL::safe_buffer_sub_data(unsigned int p_total_buffer_size, GLenum p_target, unsigned int p_offset, unsigned int p_data_size, const void *p_data, unsigned int &r_offset_after) const {
  1094. r_offset_after = p_offset + p_data_size;
  1095. #ifdef DEBUG_ENABLED
  1096. // we are trying to write across the edge of the buffer
  1097. if (r_offset_after > p_total_buffer_size)
  1098. return false;
  1099. #endif
  1100. glBufferSubData(p_target, p_offset, p_data_size, p_data);
  1101. return true;
  1102. }
  1103. // standardize the orphan / upload in one place so it can be changed per platform as necessary, and avoid future
  1104. // bugs causing pipeline stalls
  1105. inline void RasterizerStorageOpenGL::buffer_orphan_and_upload(unsigned int p_buffer_size, unsigned int p_offset, unsigned int p_data_size, const void *p_data, GLenum p_target, GLenum p_usage, bool p_optional_orphan) const {
  1106. // Orphan the buffer to avoid CPU/GPU sync points caused by glBufferSubData
  1107. // Was previously #ifndef GLES_OVER_GL however this causes stalls on desktop mac also (and possibly other)
  1108. if (!p_optional_orphan || (config.should_orphan)) {
  1109. glBufferData(p_target, p_buffer_size, NULL, p_usage);
  1110. #ifdef RASTERIZER_EXTRA_CHECKS
  1111. // fill with garbage off the end of the array
  1112. if (p_buffer_size) {
  1113. unsigned int start = p_offset + p_data_size;
  1114. unsigned int end = start + 1024;
  1115. if (end < p_buffer_size) {
  1116. uint8_t *garbage = (uint8_t *)alloca(1024);
  1117. for (int n = 0; n < 1024; n++) {
  1118. garbage[n] = Math::random(0, 255);
  1119. }
  1120. glBufferSubData(p_target, start, 1024, garbage);
  1121. }
  1122. }
  1123. #endif
  1124. }
  1125. RAST_DEV_DEBUG_ASSERT((p_offset + p_data_size) <= p_buffer_size);
  1126. glBufferSubData(p_target, p_offset, p_data_size, p_data);
  1127. }
  1128. #endif // OPENGL_BACKEND_ENABLED
  1129. #endif // RASTERIZER_STORAGE_OPENGL_H