gfxGLDevice.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 GFXGLTextureArray;
  33. class GFXGLVertexBuffer;
  34. class GFXGLPrimitiveBuffer;
  35. class GFXGLTextureTarget;
  36. class GFXGLCubemap;
  37. class GFXGLCubemapArray;
  38. class GFXGLStateCache;
  39. class GFXGLVertexDecl;
  40. class GFXGLDevice : public GFXDevice
  41. {
  42. public:
  43. struct GLCapabilities
  44. {
  45. bool anisotropicFiltering;
  46. bool bufferStorage;
  47. bool textureStorage;
  48. bool copyImage;
  49. bool vertexAttributeBinding;
  50. bool khrDebug;
  51. bool extDebugMarker;
  52. };
  53. GLCapabilities mCapabilities;
  54. void zombify();
  55. void resurrect();
  56. GFXGLDevice(U32 adapterIndex);
  57. virtual ~GFXGLDevice();
  58. static void enumerateAdapters( Vector<GFXAdapter*> &adapterList );
  59. static GFXDevice *createInstance( U32 adapterIndex );
  60. virtual void init( const GFXVideoMode &mode, PlatformWindow *window = NULL );
  61. virtual void activate() { }
  62. virtual void deactivate() { }
  63. virtual GFXAdapterType getAdapterType() { return OpenGL; }
  64. virtual void enterDebugEvent(ColorI color, const char *name);
  65. virtual void leaveDebugEvent();
  66. virtual void setDebugMarker(ColorI color, const char *name);
  67. virtual void enumerateVideoModes();
  68. virtual U32 getTotalVideoMemory_GL_EXT();
  69. virtual U32 getTotalVideoMemory();
  70. virtual GFXCubemap * createCubemap();
  71. virtual GFXCubemapArray *createCubemapArray();
  72. virtual GFXTextureArray *createTextureArray();
  73. virtual F32 getFillConventionOffset() const { return 0.0f; }
  74. ///@}
  75. /// @name Render Target functions
  76. /// @{
  77. ///
  78. virtual GFXTextureTarget *allocRenderToTextureTarget(bool genMips = true);
  79. virtual GFXWindowTarget *allocWindowTarget(PlatformWindow *window);
  80. virtual void _updateRenderTargets();
  81. ///@}
  82. /// @name Shader functions
  83. /// @{
  84. virtual F32 getPixelShaderVersion() const { return mPixelShaderVersion; }
  85. virtual void setPixelShaderVersion( F32 version ) { mPixelShaderVersion = version; }
  86. virtual void setShader(GFXShader *shader, bool force = false);
  87. /// @attention GL cannot check if the given format supports blending or filtering!
  88. virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
  89. const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter);
  90. /// Returns the number of texture samplers that can be used in a shader rendering pass
  91. virtual U32 getNumSamplers() const;
  92. /// Returns the number of simultaneous render targets supported by the device.
  93. virtual U32 getNumRenderTargets() const;
  94. virtual GFXShader* createShader();
  95. //TODO: implement me!
  96. virtual void copyResource(GFXTextureObject *pDst, GFXCubemap *pSrc, const U32 face);
  97. virtual void clear( U32 flags, const LinearColorF& color, F32 z, U32 stencil );
  98. virtual void clearColorAttachment(const U32 attachment, const LinearColorF& color);
  99. virtual bool beginSceneInternal();
  100. virtual void endSceneInternal();
  101. virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
  102. virtual void drawIndexedPrimitive( GFXPrimitiveType primType,
  103. U32 startVertex,
  104. U32 minIndex,
  105. U32 numVerts,
  106. U32 startIndex,
  107. U32 primitiveCount );
  108. virtual void setClipRect( const RectI &rect );
  109. virtual const RectI &getClipRect() const { return mClip; }
  110. virtual void preDestroy() { Parent::preDestroy(); }
  111. virtual U32 getMaxDynamicVerts() { return GFX_MAX_DYNAMIC_VERTS; }
  112. virtual U32 getMaxDynamicIndices() { return GFX_MAX_DYNAMIC_INDICES; }
  113. GFXFence *createFence();
  114. GFXOcclusionQuery* createOcclusionQuery();
  115. GFXGLStateBlockRef getCurrentStateBlock() { return mCurrentGLStateBlock; }
  116. virtual void setupGenericShaders( GenericShaderType type = GSColor );
  117. ///
  118. bool supportsAnisotropic() const { return mCapabilities.anisotropicFiltering; }
  119. GFXGLStateCache* getOpenglCache() { return mOpenglStateCache; }
  120. GFXTextureObject* getDefaultDepthTex() const;
  121. /// Returns the number of vertex streams supported by the device.
  122. const U32 getNumVertexStreams() const { return mNumVertexStream; }
  123. bool glUseMap() const { return mUseGlMap; }
  124. const char* interpretDebugResult(long result) { return "Not Implemented"; };
  125. protected:
  126. /// Called by GFXDevice to create a device specific stateblock
  127. virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
  128. /// Called by GFXDevice to actually set a stateblock.
  129. virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
  130. /// Called by base GFXDevice to actually set a const buffer
  131. virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
  132. virtual void setTextureInternal(U32 textureUnit, const GFXTextureObject*texture);
  133. virtual void setCubemapInternal(U32 textureUnit, const GFXGLCubemap* texture);
  134. virtual void setCubemapArrayInternal(U32 textureUnit, const GFXGLCubemapArray* texture);
  135. virtual void setTextureArrayInternal(U32 textureUnit, const GFXGLTextureArray* texture);
  136. /// @name State Initalization.
  137. /// @{
  138. /// State initalization. This MUST BE CALLED in setVideoMode after the device
  139. /// is created.
  140. virtual void initStates() { }
  141. virtual GFXVertexBuffer *allocVertexBuffer( U32 numVerts,
  142. const GFXVertexFormat *vertexFormat,
  143. U32 vertSize,
  144. GFXBufferType bufferType,
  145. void* data = NULL);
  146. virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data = NULL );
  147. // NOTE: The GL device doesn't need a vertex declaration at
  148. // this time, but we need to return something to keep the system
  149. // from retrying to allocate one on every call.
  150. virtual GFXVertexDecl* allocVertexDecl( const GFXVertexFormat *vertexFormat );
  151. virtual void setVertexDecl( const GFXVertexDecl *decl );
  152. virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
  153. virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
  154. private:
  155. typedef GFXDevice Parent;
  156. friend class GFXGLTextureObject;
  157. friend class GFXGLCubemap;
  158. friend class GFXGLCubemapArray;
  159. friend class GFXGLTextureArray;
  160. friend class GFXGLWindowTarget;
  161. friend class GFXGLPrimitiveBuffer;
  162. friend class GFXGLVertexBuffer;
  163. static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
  164. U32 mAdapterIndex;
  165. StrongRefPtr<GFXGLVertexBuffer> mCurrentVB[VERTEX_STREAM_COUNT];
  166. U32 mCurrentVB_Divisor[VERTEX_STREAM_COUNT];
  167. bool mNeedUpdateVertexAttrib;
  168. StrongRefPtr<GFXGLPrimitiveBuffer> mCurrentPB;
  169. U32 mDrawInstancesCount;
  170. GFXShader* mCurrentShader;
  171. GFXShaderRef mGenericShader[GS_COUNT];
  172. GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
  173. GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
  174. /// Since GL does not have separate world and view matrices we need to track them
  175. MatrixF m_mCurrentWorld;
  176. MatrixF m_mCurrentView;
  177. void* mContext;
  178. void* mPixelFormat;
  179. F32 mPixelShaderVersion;
  180. U32 mNumVertexStream;
  181. U32 mMaxShaderTextures;
  182. U32 mMaxFFTextures;
  183. U32 mMaxTRColors;
  184. RectI mClip;
  185. GFXGLStateBlockRef mCurrentGLStateBlock;
  186. GLenum mActiveTextureType[GFX_TEXTURE_STAGE_COUNT];
  187. Vector< StrongRefPtr<GFXGLVertexBuffer> > mVolatileVBs; ///< Pool of existing volatile VBs so we can reuse previously created ones
  188. Vector< StrongRefPtr<GFXGLPrimitiveBuffer> > mVolatilePBs; ///< Pool of existing volatile PBs so we can reuse previously created ones
  189. GLsizei primCountToIndexCount(GFXPrimitiveType primType, U32 primitiveCount);
  190. void preDrawPrimitive();
  191. void postDrawPrimitive(U32 primitiveCount);
  192. 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
  193. GFXPrimitiveBuffer* findVolatilePBO(U32 numIndices, U32 numPrimitives); ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary
  194. void vsyncCallback(); ///< Vsync callback
  195. void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
  196. 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
  197. void setPB(GFXGLPrimitiveBuffer* pb); ///< Sets mCurrentPB
  198. GFXGLStateCache *mOpenglStateCache;
  199. GFXWindowTargetRef *mWindowRT;
  200. bool mUseGlMap;
  201. };
  202. #define GFXGL static_cast<GFXGLDevice*>(GFXDevice::get())
  203. #endif