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 
27 class Framebuffer {
28 public:
32  enum BufferMask {
33  BUFFER_COLOR0 = 1,
34  BUFFER_COLOR1 = 2,
35  BUFFER_COLOR2 = 4,
36  BUFFER_COLOR3 = 8,
37  BUFFER_COLOR4 = 16,
38  BUFFER_COLOR5 = 32,
39  BUFFER_COLOR6 = 64,
40  BUFFER_COLOR7 = 128,
41  BUFFER_COLORS = 0x000000FF,
42 
43  BUFFER_DEPTH = 0x40000000,
44  BUFFER_STENCIL = 0x80000000,
45  BUFFER_DEPTHSTENCIL = 0xC0000000,
46  };
47  typedef uint32 Masks;
48 
49  ~Framebuffer();
50 
56  static Framebuffer* create(const std::string& name);
57 
67  static Framebuffer* create(const std::string& name, const Format& colorBufferFormat, uint16 width, uint16 height);
68 
79  static Framebuffer* create(const std::string& name, const Format& colorBufferFormat, const Format& depthStencilBufferFormat, uint16 width, uint16 height);
80 
81  // Render buffers
85  void removeRenderBuffers();
86 
90  uint32 getNumRenderBuffers() const;
91 
95  const TextureViews& getRenderBuffers() const { return _renderBuffers; }
96 
105  int32 setRenderBuffer(uint32 slot, const TexturePointer& texture, uint32 subresource = 0);
106 
113  const TexturePointer& getRenderBuffer(uint32 slot) const;
114 
120  uint32 getRenderBufferSubresource(uint32 slot) const;
121 
130  bool setDepthBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
131 
140  bool setStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
141 
150  bool setDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0);
151 
155  const TexturePointer& getDepthStencilBuffer() const;
156 
160  uint32 getDepthStencilBufferSubresource() const;
161 
166 
167 
168  // Properties
174  Masks getBufferMask() const { return _bufferMask; }
175 
179  bool isEmpty() const { return (_bufferMask == 0); }
180 
184  bool hasColor() const { return (getBufferMask() & BUFFER_COLORS); }
185 
189  bool hasDepthStencil() const { return (getBufferMask() & BUFFER_DEPTHSTENCIL); }
190 
194  bool hasDepth() const { return (getBufferMask() & BUFFER_DEPTH); }
195 
199  bool hasStencil() const { return (getBufferMask() & BUFFER_STENCIL); }
200 
210  bool validateTargetCompatibility(const Texture& texture, uint32 subresource = 0) const;
211 
215  Vec2u getSize() const { return Vec2u(getWidth(), getHeight()); }
216 
220  uint16 getWidth() const;
221 
225  uint16 getHeight() const;
226 
230  uint16 getNumSamples() const;
231 
235  const std::string& getName() const { return _name; }
236 
242  void setName(const std::string& name) { _name = name; }
243 
247  float getAspectRatio() const { return getWidth() / (float) getHeight() ; }
248 
249 #if !defined(Q_OS_ANDROID)
250  static const uint32 MAX_NUM_RENDER_BUFFERS = 8;
251 #else
252  static const uint32 MAX_NUM_RENDER_BUFFERS = 4;
253 #endif
257  static uint32 getMaxNumRenderBuffers() { return MAX_NUM_RENDER_BUFFERS; }
258 
263 
269  Stamp getDepthStamp() const { return _depthStamp; }
270 
276  const std::vector<Stamp>& getColorStamps() const { return _colorStamps; }
277 
286  static glm::vec4 evalSubregionTexcoordTransformCoefficients(const glm::ivec2& sourceSurface, const glm::ivec2& destRegionSize, const glm::ivec2& destRegionOffset = glm::ivec2(0));
287 
295  static glm::vec4 evalSubregionTexcoordTransformCoefficients(const glm::ivec2& sourceSurface, const glm::ivec4& destViewport);
296 
305  static Transform evalSubregionTexcoordTransform(const glm::ivec2& sourceSurface, const glm::ivec2& destRegionSize, const glm::ivec2& destRegionOffset = glm::ivec2(0));
306 
314  static Transform evalSubregionTexcoordTransform(const glm::ivec2& sourceSurface, const glm::ivec4& destViewport);
315 
316 protected:
318  std::string _name;
319 
322  Stamp _depthStamp { 0 };
323 
326  std::vector<Stamp> _colorStamps;
327 
329  TextureViews _renderBuffers;
330 
332  TextureView _depthStencilBuffer;
333 
335  Masks _bufferMask = 0;
336 
338  uint16 _width = 0;
339 
341  uint16 _height = 0;
342 
344  uint16 _numSamples = 0;
345 
352  void updateSize(const TexturePointer& texture);
353 
362  bool assignDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource);
363 
364  friend class Serializer;
365  friend class Deserializer;
366  // Non exposed
367  Framebuffer(const Framebuffer& framebuffer) = delete;
368  Framebuffer() {}
369 };
370 typedef std::shared_ptr<Framebuffer> FramebufferPointer;
371 typedef ResourceSwapChain<Framebuffer> FramebufferSwapChain;
372 typedef std::shared_ptr<FramebufferSwapChain> FramebufferSwapChainPointer;
373 
374 }
375 
376 #endif
Definition: Format.h:295
Definition: gpu/src/gpu/Framebuffer.h:27
Stamp _depthStamp
Definition: gpu/src/gpu/Framebuffer.h:322
bool validateTargetCompatibility(const Texture &texture, uint32 subresource=0) const
Checks if a texture is compatible with being a render attachment.
Definition: gpu/src/gpu/Framebuffer.cpp:55
void setName(const std::string &name)
Sets the framebuffer name.
Definition: gpu/src/gpu/Framebuffer.h:242
const TextureViews & getRenderBuffers() const
Definition: gpu/src/gpu/Framebuffer.h:95
std::string _name
Framebuffer name.
Definition: gpu/src/gpu/Framebuffer.h:318
TextureView _depthStencilBuffer
Depth, stencil, or combined depth stencil rneder buffer.
Definition: gpu/src/gpu/Framebuffer.h:332
static glm::vec4 evalSubregionTexcoordTransformCoefficients(const glm::ivec2 &sourceSurface, const glm::ivec2 &destRegionSize, const glm::ivec2 &destRegionOffset=glm::ivec2(0))
Used to get coefficients for mapping one surface to an area on the other surface.
Definition: gpu/src/gpu/Framebuffer.cpp:259
bool isEmpty() const
Definition: gpu/src/gpu/Framebuffer.h:179
const TexturePointer & getDepthStencilBuffer() const
Definition: gpu/src/gpu/Framebuffer.cpp:248
BufferMask
Definition: gpu/src/gpu/Framebuffer.h:32
bool setDepthStencilBuffer(const TexturePointer &texture, const Format &format, uint32 subresource=0)
Sets the combined depth stencil render buffer.
Definition: gpu/src/gpu/Framebuffer.cpp:230
void removeRenderBuffers()
Removes all render buffers from the current framebuffer.
Definition: gpu/src/gpu/Framebuffer.cpp:136
static Framebuffer * create(const std::string &name)
Creates a framebuffer with a given name. Does not create any render buffers.
Definition: gpu/src/gpu/Framebuffer.cpp:24
uint16 _width
Framebuffer width in pixels.
Definition: gpu/src/gpu/Framebuffer.h:338
int32 setRenderBuffer(uint32 slot, const TexturePointer &texture, uint32 subresource=0)
Sets a given render buffer slot in the framebuffer.
Definition: gpu/src/gpu/Framebuffer.cpp:100
float getAspectRatio() const
Definition: gpu/src/gpu/Framebuffer.h:247
uint32 getNumRenderBuffers() const
Definition: gpu/src/gpu/Framebuffer.cpp:148
bool hasDepth() const
Definition: gpu/src/gpu/Framebuffer.h:194
bool hasDepthStencil() const
Definition: gpu/src/gpu/Framebuffer.h:189
bool setDepthBuffer(const TexturePointer &texture, const Format &format, uint32 subresource=0)
Sets depth render buffer.
Definition: gpu/src/gpu/Framebuffer.cpp:198
Vec2u getSize() const
Definition: gpu/src/gpu/Framebuffer.h:215
uint16 _numSamples
Number of samples per pixel. Greater than 1 when MSAA is used.
Definition: gpu/src/gpu/Framebuffer.h:344
bool assignDepthStencilBuffer(const TexturePointer &texture, const Format &format, uint32 subresource)
Sets depth stencil buffer.
Definition: gpu/src/gpu/Framebuffer.cpp:174
Format getDepthStencilBufferFormat() const
Definition: gpu/src/gpu/Framebuffer.cpp:256
uint32 getRenderBufferSubresource(uint32 slot) const
Get subresource index for a give slot.
Definition: gpu/src/gpu/Framebuffer.cpp:166
void updateSize(const TexturePointer &texture)
Copy width, height and sample count from a texture.
Definition: gpu/src/gpu/Framebuffer.cpp:73
bool hasStencil() const
Definition: gpu/src/gpu/Framebuffer.h:199
Stamp getDepthStamp() const
Definition: gpu/src/gpu/Framebuffer.h:269
Masks getBufferMask() const
Definition: gpu/src/gpu/Framebuffer.h:174
const std::string & getName() const
Definition: gpu/src/gpu/Framebuffer.h:235
static Transform evalSubregionTexcoordTransform(const glm::ivec2 &sourceSurface, const glm::ivec2 &destRegionSize, const glm::ivec2 &destRegionOffset=glm::ivec2(0))
Used to get transform for mapping one surface to an area on the other surface.
Definition: gpu/src/gpu/Framebuffer.cpp:271
const std::vector< Stamp > & getColorStamps() const
Definition: gpu/src/gpu/Framebuffer.h:276
TextureViews _renderBuffers
Color render buffers.
Definition: gpu/src/gpu/Framebuffer.h:329
uint16 getHeight() const
Definition: gpu/src/gpu/Framebuffer.cpp:91
bool setStencilBuffer(const TexturePointer &texture, const Format &format, uint32 subresource=0)
Sets stencil render buffer.
Definition: gpu/src/gpu/Framebuffer.cpp:214
static uint32 getMaxNumRenderBuffers()
Definition: gpu/src/gpu/Framebuffer.h:257
bool hasColor() const
Definition: gpu/src/gpu/Framebuffer.h:184
uint16 getNumSamples() const
Definition: gpu/src/gpu/Framebuffer.cpp:95
uint16 _height
Framebuffer height in pixels.
Definition: gpu/src/gpu/Framebuffer.h:341
const GPUObjectPointer gpuObject
Definition: gpu/src/gpu/Framebuffer.h:262
std::vector< Stamp > _colorStamps
Definition: gpu/src/gpu/Framebuffer.h:326
Masks _bufferMask
Bit sum of BufferMask enums. Used to check which buffers are populated.
Definition: gpu/src/gpu/Framebuffer.h:335
uint16 getWidth() const
Definition: gpu/src/gpu/Framebuffer.cpp:87
uint32 getDepthStencilBufferSubresource() const
Definition: gpu/src/gpu/Framebuffer.cpp:252
const TexturePointer & getRenderBuffer(uint32 slot) const
Returns render buffer from particular slot.
Definition: gpu/src/gpu/Framebuffer.cpp:156
Definition: gpu/src/gpu/Forward.h:134