pipeline_cache_rd.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, uint32_t p_bool_specializations) {
  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. bool wireframe = p_wireframe || rasterization_state.wireframe;
  36. RD::PipelineRasterizationState raster_state_version = rasterization_state;
  37. raster_state_version.wireframe = wireframe;
  38. Vector<RD::PipelineSpecializationConstant> specialization_constants = base_specialization_constants;
  39. uint32_t bool_index = 0;
  40. uint32_t bool_specializations = p_bool_specializations;
  41. while (bool_specializations) {
  42. if (bool_specializations & (1 << bool_index)) {
  43. RD::PipelineSpecializationConstant sc;
  44. sc.bool_value = true;
  45. sc.constant_id = bool_index;
  46. sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  47. specialization_constants.push_back(sc);
  48. bool_specializations &= ~(1 << bool_index);
  49. }
  50. bool_index++;
  51. }
  52. 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, specialization_constants);
  53. ERR_FAIL_COND_V(pipeline.is_null(), RID());
  54. versions = (Version *)memrealloc(versions, sizeof(Version) * (version_count + 1));
  55. versions[version_count].framebuffer_id = p_framebuffer_format_id;
  56. versions[version_count].vertex_id = p_vertex_format_id;
  57. versions[version_count].wireframe = wireframe;
  58. versions[version_count].pipeline = pipeline;
  59. versions[version_count].render_pass = p_render_pass;
  60. versions[version_count].bool_specializations = p_bool_specializations;
  61. version_count++;
  62. return pipeline;
  63. }
  64. void PipelineCacheRD::_clear() {
  65. #ifndef _MSC_VER
  66. #warning Clear should probably recompile all the variants already compiled instead to avoid stalls? needs discussion
  67. #endif
  68. if (versions) {
  69. for (uint32_t i = 0; i < version_count; i++) {
  70. //shader may be gone, so this may not be valid
  71. if (RD::get_singleton()->render_pipeline_is_valid(versions[i].pipeline)) {
  72. RD::get_singleton()->free(versions[i].pipeline);
  73. }
  74. }
  75. version_count = 0;
  76. memfree(versions);
  77. versions = nullptr;
  78. }
  79. }
  80. 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, const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) {
  81. ERR_FAIL_COND(p_shader.is_null());
  82. _clear();
  83. shader = p_shader;
  84. input_mask = RD::get_singleton()->shader_get_vertex_input_attribute_mask(p_shader);
  85. render_primitive = p_primitive;
  86. rasterization_state = p_rasterization_state;
  87. multisample_state = p_multisample;
  88. depth_stencil_state = p_depth_stencil_state;
  89. blend_state = p_blend_state;
  90. dynamic_state_flags = p_dynamic_state_flags;
  91. base_specialization_constants = p_base_specialization_constants;
  92. }
  93. void PipelineCacheRD::update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants) {
  94. base_specialization_constants = p_base_specialization_constants;
  95. _clear();
  96. }
  97. void PipelineCacheRD::update_shader(RID p_shader) {
  98. ERR_FAIL_COND(p_shader.is_null());
  99. _clear();
  100. setup(p_shader, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);
  101. }
  102. void PipelineCacheRD::clear() {
  103. _clear();
  104. shader = RID(); //clear shader
  105. input_mask = 0;
  106. }
  107. PipelineCacheRD::PipelineCacheRD() {
  108. version_count = 0;
  109. versions = nullptr;
  110. input_mask = 0;
  111. }
  112. PipelineCacheRD::~PipelineCacheRD() {
  113. _clear();
  114. }