MTLGfx.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2022 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <Metal/Metal.h>
  5. #include <QuartzCore/QuartzCore.h>
  6. #include "VideoCommon/AbstractGfx.h"
  7. #include "VideoBackends/Metal/MRCHelpers.h"
  8. namespace Metal
  9. {
  10. class Framebuffer;
  11. class Texture;
  12. class Gfx final : public ::AbstractGfx
  13. {
  14. public:
  15. Gfx(MRCOwned<CAMetalLayer*> layer);
  16. ~Gfx() override;
  17. bool IsHeadless() const override;
  18. std::unique_ptr<AbstractTexture> CreateTexture(const TextureConfig& config,
  19. std::string_view name) override;
  20. std::unique_ptr<AbstractStagingTexture>
  21. CreateStagingTexture(StagingTextureType type, const TextureConfig& config) override;
  22. std::unique_ptr<AbstractFramebuffer>
  23. CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
  24. std::vector<AbstractTexture*> additional_color_attachments) override;
  25. std::unique_ptr<AbstractShader> CreateShaderFromSource(ShaderStage stage, std::string_view source,
  26. std::string_view name) override;
  27. std::unique_ptr<AbstractShader> CreateShaderFromBinary(ShaderStage stage, const void* data,
  28. size_t length,
  29. std::string_view name) override;
  30. std::unique_ptr<AbstractShader> CreateShaderFromMSL(ShaderStage stage, std::string msl,
  31. std::string_view glsl, std::string_view name);
  32. std::unique_ptr<NativeVertexFormat>
  33. CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl) override;
  34. std::unique_ptr<AbstractPipeline> CreatePipeline(const AbstractPipelineConfig& config,
  35. const void* cache_data = nullptr,
  36. size_t cache_data_length = 0) override;
  37. void Flush() override;
  38. void WaitForGPUIdle() override;
  39. void OnConfigChanged(u32 bits) override;
  40. void ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool color_enable, bool alpha_enable,
  41. bool z_enable, u32 color, u32 z) override;
  42. void SetPipeline(const AbstractPipeline* pipeline) override;
  43. void SetFramebuffer(AbstractFramebuffer* framebuffer) override;
  44. void SetAndDiscardFramebuffer(AbstractFramebuffer* framebuffer) override;
  45. void SetAndClearFramebuffer(AbstractFramebuffer* framebuffer, const ClearColor& color_value = {},
  46. float depth_value = 0.0f) override;
  47. void SetScissorRect(const MathUtil::Rectangle<int>& rc) override;
  48. void SetTexture(u32 index, const AbstractTexture* texture) override;
  49. void SetSamplerState(u32 index, const SamplerState& state) override;
  50. void SetComputeImageTexture(u32 index, AbstractTexture* texture, bool read, bool write) override;
  51. void UnbindTexture(const AbstractTexture* texture) override;
  52. void SetViewport(float x, float y, float width, float height, float near_depth,
  53. float far_depth) override;
  54. void Draw(u32 base_vertex, u32 num_vertices) override;
  55. void DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex) override;
  56. void DispatchComputeShader(const AbstractShader* shader, u32 groupsize_x, u32 groupsize_y,
  57. u32 groupsize_z, u32 groups_x, u32 groups_y, u32 groups_z) override;
  58. bool BindBackbuffer(const ClearColor& clear_color = {}) override;
  59. void PresentBackbuffer() override;
  60. SurfaceInfo GetSurfaceInfo() const override;
  61. private:
  62. MRCOwned<CAMetalLayer*> m_layer;
  63. MRCOwned<id<CAMetalDrawable>> m_drawable;
  64. std::unique_ptr<Texture> m_bb_texture;
  65. std::unique_ptr<Framebuffer> m_backbuffer;
  66. u32 m_texture_counter = 0;
  67. u32 m_staging_texture_counter = 0;
  68. std::array<u32, 4> m_shader_counter = {};
  69. void CheckForSurfaceChange();
  70. void CheckForSurfaceResize();
  71. void SetupSurface();
  72. };
  73. } // namespace Metal