opengl_texture.h (3825B)
1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0) 3 4 #pragma once 5 6 #include "gpu_device.h" 7 #include "gpu_texture.h" 8 #include "opengl_loader.h" 9 10 #include <tuple> 11 12 class OpenGLDevice; 13 class OpenGLStreamBuffer; 14 15 class OpenGLTexture final : public GPUTexture 16 { 17 friend OpenGLDevice; 18 19 public: 20 OpenGLTexture(const OpenGLTexture&) = delete; 21 ~OpenGLTexture(); 22 23 static bool UseTextureStorage(bool multisampled); 24 static const std::tuple<GLenum, GLenum, GLenum>& GetPixelFormatMapping(Format format, bool gles); 25 26 ALWAYS_INLINE GLuint GetGLId() const { return m_id; } 27 28 bool Update(u32 x, u32 y, u32 width, u32 height, const void* data, u32 pitch, u32 layer = 0, u32 level = 0) override; 29 bool Map(void** map, u32* map_stride, u32 x, u32 y, u32 width, u32 height, u32 layer = 0, u32 level = 0) override; 30 void Unmap() override; 31 32 void SetDebugName(std::string_view name) override; 33 34 static std::unique_ptr<OpenGLTexture> Create(u32 width, u32 height, u32 layers, u32 levels, u32 samples, Type type, 35 Format format, const void* data = nullptr, u32 data_pitch = 0); 36 37 bool UseTextureStorage() const; 38 39 ALWAYS_INLINE GLenum GetGLTarget() const 40 { 41 return (IsMultisampled() ? GL_TEXTURE_2D_MULTISAMPLE : (IsTextureArray() ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D)); 42 } 43 44 void CommitClear(); 45 46 OpenGLTexture& operator=(const OpenGLTexture&) = delete; 47 48 private: 49 OpenGLTexture(u32 width, u32 height, u32 layers, u32 levels, u32 samples, Type type, Format format, GLuint id); 50 51 GLuint m_id = 0; 52 53 u32 m_map_offset = 0; 54 u16 m_map_x = 0; 55 u16 m_map_y = 0; 56 u16 m_map_width = 0; 57 u16 m_map_height = 0; 58 u8 m_map_layer = 0; 59 u8 m_map_level = 0; 60 }; 61 62 class OpenGLTextureBuffer final : public GPUTextureBuffer 63 { 64 friend OpenGLDevice; 65 66 public: 67 ~OpenGLTextureBuffer() override; 68 69 ALWAYS_INLINE OpenGLStreamBuffer* GetBuffer() const { return m_buffer.get(); } 70 ALWAYS_INLINE GLuint GetTextureId() const { return m_texture_id; } 71 72 bool CreateBuffer(); 73 74 // Inherited via GPUTextureBuffer 75 void* Map(u32 required_elements) override; 76 void Unmap(u32 used_elements) override; 77 78 void SetDebugName(std::string_view name) override; 79 80 private: 81 OpenGLTextureBuffer(Format format, u32 size_in_elements, std::unique_ptr<OpenGLStreamBuffer> buffer, 82 GLuint texture_id); 83 84 std::unique_ptr<OpenGLStreamBuffer> m_buffer; 85 GLuint m_texture_id; 86 }; 87 88 class OpenGLSampler final : public GPUSampler 89 { 90 friend OpenGLDevice; 91 92 public: 93 ~OpenGLSampler() override; 94 95 ALWAYS_INLINE GLuint GetID() const { return m_id; } 96 97 void SetDebugName(std::string_view name) override; 98 99 private: 100 OpenGLSampler(GLuint id); 101 102 GLuint m_id; 103 }; 104 105 class OpenGLDownloadTexture final : public GPUDownloadTexture 106 { 107 public: 108 ~OpenGLDownloadTexture() override; 109 110 static std::unique_ptr<OpenGLDownloadTexture> Create(u32 width, u32 height, GPUTexture::Format format, void* memory, 111 size_t memory_size, u32 memory_pitch); 112 113 void CopyFromTexture(u32 dst_x, u32 dst_y, GPUTexture* src, u32 src_x, u32 src_y, u32 width, u32 height, 114 u32 src_layer, u32 src_level, bool use_transfer_pitch) override; 115 116 bool Map(u32 x, u32 y, u32 width, u32 height) override; 117 void Unmap() override; 118 119 void Flush() override; 120 121 void SetDebugName(std::string_view name) override; 122 123 private: 124 OpenGLDownloadTexture(u32 width, u32 height, GPUTexture::Format format, bool imported, GLuint buffer_id, 125 u8* cpu_buffer, u32 buffer_size, const u8* map_ptr, u32 map_pitch); 126 127 GLuint m_buffer_id = 0; 128 u32 m_buffer_size = 0; 129 130 GLsync m_sync = {}; 131 132 // used when buffer storage is not available 133 u8* m_cpu_buffer = nullptr; 134 };