gfxGLDevice.h 11 KB

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