gfxGLDevice.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include "gfx/gl/tGL/tGL.h"
  28. #include "windowManager/platformWindow.h"
  29. #include "gfx/gfxFence.h"
  30. #include "gfx/gfxResource.h"
  31. #include "gfx/gl/gfxGLStateBlock.h"
  32. class GFXGLVertexBuffer;
  33. class GFXGLPrimitiveBuffer;
  34. class GFXGLTextureTarget;
  35. class GFXGLCubemap;
  36. class GFXGLStateCache;
  37. class GFXGLVertexDecl;
  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_GL_EXT();
  56. virtual U32 getTotalVideoMemory();
  57. virtual GFXCubemap * createCubemap();
  58. virtual F32 getFillConventionOffset() const { return 0.0f; }
  59. ///@}
  60. /// @name Render Target functions
  61. /// @{
  62. ///
  63. virtual GFXTextureTarget *allocRenderToTextureTarget();
  64. virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window);
  65. virtual void _updateRenderTargets();
  66. ///@}
  67. /// @name Shader functions
  68. /// @{
  69. virtual F32 getPixelShaderVersion() const { return mPixelShaderVersion; }
  70. virtual void setPixelShaderVersion( F32 version ) { mPixelShaderVersion = version; }
  71. virtual void setShader(GFXShader *shader, bool force = false);
  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. GFXGLStateCache* getOpenglCache() { return mOpenglStateCache; }
  102. GFXTextureObject* getDefaultDepthTex() const;
  103. /// Returns the number of vertex streams supported by the device.
  104. const U32 getNumVertexStreams() const { return mNumVertexStream; }
  105. bool glUseMap() const { return mUseGlMap; }
  106. protected:
  107. /// Called by GFXDevice to create a device specific stateblock
  108. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  109. /// Called by GFXDevice to actually set a stateblock.
  110. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  111. /// Called by base GFXDevice to actually set a const buffer
  112. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  113. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture);
  114. virtual void setCubemapInternal(U32 cubemap, const GFXGLCubemap* texture);
  115. virtual void setLightInternal(U32 lightStage, const GFXLightInfo light, bool lightEnable);
  116. virtual void setLightMaterialInternal(const GFXLightMaterial mat);
  117. virtual void setGlobalAmbientInternal(ColorF color);
  118. /// @name State Initalization.
  119. /// @{
  120. /// State initalization. This MUST BE CALLED in setVideoMode after the device
  121. /// is created.
  122. virtual void initStates() { }
  123. virtual void setMatrix( GFXMatrixType mtype, const MatrixF &mat );
  124. virtual GFXVertexBuffer *allocVertexBuffer( U32 numVerts,
  125. const GFXVertexFormat *vertexFormat,
  126. U32 vertSize,
  127. GFXBufferType bufferType );
  128. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType );
  129. // NOTE: The GL device doesn't need a vertex declaration at
  130. // this time, but we need to return something to keep the system
  131. // from retrying to allocate one on every call.
  132. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
  133. virtual void setVertexDecl( const GFXVertexDecl *decl );
  134. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  135. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  136. private:
  137. typedef GFXDevice Parent;
  138. friend class GFXGLTextureObject;
  139. friend class GFXGLCubemap;
  140. friend class GFXGLWindowTarget;
  141. friend class GFXGLPrimitiveBuffer;
  142. friend class GFXGLVertexBuffer;
  143. static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
  144. U32 mAdapterIndex;
  145. StrongRefPtr<GFXGLVertexBuffer> mCurrentVB[VERTEX_STREAM_COUNT];
  146. U32 mCurrentVB_Divisor[VERTEX_STREAM_COUNT];
  147. bool mNeedUpdateVertexAttrib;
  148. StrongRefPtr<GFXGLPrimitiveBuffer> mCurrentPB;
  149. U32 mDrawInstancesCount;
  150. GFXShader* mCurrentShader;
  151. GFXShaderRef mGenericShader[GS_COUNT];
  152. GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
  153. GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
  154. /// Since GL does not have separate world and view matrices we need to track them
  155. MatrixF m_mCurrentWorld;
  156. MatrixF m_mCurrentView;
  157. void* mContext;
  158. void* mPixelFormat;
  159. F32 mPixelShaderVersion;
  160. bool mSupportsAnisotropic;
  161. U32 mNumVertexStream;
  162. U32 mMaxShaderTextures;
  163. U32 mMaxFFTextures;
  164. U32 mMaxTRColors;
  165. RectI mClip;
  166. GFXGLStateBlockRef mCurrentGLStateBlock;
  167. GLenum mActiveTextureType[TEXTURE_STAGE_COUNT];
  168. Vector< StrongRefPtr<GFXGLVertexBuffer> > mVolatileVBs; ///< Pool of existing volatile VBs so we can reuse previously created ones
  169. Vector< StrongRefPtr<GFXGLPrimitiveBuffer> > mVolatilePBs; ///< Pool of existing volatile PBs so we can reuse previously created ones
  170. GLsizei primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount);
  171. void preDrawPrimitive();
  172. void postDrawPrimitive(U32 primitiveCount);
  173. 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
  174. GFXPrimitiveBuffer* findVolatilePBO(U32 numIndices, U32 numPrimitives); ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary
  175. void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
  176. 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
  177. void setPB(GFXGLPrimitiveBuffer* pb); ///< Sets mCurrentPB
  178. GFXGLStateCache *mOpenglStateCache;
  179. GFXWindowTargetRef *mWindowRT;
  180. bool mUseGlMap;
  181. };
  182. #define GFXGL static_cast<GFXGLDevice*>(GFXDevice::get())
  183. #endif