Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DxRenderTarget.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Exception.h"
27 #include "cinder/dx/dx.h"
28 #include "cinder/dx/DxTexture.h"
29 
30 namespace cinder { namespace dx {
31 
33 typedef std::shared_ptr<RenderTarget> RenderTargetRef;
34 
36 //class Renderbuffer {
37 // public:
38 // //! Creates a NULL Renderbuffer
39 // Renderbuffer() {}
40 // //! Create a Renderbuffer \a width pixels wide and \a heigh pixels high, with an internal format of \a internalFormat, defaulting to GL_RGBA8
41 // Renderbuffer( int width, int height, DXGI_FORMAT internalFormat = DXGI_FORMAT_R8G8B8A8_UNORM );
42 // //! Create a Renderbuffer \a width pixels wide and \a heigh pixels high, with an internal format of \a internalFormat, defaulting to GL_RGBA8, MSAA samples \a msaaSamples, and CSAA samples \a coverageSamples
43 // Renderbuffer( int width, int height, DXGI_FORMAT internalFormat, int msaaSamples, int coverageSamples = 0 );
44 //
45 // //! Returns the width of the Renderbuffer in pixels
46 // int getWidth() const { return mObj->mWidth; }
47 // //! Returns the height of the Renderbuffer in pixels
48 // int getHeight() const { return mObj->mHeight; }
49 // //! Returns the size of the Renderbuffer in pixels
50 // Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
51 // //! Returns the bounding area of the Renderbuffer in pixels
52 // Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
53 // //! Returns the aspect ratio of the Renderbuffer
54 // float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
55 //
56 // //! Returns the ID of the Renderbuffer
57 // GLuint getId() const { return mObj->mId; }
58 // //! Returns the internal format of the Renderbuffer
59 // DXGI_FORMAT getInternalFormat() const { return mObj->mInternalFormat; }
60 // //! Returns the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampling
61 // int getSamples() const { return mObj->mSamples; }
62 // //! Returns the number of coverage samples used in CSAA-style antialiasing. Defaults to none.
63 // int getCoverageSamples() const { return mObj->mCoverageSamples; }
64 //
65 // private:
66 // struct Obj {
67 // Obj();
68 // Obj( int aWidth, int aHeight, DXGI_FORMAT internalFormat, int msaaSamples, int coverageSamples );
69 // ~Obj();
70 //
71 // int mWidth, mHeight;
72 // GLuint mId;
73 // DXGI_FORMAT mInternalFormat;
74 // int mSamples, mCoverageSamples;
75 // };
76 //
77 // std::shared_ptr<Obj> mObj;
78 //
79 // public:
80 // //@{
81 // //! Emulates shared_ptr-like behavior
82 // typedef std::shared_ptr<Obj> Renderbuffer::*unspecified_bool_type;
83 // operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &Renderbuffer::mObj; }
84 // void reset() { mObj.reset(); }
85 // //@}
86 //};
87 
89 class RenderTarget {
90  public:
91  struct Format;
92 
96  RenderTarget( int width, int height, Format format = Format() );
98  RenderTarget( int width, int height, bool alpha, bool color = true, bool depth = true );
99 
101  static RenderTargetRef create( int width, int height, Format format = Format() );
103  static RenderTargetRef create( int width, int height, bool alpha, bool color = true, bool depth = true );
104 
106  int getWidth() const { return mObj->mWidth; }
108  int getHeight() const { return mObj->mHeight; }
110  Vec2i getSize() const { return Vec2i( mObj->mWidth, mObj->mHeight ); }
112  Area getBounds() const { return Area( 0, 0, mObj->mWidth, mObj->mHeight ); }
114  float getAspectRatio() const { return mObj->mWidth / (float)mObj->mHeight; }
116  const Format& getFormat() const { return mObj->mFormat; }
118  //GLenum getTarget() const { return mObj->mFormat.mTarget; }
119 
121  //ID3D11Texture2D*& getTexture( int attachment = 0 );
125 
127  void bindTexture( int textureUnit = 0, int attachment = 0 );
129  void unbindTexture();
131  void bindDepthTexture( int textureUnit = 0 );
133  void bindFramebuffer();
135  static void unbindFramebuffer();
136 
138  //ID3D11RenderTargetView* getId() const { return mObj->mId; }
139 
141  //GLuint getResolveId() const { if( mObj->mResolveFramebufferId ) return mObj->mResolveFramebufferId; else return mObj->mId; }
142 
144  void blitTo( RenderTarget dst, const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT ) const;
146  void blitToScreen( const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT ) const;
148  void blitFromScreen( const Area &srcArea, const Area &dstArea, GLenum filter = GL_NEAREST, GLbitfield mask = GL_COLOR_BUFFER_BIT );
149 
151  //static int getMaxSamples();
153  static int getMaxAttachments();
154 
155  struct Format {
156  public:
158  Format();
159 
161  //void setTarget( GLenum target ) { mTarget = target; }
163  void setColorInternalFormat( DXGI_FORMAT colorInternalFormat ) { mColorInternalFormat = colorInternalFormat; }
165  void setDepthInternalFormat( DXGI_FORMAT depthInternalFormat ) { mDepthInternalFormat = depthInternalFormat; }
167  void setSamples( int samples ) { mSamples = samples; }
171  void enableColorBuffer( bool colorBuffer = true, int numColorBuffers = 1 );
173  void enableDepthBuffer( bool depthBuffer = true, bool asTexture = true );
174 // void enableStencilBuffer( bool stencilBuffer = true ) { mStencilBuffer = stencilBuffer; }
177 
179  void setWrap( D3D11_TEXTURE_ADDRESS_MODE wrapS, D3D11_TEXTURE_ADDRESS_MODE wrapT ) { setWrapS( wrapS ); setWrapT( wrapT ); }
182  void setWrapS( D3D11_TEXTURE_ADDRESS_MODE wrapS ) { mWrapS = wrapS; }
185  void setWrapT( D3D11_TEXTURE_ADDRESS_MODE wrapT ) { mWrapT = wrapT; }
188  void setFilter( D3D11_FILTER filter ) { mFilter = filter; }
189 
191  //GLenum getTarget() const { return mTarget; }
193  DXGI_FORMAT getColorInternalFormat() const { return mColorInternalFormat; }
195  DXGI_FORMAT getDepthInternalFormat() const { return mDepthInternalFormat; }
197  int getSamples() const { return mSamples; }
199  int getCoverageSamples() const { return mCoverageSamples; }
201  bool hasColorBuffer() const { return mNumColorBuffers > 0; }
203  int getNumColorBuffers() const { return mNumColorBuffers; }
205  bool hasDepthBuffer() const { return mDepthBuffer; }
207  //bool hasDepthBufferTexture() const { return mDepthBufferAsTexture; }
208 // bool hasStencilBuffer() const { return mStencilBuffer; }
210  bool hasMipMapping() const { return mMipmapping; }
211 
212  protected:
213  //GLenum mTarget;
215  int mSamples;
218  //bool mDepthBuffer, mDepthBufferAsTexture, mStencilBuffer;
221  D3D11_TEXTURE_ADDRESS_MODE mWrapS, mWrapT;
222  D3D11_FILTER mFilter;
223 
224  friend class RenderTarget;
225  };
226 
227  protected:
228  void init();
229  bool initMultisample( bool csaa );
230  void resolveTextures() const;
231  void updateMipmaps( bool bindFirst, int attachment ) const;
233 
234  struct Obj {
235  Obj();
236  Obj( int aWidth, int aHeight );
237  ~Obj();
238 
241  //ID3D11RenderTargetView* mId;
242  //ID3D11RenderTargetView* mResolveFramebufferId;
243  //std::vector<Renderbuffer> mMultisampleColorRenderbuffers;
244  //Renderbuffer mMultisampleDepthRenderbuffer;
245 
246  std::vector<ID3D11RenderTargetView*> mRenderTargets;
247  //std::vector<ID3D11Texture2D*> mColorTextures;
248  //std::vector<ID3D11ShaderResourceView*> mColorSRVs;
249  std::vector<dx::TextureRef> mColorTextures;
250  //ID3D11Texture2D* mDepthTexture;
252  ID3D11DepthStencilView* mDepthView;
253  //Renderbuffer mDepthRenderbuffer;
255  };
256 
257  std::shared_ptr<Obj> mObj;
258 
261 
262  public:
264  typedef std::shared_ptr<Obj> RenderTarget::*unspecified_bool_type;
266  operator unspecified_bool_type() const { return ( mObj.get() == 0 ) ? 0 : &RenderTarget::mObj; }
267  void reset() { mObj.reset(); }
269 };
270 
272 };
273 
275  public:
277  RenderTargetExceptionInvalidSpecification( const std::string &message ) throw();
278 
279  virtual const char * what() const throw() { return mMessage; }
280 
281  private:
282  char mMessage[256];
283 };
284 
285 } } // namespace cinder::dx
bool hasDepthBuffer() const
Returns whether the FBO contains a depth buffer.
Definition: DxRenderTarget.h:205
void reset()
Emulates shared_ptr-like behavior.
Definition: DxRenderTarget.h:267
static RenderTargetRef create(int width, int height, Format format=Format())
Creates an FBO width pixels wide and height pixels high, using Fbo::Format format.
Definition: DxRenderTarget.cpp:202
int mCoverageSamples
Definition: DxRenderTarget.h:216
void blitTo(RenderTarget dst, const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT) const
Returns the ID of the framebuffer itself. For antialiased FBOs this is the ID of the output multisamp...
Definition: DxRenderTarget.cpp:622
static int getMaxAttachments()
Returns the maximum number of color attachments the graphics card is capable of using for an Fbo...
Definition: DxRenderTarget.cpp:613
float getAspectRatio() const
Returns the aspect ratio of the FBO.
Definition: DxRenderTarget.h:114
DXGI_FORMAT getColorInternalFormat() const
Returns the texture target associated with the FBO.
Definition: DxRenderTarget.h:193
dx::TextureRef mDepthTexture
Definition: DxRenderTarget.h:251
Definition: Area.h:37
GLsizei const GLchar ** string
Definition: GLee.h:2427
bool checkStatus(class RenderTargetExceptionInvalidSpecification *resultExc)
Definition: DxRenderTarget.cpp:564
int mWidth
Definition: DxRenderTarget.h:239
virtual const char * what() const
Definition: DxRenderTarget.h:279
void setWrapS(D3D11_TEXTURE_ADDRESS_MODE wrapS)
Sets the horizontal wrapping behavior for the FBO's textures. Default is GL_CLAMP_TO_EDGE. Possible values are GL_CLAMP, GL_REPEAT and GL_CLAMP_TO_EDGE.
Definition: DxRenderTarget.h:182
GLenum GLsizei width
Definition: GLee.h:969
void unbindTexture()
Unbinds the texture associated with an Fbo's target.
Definition: DxRenderTarget.cpp:483
void setColorInternalFormat(DXGI_FORMAT colorInternalFormat)
Set the texture target associated with the FBO. Defaults to GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB i...
Definition: DxRenderTarget.h:163
std::shared_ptr< Texture > TextureRef
Definition: dx.h:61
D3D11_TEXTURE_ADDRESS_MODE mWrapS
Definition: DxRenderTarget.h:221
#define GL_NEAREST
Definition: gldx.h:933
void blitToScreen(const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT) const
Copies to the screen from Area srcArea to dstArea using filter filter. mask allows specification of c...
Definition: DxRenderTarget.cpp:631
Format()
Default constructor, sets the target to GL_TEXTURE_2D with an 8-bit color+alpha, a 24-bit depth textu...
Definition: DxRenderTarget.cpp:145
bool mNeedsResolve
Definition: DxRenderTarget.h:254
void enableDepthBuffer(bool depthBuffer=true, bool asTexture=true)
Enables or disables the creation of a depth buffer for the FBO. If asTexture the depth buffer is crea...
Definition: DxRenderTarget.cpp:172
int mNumColorBuffers
Definition: DxRenderTarget.h:220
bool hasColorBuffer() const
Returns whether the FBO contains a color buffer.
Definition: DxRenderTarget.h:201
int getSamples() const
Returns the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampl...
Definition: DxRenderTarget.h:197
RenderTargetExceptionInvalidSpecification()
Definition: DxRenderTarget.h:276
void resolveTextures() const
Definition: DxRenderTarget.cpp:494
GLsizei coverageSamples
Definition: GLee.h:13032
#define GL_COLOR_BUFFER_BIT
Definition: gldx.h:99
dx::TextureRef getDepthTexture()
Returns a reference to the depth texture of the FBO.
Definition: DxRenderTarget.cpp:468
void updateMipmaps(bool bindFirst, int attachment) const
Definition: DxRenderTarget.cpp:526
bool hasMipMapping() const
Returns whether the contents of the FBO textures are mip-mapped.
Definition: DxRenderTarget.h:210
DXGI_FORMAT mDepthInternalFormat
Definition: DxRenderTarget.h:214
const Format & getFormat() const
Returns the Fbo::Format of this FBO.
Definition: DxRenderTarget.h:116
D3D11_TEXTURE_ADDRESS_MODE mWrapT
Definition: DxRenderTarget.h:221
void setCoverageSamples(int coverageSamples)
Sets the number of coverage samples used in CSAA-style antialiasing. Defaults to none. Note that not all implementations support CSAA, and is currenlty Windows-only Nvidia. Ignored on OpenGL ES.
Definition: DxRenderTarget.h:169
RenderTarget()
Creates a NULL FBO.
Definition: DxRenderTarget.h:94
void setFilter(D3D11_FILTER filter)
Sets the minification filtering behavior for the FBO's textures. Default is GL_LINEAR: Possible value...
Definition: DxRenderTarget.h:188
void bindDepthTexture(int textureUnit=0)
Binds the depth texture associated with an Fbo to its target.
Definition: DxRenderTarget.cpp:488
std::vector< dx::TextureRef > mColorTextures
Definition: DxRenderTarget.h:249
static GLint sMaxSamples
Definition: DxRenderTarget.h:259
static void unbindFramebuffer()
Unbinds the Fbo as the currently active framebuffer, restoring the primary context as the target for ...
Definition: DxRenderTarget.cpp:558
GLenum GLsizei GLsizei height
Definition: GLee.h:1029
Definition: dx.h:342
GLclampf GLclampf GLclampf alpha
Definition: GLee.h:951
std::shared_ptr< Obj > RenderTarget::* unspecified_bool_type
Emulates shared_ptr-like behavior.
Definition: DxRenderTarget.h:265
void blitFromScreen(const Area &srcArea, const Area &dstArea, GLenum filter=GL_NEAREST, GLbitfield mask=GL_COLOR_BUFFER_BIT)
Copies from the screen from Area srcArea to dstArea using filter filter. mask allows specification of...
Definition: DxRenderTarget.cpp:640
dx::TextureRef getTexture(int attachment=0)
Returns the texture target for this FBO. Typically GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE_ARB.
Definition: DxRenderTarget.cpp:460
Definition: DxRenderTarget.h:271
Obj()
Definition: DxRenderTarget.cpp:111
void setWrap(D3D11_TEXTURE_ADDRESS_MODE wrapS, D3D11_TEXTURE_ADDRESS_MODE wrapT)
Sets the wrapping behavior for the FBO's textures. Possible values are GL_CLAMP, GL_REPEAT and GL_CLA...
Definition: DxRenderTarget.h:179
void bindTexture(int textureUnit=0, int attachment=0)
Binds the color texture associated with an Fbo to its target. Optionally binds to a multitexturing un...
Definition: DxRenderTarget.cpp:473
void setSamples(int samples)
Sets the number of samples used in MSAA-style antialiasing. Defaults to none, disabling multisampling...
Definition: DxRenderTarget.h:167
D3D11_FILTER mFilter
Definition: DxRenderTarget.h:222
ID3D11DepthStencilView * mDepthView
Definition: DxRenderTarget.h:252
bool mNeedsMipmapUpdate
Definition: DxRenderTarget.h:254
int getCoverageSamples() const
Returns the number of coverage samples used in CSAA-style antialiasing. Defaults to none...
Definition: DxRenderTarget.h:199
void setDepthInternalFormat(DXGI_FORMAT depthInternalFormat)
Sets the GL internal format for the depth buffer. Defaults to GL_DEPTH_COMPONENT24. Common options also include GL_DEPTH_COMPONENT16 and GL_DEPTH_COMPONENT32.
Definition: DxRenderTarget.h:165
GLsizei samples
Definition: GLee.h:5425
DXGI_FORMAT getDepthInternalFormat() const
Returns the GL internal format for the depth buffer. Defaults to GL_DEPTH_COMPONENT24.
Definition: DxRenderTarget.h:195
int mHeight
Definition: DxRenderTarget.h:239
bool mDepthBuffer
Definition: DxRenderTarget.h:219
static GLint sMaxAttachments
Definition: DxRenderTarget.h:260
Definition: DxRenderTarget.h:234
void enableMipmapping(bool enableMipmapping=true)
Enables or disables mip-mapping for the FBO's textures.
Definition: DxRenderTarget.h:176
~Obj()
Definition: DxRenderTarget.cpp:124
int int int * dx
Definition: GLee.h:17162
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: GLee.h:5419
std::vector< ID3D11RenderTargetView * > mRenderTargets
Definition: DxRenderTarget.h:246
Definition: DxRenderTarget.h:155
Format mFormat
Definition: DxRenderTarget.h:240
int GLint
Definition: gldx.h:51
int mSamples
Definition: DxRenderTarget.h:215
GLenum GLsizei GLenum format
Definition: GLee.h:969
GLuint dst
Definition: GLee.h:10536
void enableColorBuffer(bool colorBuffer=true, int numColorBuffers=1)
Enables or disables the creation of a color buffer for the FBO.. Creates multiple color attachments w...
Definition: DxRenderTarget.cpp:162
int getHeight() const
Returns the height of the FBO in pixels.
Definition: DxRenderTarget.h:108
int getWidth() const
Returns the width of the FBO in pixels.
Definition: DxRenderTarget.h:106
std::shared_ptr< RenderTarget > RenderTargetRef
Definition: DxRenderTarget.h:32
Represents an DirectX RenderTarget object.
Definition: DxRenderTarget.h:89
bool mMipmapping
Definition: DxRenderTarget.h:217
unsigned int GLbitfield
Definition: gldx.h:48
Vec2i getSize() const
Returns the size of the FBO in pixels.
Definition: DxRenderTarget.h:110
GLenum attachment
Definition: GLee.h:5383
GLint GLint GLsizei GLsizei GLsizei depth
Definition: GLee.h:1161
Area getBounds() const
Returns the bounding area of the FBO in pixels.
Definition: DxRenderTarget.h:112
GLenum GLint GLuint mask
Definition: GLee.h:2241
std::shared_ptr< Obj > mObj
Definition: DxRenderTarget.h:257
unsigned int GLenum
Definition: gldx.h:46
void init()
Definition: DxRenderTarget.cpp:214
void bindFramebuffer()
Binds the Fbo as the currently active framebuffer, meaning it will receive the results of all subsequ...
Definition: DxRenderTarget.cpp:543
DXGI_FORMAT mColorInternalFormat
Definition: DxRenderTarget.h:214
void setWrapT(D3D11_TEXTURE_ADDRESS_MODE wrapT)
Sets the vertical wrapping behavior for the FBO's textures. Default is GL_CLAMP_TO_EDGE. Possible values are GL_CLAMP, GL_REPEAT and GL_CLAMP_TO_EDGE.
Definition: DxRenderTarget.h:185
Vec2< int > Vec2i
Definition: Vector.h:1313
int getNumColorBuffers() const
Returns the number of color buffers.
Definition: DxRenderTarget.h:203
GLuint color
Definition: GLee.h:3198
bool initMultisample(bool csaa)
Definition: DxRenderTarget.cpp:404