pipeline_cache_rd.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************/
  2. /* pipeline_cache_rd.cpp */
  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. #include "pipeline_cache_rd.h"
  31. #include "core/os/memory.h"
  32. RID PipelineCacheRD::_generate_version(RD::VertexFormatID p_vertex_format_id, RD::FramebufferFormatID p_framebuffer_format_id, bool p_wireframe, uint32_t p_render_pass) {
  33. RD::PipelineMultisampleState multisample_state_version = multisample_state;
  34. multisample_state_version.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_framebuffer_format_id, p_render_pass);
  35. RD::PipelineRasterizationState raster_state_version = rasterization_state;
  36. raster_state_version.wireframe = p_wireframe;
  37. RID pipeline = RD::get_singleton()->render_pipeline_create(shader, p_framebuffer_format_id, p_vertex_format_id, render_primitive, raster_state_version, multisample_state_version, depth_stencil_state, blend_state, dynamic_state_flags, p_render_pass);
  38. ERR_FAIL_COND_V(pipeline.is_null(), RID());
  39. versions = (Version *)memrealloc(versions, sizeof(Version) * (version_count + 1));
  40. versions[version_count].framebuffer_id = p_framebuffer_format_id;
  41. versions[version_count].vertex_id = p_vertex_format_id;
  42. versions[version_count].wireframe = p_wireframe;
  43. versions[version_count].pipeline = pipeline;
  44. versions[version_count].render_pass = p_render_pass;
  45. version_count++;
  46. return pipeline;
  47. }
  48. void PipelineCacheRD::_clear() {
  49. if (versions) {
  50. for (uint32_t i = 0; i < version_count; i++) {
  51. //shader may be gone, so this may not be valid
  52. if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) {
  53. RD::get_singleton()->free(versions[i].pipeline);
  54. }
  55. }
  56. version_count = 0;
  57. memfree(versions);
  58. versions = nullptr;
  59. }
  60. }
  61. void PipelineCacheRD::setup(RID p_shader, RD::RenderPrimitive p_primitive, const RD::PipelineRasterizationState &p_rasterization_state, RD::PipelineMultisampleState p_multisample, const RD::PipelineDepthStencilState &p_depth_stencil_state, const RD::PipelineColorBlendState &p_blend_state, int p_dynamic_state_flags) {
  62. ERR_FAIL_COND(p_shader.is_null());
  63. _clear();
  64. shader = p_shader;
  65. input_mask = RD::get_singleton()->shader_get_vertex_input_attribute_mask(p_shader);
  66. render_primitive = p_primitive;
  67. rasterization_state = p_rasterization_state;
  68. multisample_state = p_multisample;
  69. depth_stencil_state = p_depth_stencil_state;
  70. blend_state = p_blend_state;
  71. dynamic_state_flags = p_dynamic_state_flags;
  72. }
  73. void PipelineCacheRD::update_shader(RID p_shader) {
  74. ERR_FAIL_COND(p_shader.is_null());
  75. _clear();
  76. setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);
  77. }
  78. void PipelineCacheRD::clear() {
  79. _clear();
  80. shader = RID(); //clear shader
  81. input_mask = 0;
  82. }
  83. PipelineCacheRD::PipelineCacheRD() {
  84. version_count = 0;
  85. versions = nullptr;
  86. input_mask = 0;
  87. }
  88. PipelineCacheRD::~PipelineCacheRD() {
  89. _clear();
  90. }