render_pipeline_vertex_format_cache_rd.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "render_pipeline_vertex_format_cache_rd.h"
  2. #include "core/os/memory.h"
  3. RID RenderPipelineVertexFormatCacheRD::_generate_version(RD::VertexFormatID p_format_id, RenderingDevice::TextureSamples p_samples) {
  4. RD::PipelineMultisampleState multisample_state_version;
  5. if (p_samples != RD::TEXTURE_SAMPLES_1) {
  6. multisample_state_version = multisample_state;
  7. multisample_state_version.sample_count = p_samples;
  8. }
  9. RID pipeline = RD::get_singleton()->render_pipeline_create(shader, framebuffer_format, p_format_id, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);
  10. ERR_FAIL_COND_V(pipeline.is_null(), RID());
  11. versions[p_samples] = (Version *)memrealloc(versions[p_samples], sizeof(Version) * (version_count[p_samples] + 1));
  12. versions[p_samples][version_count[p_samples]].format_id = p_format_id;
  13. versions[p_samples][version_count[p_samples]].pipeline = pipeline;
  14. version_count[p_samples]++;
  15. return pipeline;
  16. }
  17. void RenderPipelineVertexFormatCacheRD::_clear() {
  18. for (int v = 0; v < RD::TEXTURE_SAMPLES_MAX; v++) {
  19. if (versions[v]) {
  20. for (uint32_t i = 0; i < version_count[v]; i++) {
  21. //shader may be gone, so this may not be valid
  22. if (RD::get_singleton()->render_pipeline_is_valid(versions[v][i].pipeline)) {
  23. RD::get_singleton()->free(versions[v][i].pipeline);
  24. }
  25. }
  26. version_count[v] = 0;
  27. memfree(versions[v]);
  28. versions[v] = NULL;
  29. }
  30. }
  31. }
  32. void RenderPipelineVertexFormatCacheRD::setup(RID p_shader, RD::FramebufferFormatID p_framebuffer_format, 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) {
  33. ERR_FAIL_COND(p_shader.is_null());
  34. shader = p_shader;
  35. framebuffer_format = p_framebuffer_format;
  36. render_primitive = p_primitive;
  37. rasterization_state = p_rasterization_state;
  38. multisample_state = p_multisample;
  39. depth_stencil_state = p_depth_stencil_state;
  40. blend_state = p_blend_state;
  41. dynamic_state_flags = p_dynamic_state_flags;
  42. }
  43. void RenderPipelineVertexFormatCacheRD::update_shader(RID p_shader) {
  44. ERR_FAIL_COND(p_shader.is_null());
  45. _clear();
  46. setup(p_shader, framebuffer_format, render_primitive, rasterization_state, multisample_state, depth_stencil_state, blend_state, dynamic_state_flags);
  47. }
  48. RenderPipelineVertexFormatCacheRD::RenderPipelineVertexFormatCacheRD() {
  49. for (int i = 0; i < RD::TEXTURE_SAMPLES_MAX; i++) {
  50. version_count[i] = 0;
  51. versions[i] = NULL;
  52. }
  53. }
  54. RenderPipelineVertexFormatCacheRD::~RenderPipelineVertexFormatCacheRD() {
  55. _clear();
  56. }