1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "BsGLPrerequisites.h" |
6 | #include "BsGLTexture.h" |
7 | #include "BsGLFrameBufferObject.h" |
8 | #include "Utility/BsModule.h" |
9 | |
10 | #define GL_DEPTH24_STENCIL8_EXT 0x88F0 |
11 | |
12 | namespace bs |
13 | { |
14 | /** @addtogroup GL |
15 | * @{ |
16 | */ |
17 | |
18 | /** |
19 | * OpenGL implementation of a render texture. |
20 | * |
21 | * @note Sim thread only. |
22 | */ |
23 | class GLRenderTexture : public RenderTexture |
24 | { |
25 | public: |
26 | virtual ~GLRenderTexture() { } |
27 | |
28 | protected: |
29 | friend class GLTextureManager; |
30 | |
31 | GLRenderTexture(const RENDER_TEXTURE_DESC& desc); |
32 | |
33 | /** @copydoc RenderTexture::getProperties */ |
34 | const RenderTargetProperties& getPropertiesInternal() const override { return mProperties; } |
35 | |
36 | RenderTextureProperties mProperties; |
37 | }; |
38 | |
39 | namespace ct |
40 | { |
41 | /** |
42 | * OpenGL implementation of a render texture. |
43 | * |
44 | * @note Core thread only. |
45 | */ |
46 | class GLRenderTexture : public RenderTexture |
47 | { |
48 | public: |
49 | GLRenderTexture(const RENDER_TEXTURE_DESC& desc, UINT32 deviceIdx); |
50 | virtual ~GLRenderTexture(); |
51 | |
52 | /** @copydoc RenderTexture::getCustomAttribute */ |
53 | void getCustomAttribute(const String& name, void* data) const override; |
54 | |
55 | protected: |
56 | friend class bs::GLRenderTexture; |
57 | |
58 | /** @copydoc RenderTexture::initialize */ |
59 | void initialize() override; |
60 | |
61 | /** @copydoc RenderTexture::getProperties */ |
62 | const RenderTargetProperties& getPropertiesInternal() const override { return mProperties; } |
63 | |
64 | RenderTextureProperties mProperties; |
65 | GLFrameBufferObject* mFB; |
66 | }; |
67 | |
68 | /** |
69 | * Manager that handles valid render texture formats. |
70 | * |
71 | * @note Must be initialized when RenderSystem is first started. |
72 | */ |
73 | class GLRTTManager : public Module<GLRTTManager> |
74 | { |
75 | public: |
76 | GLRTTManager(); |
77 | ~GLRTTManager(); |
78 | |
79 | /** |
80 | * Check if a certain format is usable as a render target format. |
81 | * |
82 | * @note Thread safe. |
83 | */ |
84 | bool checkFormat(PixelFormat format) const { return mProps[format].valid; } |
85 | |
86 | /** |
87 | * Get the closest supported alternative format. If format is supported, returns format. |
88 | * |
89 | * @note Thread safe. |
90 | */ |
91 | virtual PixelFormat getSupportedAlternative(PixelFormat format); |
92 | |
93 | /** Returns a persistent FBO that is used as a source buffer for blit operations. */ |
94 | GLuint getBlitReadFBO() const { return mBlitReadFBO; } |
95 | |
96 | /** Returns a persistent FBO that is used as a destination buffer for blit operations. */ |
97 | GLuint getBlitDrawFBO() const { return mBlitWriteFBO; } |
98 | private: |
99 | /** Frame buffer object properties for a certain texture format. */ |
100 | struct FormatProperties |
101 | { |
102 | /** Allowed modes/properties for this pixel format. */ |
103 | struct Mode |
104 | { |
105 | UINT32 depth; /**< Depth format (0 = no depth). */ |
106 | UINT32 stencil; /**< Stencil format (0 = no stencil). */ |
107 | }; |
108 | |
109 | Vector<Mode> modes; |
110 | bool valid; |
111 | }; |
112 | |
113 | /** Detect which internal formats are allowed to be used on render target color or depth/stencil surfaces. */ |
114 | void detectFBOFormats(); |
115 | |
116 | /** Checks are the specified depth & stencil formats compatible. */ |
117 | bool _tryFormat(GLenum depthFormat, GLenum stencilFormat); |
118 | |
119 | /** Checks is the specified packed format valid for using in the render target. */ |
120 | bool _tryPackedFormat(GLenum packedFormat); |
121 | |
122 | FormatProperties mProps[PF_COUNT]; |
123 | GLuint mBlitReadFBO; |
124 | GLuint mBlitWriteFBO; |
125 | }; |
126 | } |
127 | /** @} */ |
128 | } |