pipeline_cache_rd.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* pipeline_cache_rd.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 PIPELINE_CACHE_RD_H
  31. #define PIPELINE_CACHE_RD_H
  32. #include "core/os/spin_lock.h"
  33. #include "servers/rendering/rendering_device.h"
  34. class PipelineCacheRD {
  35. SpinLock spin_lock;
  36. RID shader;
  37. RD::RenderPrimitive render_primitive;
  38. RD::PipelineRasterizationState rasterization_state;
  39. RD::PipelineMultisampleState multisample_state;
  40. RD::PipelineDepthStencilState depth_stencil_state;
  41. RD::PipelineColorBlendState blend_state;
  42. int dynamic_state_flags = 0;
  43. Vector<RD::PipelineSpecializationConstant> base_specialization_constants;
  44. struct Version {
  45. RD::VertexFormatID vertex_id;
  46. RD::FramebufferFormatID framebuffer_id;
  47. uint32_t render_pass;
  48. bool wireframe;
  49. uint32_t bool_specializations;
  50. RID pipeline;
  51. };
  52. Version *versions = nullptr;
  53. uint32_t version_count;
  54. RID _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 = 0);
  55. void _clear();
  56. public:
  57. void 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 = 0, const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants = Vector<RD::PipelineSpecializationConstant>());
  58. void update_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_base_specialization_constants);
  59. void update_shader(RID p_shader);
  60. _FORCE_INLINE_ RID get_render_pipeline(RD::VertexFormatID p_vertex_format_id, RD::FramebufferFormatID p_framebuffer_format_id, bool p_wireframe = false, uint32_t p_render_pass = 0, uint32_t p_bool_specializations = 0) {
  61. #ifdef DEBUG_ENABLED
  62. ERR_FAIL_COND_V_MSG(shader.is_null(), RID(),
  63. "Attempted to use an unused shader variant (shader is null),");
  64. #endif
  65. spin_lock.lock();
  66. p_wireframe |= rasterization_state.wireframe;
  67. RID result;
  68. for (uint32_t i = 0; i < version_count; i++) {
  69. if (versions[i].vertex_id == p_vertex_format_id && versions[i].framebuffer_id == p_framebuffer_format_id && versions[i].wireframe == p_wireframe && versions[i].render_pass == p_render_pass && versions[i].bool_specializations == p_bool_specializations) {
  70. result = versions[i].pipeline;
  71. spin_lock.unlock();
  72. return result;
  73. }
  74. }
  75. result = _generate_version(p_vertex_format_id, p_framebuffer_format_id, p_wireframe, p_render_pass, p_bool_specializations);
  76. spin_lock.unlock();
  77. return result;
  78. }
  79. _FORCE_INLINE_ uint64_t get_vertex_input_mask() {
  80. ERR_FAIL_COND_V(shader.is_null(), 0);
  81. return RD::get_singleton()->shader_get_vertex_input_attribute_mask(shader);
  82. }
  83. void clear();
  84. PipelineCacheRD();
  85. ~PipelineCacheRD();
  86. };
  87. #endif // PIPELINE_CACHE_RD_H