gfxGLDevice.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GFXGLDEVICE_H_
  23. #define _GFXGLDEVICE_H_
  24. #include "platform/platform.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "gfx/gfxInit.h"
  27. #ifndef GL_GGL_H
  28. #include "gfx/gl/ggl/ggl.h"
  29. #endif
  30. #include "windowManager/platformWindow.h"
  31. #include "gfx/gfxFence.h"
  32. #include "gfx/gfxResource.h"
  33. #include "gfx/gl/gfxGLStateBlock.h"
  34. class GFXGLVertexBuffer;
  35. class GFXGLPrimitiveBuffer;
  36. class GFXGLTextureTarget;
  37. class GFXGLCubemap;
  38. class GFXGLDevice : public GFXDevice
  39. {
  40. public:
  41. void zombify();
  42. void resurrect();
  43. GFXGLDevice(U32 adapterIndex);
  44. virtual ~GFXGLDevice();
  45. static void enumerateAdapters( Vector<GFXAdapter*> &adapterList );
  46. static GFXDevice *createInstance( U32 adapterIndex );
  47. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL );
  48. virtual void activate() { }
  49. virtual void deactivate() { }
  50. virtual GFXAdapterType getAdapterType() { return OpenGL; }
  51. virtual void enterDebugEvent(ColorI color, const char *name) { }
  52. virtual void leaveDebugEvent() { }
  53. virtual void setDebugMarker(ColorI color, const char *name) { }
  54. virtual void enumerateVideoModes();
  55. virtual U32 getTotalVideoMemory();
  56. virtual GFXCubemap * createCubemap();
  57. virtual F32 getFillConventionOffset() const { return 0.0f; }
  58. ///@}
  59. /// @name Render Target functions
  60. /// @{
  61. ///
  62. virtual GFXTextureTarget *allocRenderToTextureTarget();
  63. virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window);
  64. virtual void _updateRenderTargets();
  65. ///@}
  66. /// @name Shader functions
  67. /// @{
  68. virtual F32 getPixelShaderVersion() const { return mPixelShaderVersion; }
  69. virtual void setPixelShaderVersion( F32 version ) { mPixelShaderVersion = version; }
  70. virtual void setShader(GFXShader* shd);
  71. virtual void disableShaders(); ///< Equivalent to setShader(NULL)
  72. /// @attention GL cannot check if the given format supports blending or filtering!
  73. virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
  74. const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter);
  75. /// Returns the number of texture samplers that can be used in a shader rendering pass
  76. virtual U32 getNumSamplers() const;
  77. /// Returns the number of simultaneous render targets supported by the device.
  78. virtual U32 getNumRenderTargets() const;
  79. virtual GFXShader* createShader();
  80. virtual void clear( U32 flags, ColorI color, F32 z, U32 stencil );
  81. virtual bool beginSceneInternal();
  82. virtual void endSceneInternal();
  83. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
  84. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  85. U32 startVertex,
  86. U32 minIndex,
  87. U32 numVerts,
  88. U32 startIndex,
  89. U32 primitiveCount );
  90. virtual void setClipRect( const RectI &rect );
  91. virtual const RectI &getClipRect() const { return mClip; }
  92. virtual void preDestroy() { Parent::preDestroy(); }
  93. virtual U32 getMaxDynamicVerts() { return MAX_DYNAMIC_VERTS; }
  94. virtual U32 getMaxDynamicIndices() { return MAX_DYNAMIC_INDICES; }
  95. GFXFence *createFence();
  96. GFXOcclusionQuery* createOcclusionQuery();
  97. GFXGLStateBlockRef getCurrentStateBlock() { return mCurrentGLStateBlock; }
  98. virtual void setupGenericShaders( GenericShaderType type = GSColor );
  99. ///
  100. bool supportsAnisotropic() const { return mSupportsAnisotropic; }
  101. protected:
  102. /// Called by GFXDevice to create a device specific stateblock
  103. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  104. /// Called by GFXDevice to actually set a stateblock.
  105. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  106. /// Called by base GFXDevice to actually set a const buffer
  107. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  108. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture);
  109. virtual void setCubemapInternal(U32 cubemap, const GFXGLCubemap* texture);
  110. virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
  111. virtual void setLightMaterialInternal(const GFXLightMaterial mat);
  112. virtual void setGlobalAmbientInternal(ColorF color);
  113. /// @name State Initalization.
  114. /// @{
  115. /// State initalization. This MUST BE CALLED in setVideoMode after the device
  116. /// is created.
  117. virtual void initStates() { }
  118. virtual void setMatrix( GFXMatrixType mtype, const MatrixF &mat );
  119. virtual GFXVertexBuffer *allocVertexBuffer( U32 numVerts,
  120. const GFXVertexFormat *vertexFormat,
  121. U32 vertSize,
  122. GFXBufferType bufferType );
  123. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType );
  124. // NOTE: The GL device doesn't need a vertex declaration at
  125. // this time, but we need to return something to keep the system
  126. // from retrying to allocate one on every call.
  127. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat )
  128. {
  129. static GFXVertexDecl decl;
  130. return &decl;
  131. }
  132. virtual void setVertexDecl( const GFXVertexDecl *decl ) { }
  133. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  134. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  135. private:
  136. typedef GFXDevice Parent;
  137. friend class GFXGLTextureObject;
  138. friend class GFXGLCubemap;
  139. friend class GFXGLWindowTarget;
  140. friend class GFXGLPrimitiveBuffer;
  141. friend class GFXGLVertexBuffer;
  142. static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
  143. U32 mAdapterIndex;
  144. StrongRefPtr<GFXGLVertexBuffer> mCurrentVB;
  145. StrongRefPtr<GFXGLPrimitiveBuffer> mCurrentPB;
  146. /// Since GL does not have separate world and view matrices we need to track them
  147. MatrixF m_mCurrentWorld;
  148. MatrixF m_mCurrentView;
  149. void* mContext;
  150. void* mPixelFormat;
  151. F32 mPixelShaderVersion;
  152. bool mSupportsAnisotropic;
  153. U32 mMaxShaderTextures;
  154. U32 mMaxFFTextures;
  155. RectI mClip;
  156. GFXGLStateBlockRef mCurrentGLStateBlock;
  157. GLenum mActiveTextureType[TEXTURE_STAGE_COUNT];
  158. Vector< StrongRefPtr<GFXGLVertexBuffer> > mVolatileVBs; ///< Pool of existing volatile VBs so we can reuse previously created ones
  159. Vector< StrongRefPtr<GFXGLPrimitiveBuffer> > mVolatilePBs; ///< Pool of existing volatile PBs so we can reuse previously created ones
  160. GLsizei primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount);
  161. void preDrawPrimitive();
  162. void postDrawPrimitive(U32 primitiveCount);
  163. GFXVertexBuffer* findVolatileVBO(U32 numVerts, const GFXVertexFormat *vertexFormat, U32 vertSize); ///< Returns an existing volatile VB which has >= numVerts and the same vert flags/size, or creates a new VB if necessary
  164. GFXPrimitiveBuffer* findVolatilePBO(U32 numIndices, U32 numPrimitives); ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary
  165. void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
  166. GFXFence* _createPlatformSpecificFence(); ///< If our platform (e.g. OS X) supports a fence extenstion (e.g. GL_APPLE_fence) this will create one, otherwise returns NULL
  167. void setPB(GFXGLPrimitiveBuffer* pb); ///< Sets mCurrentPB
  168. };
  169. #endif