Overte C++ Documentation
gpu/src/gpu/Framebuffer.h
1 //
2 // Framebuffer.h
3 // libraries/gpu/src/gpu
4 //
5 // Created by Sam Gateau on 4/12/2015.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_Framebuffer_h
12 #define hifi_gpu_Framebuffer_h
13 
14 #include "Texture.h"
15 #include "ResourceSwapChain.h"
16 #include <memory>
17 
18 class Transform; // Texcood transform util
19 
20 namespace gpu {
21 
22 typedef Element Format;
23 
24 class Swapchain {
25 public:
26  // Properties
27  uint16 getWidth() const { return _width; }
28  uint16 getHeight() const { return _height; }
29  uint16 getNumSamples() const { return _numSamples; }
30 
31  bool hasDepthStencil() const { return _hasDepthStencil; }
32  bool isFullscreen() const { return _isFullscreen; }
33 
34  uint32 getSwapInterval() const { return _swapInterval; }
35 
36  bool isStereo() const { return _isStereo; }
37 
38  uint32 getFrameCount() const { return _frameCount; }
39 
40  // Pure interface
41  void setSwapInterval(uint32 interval);
42 
43  void resize(uint16 width, uint16 height);
44  void setFullscreen(bool fullscreen);
45 
46  Swapchain() {}
47  Swapchain(const Swapchain& swapchain) {}
48  virtual ~Swapchain() {}
49 
50 protected:
51  mutable uint32 _frameCount = 0;
52 
53  Format _colorFormat;
54  uint16 _width = 1;
55  uint16 _height = 1;
56  uint16 _numSamples = 1;
57  uint16 _swapInterval = 0;
58 
59  bool _hasDepthStencil = false;
60  bool _isFullscreen = false;
61  bool _isStereo = false;
62 
63  // Non exposed
64 
65  friend class Framebuffer;
66 };
67 typedef std::shared_ptr<Swapchain> SwapchainPointer;
68 
69 
70 class Framebuffer {
71 public:
72  enum BufferMask {
73  BUFFER_COLOR0 = 1,
74  BUFFER_COLOR1 = 2,
75  BUFFER_COLOR2 = 4,
76  BUFFER_COLOR3 = 8,
77  BUFFER_COLOR4 = 16,
78  BUFFER_COLOR5 = 32,
79  BUFFER_COLOR6 = 64,
80  BUFFER_COLOR7 = 128,
81  BUFFER_COLORS = 0x000000FF,
82 
83  BUFFER_DEPTH = 0x40000000,
84  BUFFER_STENCIL = 0x80000000,
85  BUFFER_DEPTHSTENCIL = 0xC0000000,
86  };
87  typedef uint32 Masks;
88 
89  ~Framebuffer();
90 
91  static Framebuffer* create(const SwapchainPointer& swapchain);
92  static Framebuffer* create(const std::string& name);
93  static Framebuffer* create(const std::string& name, const Format& colorBufferFormat, uint16 width, uint16 height);
94  static Framebuffer* create(const std::string& name, const Format& colorBufferFormat, const Format& depthStencilBufferFormat, uint16 width, uint16 height);
95  static Framebuffer* createShadowmap(uint16 width);
96 
97  bool isSwapchain() const;
98  const SwapchainPointer& getSwapchain() const { return _swapchain; }
99 
100  uint32 getFrameCount() const;
101 
102  // Render buffers
103  void removeRenderBuffers();
104  uint32 getNumRenderBuffers() const;
105  const TextureViews& getRenderBuffers() const { return _renderBuffers; }
106 
107  int32 setRenderBuffer(uint32 slot, const TexturePointer& texture, uint32 subresource = 0);
108  const TexturePointer& getRenderBuffer(uint32 slot) const;
109  uint32 getRenderBufferSubresource(uint32 slot) const;
110 
111  bool setDepthBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
112  bool setStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
113  bool setDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
114  const TexturePointer& getDepthStencilBuffer() const;
115  uint32 getDepthStencilBufferSubresource() const;
116  Format getDepthStencilBufferFormat() const;
117 
118 
119  // Properties
120  Masks getBufferMask() const { return _bufferMask; }
121  bool isEmpty() const { return (_bufferMask == 0); }
122  bool hasColor() const { return (getBufferMask() & BUFFER_COLORS); }
123  bool hasDepthStencil() const { return (getBufferMask() & BUFFER_DEPTHSTENCIL); }
124  bool hasDepth() const { return (getBufferMask() & BUFFER_DEPTH); }
125  bool hasStencil() const { return (getBufferMask() & BUFFER_STENCIL); }
126 
127  bool validateTargetCompatibility(const Texture& texture, uint32 subresource = 0) const;
128 
129  Vec2u getSize() const { return Vec2u(getWidth(), getHeight()); }
130  uint16 getWidth() const;
131  uint16 getHeight() const;
132  uint16 getNumSamples() const;
133  const std::string& getName() const { return _name; }
134  void setName(const std::string& name) { _name = name; }
135 
136  float getAspectRatio() const { return getWidth() / (float) getHeight() ; }
137 
138 #if !defined(Q_OS_ANDROID)
139  static const uint32 MAX_NUM_RENDER_BUFFERS = 8;
140 #else
141  static const uint32 MAX_NUM_RENDER_BUFFERS = 4;
142 #endif
143  static uint32 getMaxNumRenderBuffers() { return MAX_NUM_RENDER_BUFFERS; }
144 
145  const GPUObjectPointer gpuObject {};
146 
147  Stamp getDepthStamp() const { return _depthStamp; }
148  const std::vector<Stamp>& getColorStamps() const { return _colorStamps; }
149 
150  static glm::vec4 evalSubregionTexcoordTransformCoefficients(const glm::ivec2& sourceSurface, const glm::ivec2& destRegionSize, const glm::ivec2& destRegionOffset = glm::ivec2(0));
151  static glm::vec4 evalSubregionTexcoordTransformCoefficients(const glm::ivec2& sourceSurface, const glm::ivec4& destViewport);
152 
153  static Transform evalSubregionTexcoordTransform(const glm::ivec2& sourceSurface, const glm::ivec2& destRegionSize, const glm::ivec2& destRegionOffset = glm::ivec2(0));
154  static Transform evalSubregionTexcoordTransform(const glm::ivec2& sourceSurface, const glm::ivec4& destViewport);
155 
156 protected:
157  std::string _name;
158  SwapchainPointer _swapchain;
159 
160  Stamp _depthStamp { 0 };
161  std::vector<Stamp> _colorStamps;
162  TextureViews _renderBuffers;
163  TextureView _depthStencilBuffer;
164 
165  Masks _bufferMask = 0;
166 
167  uint32 _frameCount = 0;
168 
169  uint16 _width = 0;
170  uint16 _height = 0;
171  uint16 _numSamples = 0;
172 
173  void updateSize(const TexturePointer& texture);
174  bool assignDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource);
175 
176  friend class Serializer;
177  friend class Deserializer;
178  // Non exposed
179  Framebuffer(const Framebuffer& framebuffer) = delete;
180  Framebuffer() {}
181 };
182 typedef std::shared_ptr<Framebuffer> FramebufferPointer;
183 typedef ResourceSwapChain<Framebuffer> FramebufferSwapChain;
184 typedef std::shared_ptr<FramebufferSwapChain> FramebufferSwapChainPointer;
185 
186 }
187 
188 #endif
A simple object wrapper for an OpenGL texture.
Definition: material-networking/src/material-networking/TextureCache.h:39