2012-09-19 11:15:01 -04:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
# ifndef _GFXGLDEVICE_H_
# define _GFXGLDEVICE_H_
# include "platform/platform.h"
# include "gfx/gfxDevice.h"
# include "gfx/gfxInit.h"
2014-11-08 17:41:17 +01:00
# include "gfx/gl/tGL/tGL.h"
2012-09-19 11:15:01 -04:00
# include "windowManager/platformWindow.h"
# include "gfx/gfxFence.h"
# include "gfx/gfxResource.h"
# include "gfx/gl/gfxGLStateBlock.h"
2021-01-01 21:05:21 +01:00
class GFXGLTextureArray ;
2012-09-19 11:15:01 -04:00
class GFXGLVertexBuffer ;
class GFXGLPrimitiveBuffer ;
class GFXGLTextureTarget ;
class GFXGLCubemap ;
2019-04-15 23:11:18 -05:00
class GFXGLCubemapArray ;
2014-11-08 17:41:17 +01:00
class GFXGLStateCache ;
class GFXGLVertexDecl ;
2024-03-13 22:23:01 +00:00
class GFXGLShaderConstBuffer ;
2012-09-19 11:15:01 -04:00
class GFXGLDevice : public GFXDevice
{
2024-03-14 14:23:08 +00:00
2012-09-19 11:15:01 -04:00
public :
2016-05-06 22:57:35 -04:00
struct GLCapabilities
{
bool anisotropicFiltering ;
bool bufferStorage ;
bool textureStorage ;
bool copyImage ;
bool vertexAttributeBinding ;
2019-08-21 03:12:54 -05:00
bool khrDebug ;
bool extDebugMarker ;
2016-05-06 22:57:35 -04:00
} ;
GLCapabilities mCapabilities ;
2016-05-06 21:50:11 -04:00
2024-03-14 14:23:08 +00:00
// UBO map
typedef Map < String , GLuint > DeviceBufferMap ;
// grab device buffer.
GLuint getDeviceBuffer ( const GFXShaderConstDesc desc ) ;
2012-09-19 11:15:01 -04:00
void zombify ( ) ;
void resurrect ( ) ;
GFXGLDevice ( U32 adapterIndex ) ;
virtual ~ GFXGLDevice ( ) ;
static void enumerateAdapters ( Vector < GFXAdapter * > & adapterList ) ;
static GFXDevice * createInstance ( U32 adapterIndex ) ;
2024-03-18 18:40:22 +00:00
void init ( const GFXVideoMode & mode , PlatformWindow * window = NULL ) override ;
2012-09-19 11:15:01 -04:00
virtual void activate ( ) { }
virtual void deactivate ( ) { }
2024-03-18 18:40:22 +00:00
GFXAdapterType getAdapterType ( ) override { return OpenGL ; }
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void enterDebugEvent ( ColorI color , const char * name ) override ;
void leaveDebugEvent ( ) override ;
void setDebugMarker ( ColorI color , const char * name ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void enumerateVideoModes ( ) override ;
2012-09-19 11:15:01 -04:00
2014-11-08 17:41:17 +01:00
virtual U32 getTotalVideoMemory_GL_EXT ( ) ;
2012-09-19 11:15:01 -04:00
virtual U32 getTotalVideoMemory ( ) ;
2024-03-18 18:40:22 +00:00
GFXCubemap * createCubemap ( ) override ;
GFXCubemapArray * createCubemapArray ( ) override ;
GFXTextureArray * createTextureArray ( ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
F32 getFillConventionOffset ( ) const override { return 0.0f ; }
2012-09-19 11:15:01 -04:00
///@}
/// @name Render Target functions
/// @{
///
2024-03-18 18:40:22 +00:00
GFXTextureTarget * allocRenderToTextureTarget ( bool genMips = true ) override ;
GFXWindowTarget * allocWindowTarget ( PlatformWindow * window ) override ;
void _updateRenderTargets ( ) override ;
2012-09-19 11:15:01 -04:00
///@}
/// @name Shader functions
/// @{
2024-03-18 18:40:22 +00:00
F32 getPixelShaderVersion ( ) const override { return mPixelShaderVersion ; }
void setPixelShaderVersion ( F32 version ) override { mPixelShaderVersion = version ; }
2024-03-13 22:23:01 +00:00
2024-03-18 18:40:22 +00:00
void setShader ( GFXShader * shader , bool force = false ) override ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
/// @attention GL cannot check if the given format supports blending or filtering!
2024-03-18 18:40:22 +00:00
GFXFormat selectSupportedFormat ( GFXTextureProfile * profile ,
const Vector < GFXFormat > & formats , bool texture , bool mustblend , bool mustfilter ) override ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
/// Returns the number of texture samplers that can be used in a shader rendering pass
2024-03-18 18:40:22 +00:00
U32 getNumSamplers ( ) const override ;
2012-09-19 11:15:01 -04:00
/// Returns the number of simultaneous render targets supported by the device.
2024-03-18 18:40:22 +00:00
U32 getNumRenderTargets ( ) const override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
GFXShader * createShader ( ) override ;
2018-11-28 17:51:52 +10:00
//TODO: implement me!
2024-03-18 18:40:22 +00:00
void copyResource ( GFXTextureObject * pDst , GFXCubemap * pSrc , const U32 face ) override ;
void clear ( U32 flags , const LinearColorF & color , F32 z , U32 stencil ) override ;
void clearColorAttachment ( const U32 attachment , const LinearColorF & color ) override ;
bool beginSceneInternal ( ) override ;
void endSceneInternal ( ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void drawPrimitive ( GFXPrimitiveType primType , U32 vertexStart , U32 primitiveCount ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void drawIndexedPrimitive ( GFXPrimitiveType primType ,
2024-03-13 22:23:01 +00:00
U32 startVertex ,
U32 minIndex ,
U32 numVerts ,
U32 startIndex ,
2024-03-18 18:40:22 +00:00
U32 primitiveCount ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void setClipRect ( const RectI & rect ) override ;
const RectI & getClipRect ( ) const override { return mClip ; }
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void preDestroy ( ) override { Parent : : preDestroy ( ) ; }
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
U32 getMaxDynamicVerts ( ) override { return GFX_MAX_DYNAMIC_VERTS ; }
U32 getMaxDynamicIndices ( ) override { return GFX_MAX_DYNAMIC_INDICES ; }
2024-03-13 22:23:01 +00:00
2024-03-18 18:40:22 +00:00
GFXFence * createFence ( ) override ;
2024-03-13 22:23:01 +00:00
2024-03-18 18:40:22 +00:00
GFXOcclusionQuery * createOcclusionQuery ( ) override ;
2012-09-19 11:15:01 -04:00
GFXGLStateBlockRef getCurrentStateBlock ( ) { return mCurrentGLStateBlock ; }
2024-03-13 22:23:01 +00:00
2024-03-18 18:40:22 +00:00
void setupGenericShaders ( GenericShaderType type = GSColor ) override ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
///
2021-12-15 20:09:18 +10:00
bool supportsAnisotropic ( ) const { return mCapabilities . anisotropicFiltering ; }
2014-11-08 17:41:17 +01:00
GFXGLStateCache * getOpenglCache ( ) { return mOpenglStateCache ; }
GFXTextureObject * getDefaultDepthTex ( ) const ;
2024-03-13 22:23:01 +00:00
/// Returns the number of vertex streams supported by the device.
2014-11-08 17:41:17 +01:00
const U32 getNumVertexStreams ( ) const { return mNumVertexStream ; }
2024-03-13 22:23:01 +00:00
bool glUseMap ( ) const { return mUseGlMap ; }
2024-03-18 18:40:22 +00:00
const char * interpretDebugResult ( long result ) override { return " Not Implemented " ; } ;
2024-03-13 22:23:01 +00:00
protected :
2012-09-19 11:15:01 -04:00
/// Called by GFXDevice to create a device specific stateblock
2024-03-18 18:40:22 +00:00
GFXStateBlockRef createStateBlockInternal ( const GFXStateBlockDesc & desc ) override ;
2012-09-19 11:15:01 -04:00
/// Called by GFXDevice to actually set a stateblock.
2024-03-18 18:40:22 +00:00
void setStateBlockInternal ( GFXStateBlock * block , bool force ) override ;
2012-09-19 11:15:01 -04:00
/// Called by base GFXDevice to actually set a const buffer
2024-03-18 18:40:22 +00:00
void setShaderConstBufferInternal ( GFXShaderConstBuffer * buffer ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void setTextureInternal ( U32 textureUnit , const GFXTextureObject * texture ) override ;
2019-04-15 23:11:18 -05:00
virtual void setCubemapInternal ( U32 textureUnit , const GFXGLCubemap * texture ) ;
virtual void setCubemapArrayInternal ( U32 textureUnit , const GFXGLCubemapArray * texture ) ;
2021-01-01 21:05:21 +01:00
virtual void setTextureArrayInternal ( U32 textureUnit , const GFXGLTextureArray * texture ) ;
2012-09-19 11:15:01 -04:00
/// @name State Initalization.
/// @{
/// State initalization. This MUST BE CALLED in setVideoMode after the device
/// is created.
2024-03-18 18:40:22 +00:00
void initStates ( ) override { }
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
GFXVertexBuffer * allocVertexBuffer ( U32 numVerts ,
2012-09-19 11:15:01 -04:00
const GFXVertexFormat * vertexFormat ,
2024-03-13 22:23:01 +00:00
U32 vertSize ,
2016-02-20 21:28:18 +01:00
GFXBufferType bufferType ,
2024-03-18 18:40:22 +00:00
void * data = NULL ) override ;
GFXPrimitiveBuffer * allocPrimitiveBuffer ( U32 numIndices , U32 numPrimitives , GFXBufferType bufferType , void * data = NULL ) override ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
// NOTE: The GL device doesn't need a vertex declaration at
// this time, but we need to return something to keep the system
// from retrying to allocate one on every call.
2024-03-18 18:40:22 +00:00
GFXVertexDecl * allocVertexDecl ( const GFXVertexFormat * vertexFormat ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void setVertexDecl ( const GFXVertexDecl * decl ) override ;
2012-09-19 11:15:01 -04:00
2024-03-18 18:40:22 +00:00
void setVertexStream ( U32 stream , GFXVertexBuffer * buffer ) override ;
void setVertexStreamFrequency ( U32 stream , U32 frequency ) override ;
2024-03-13 22:23:01 +00:00
StrongRefPtr < GFXGLShaderConstBuffer > mCurrentConstBuffer ;
2024-03-14 14:23:08 +00:00
DeviceBufferMap mDeviceBufferMap ;
2012-09-19 11:15:01 -04:00
private :
typedef GFXDevice Parent ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
friend class GFXGLTextureObject ;
friend class GFXGLCubemap ;
2019-04-15 23:11:18 -05:00
friend class GFXGLCubemapArray ;
2021-01-01 21:05:21 +01:00
friend class GFXGLTextureArray ;
2012-09-19 11:15:01 -04:00
friend class GFXGLWindowTarget ;
friend class GFXGLPrimitiveBuffer ;
friend class GFXGLVertexBuffer ;
2019-04-15 23:11:18 -05:00
static GFXAdapter : : CreateDeviceInstanceDelegate mCreateDeviceInstance ;
2012-09-19 11:15:01 -04:00
U32 mAdapterIndex ;
2024-03-13 22:23:01 +00:00
2014-11-08 17:41:17 +01:00
StrongRefPtr < GFXGLVertexBuffer > mCurrentVB [ VERTEX_STREAM_COUNT ] ;
U32 mCurrentVB_Divisor [ VERTEX_STREAM_COUNT ] ;
bool mNeedUpdateVertexAttrib ;
2012-09-19 11:15:01 -04:00
StrongRefPtr < GFXGLPrimitiveBuffer > mCurrentPB ;
2014-11-08 17:41:17 +01:00
U32 mDrawInstancesCount ;
2024-03-13 22:23:01 +00:00
2014-11-08 17:41:17 +01:00
GFXShader * mCurrentShader ;
GFXShaderRef mGenericShader [ GS_COUNT ] ;
GFXShaderConstBufferRef mGenericShaderBuffer [ GS_COUNT ] ;
GFXShaderConstHandle * mModelViewProjSC [ GS_COUNT ] ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
/// Since GL does not have separate world and view matrices we need to track them
MatrixF m_mCurrentWorld ;
MatrixF m_mCurrentView ;
void * mContext ;
void * mPixelFormat ;
F32 mPixelShaderVersion ;
2014-11-08 17:41:17 +01:00
U32 mNumVertexStream ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
U32 mMaxShaderTextures ;
U32 mMaxFFTextures ;
2014-11-08 17:41:17 +01:00
U32 mMaxTRColors ;
2012-09-19 11:15:01 -04:00
RectI mClip ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
GFXGLStateBlockRef mCurrentGLStateBlock ;
2024-03-13 22:23:01 +00:00
2021-01-05 12:57:17 +10:00
GLenum mActiveTextureType [ GFX_TEXTURE_STAGE_COUNT ] ;
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
Vector < StrongRefPtr < GFXGLVertexBuffer > > mVolatileVBs ; ///< Pool of existing volatile VBs so we can reuse previously created ones
Vector < StrongRefPtr < GFXGLPrimitiveBuffer > > mVolatilePBs ; ///< Pool of existing volatile PBs so we can reuse previously created ones
GLsizei primCountToIndexCount ( GFXPrimitiveType primType , U32 primitiveCount ) ;
void preDrawPrimitive ( ) ;
2024-03-13 22:23:01 +00:00
void postDrawPrimitive ( U32 primitiveCount ) ;
2012-09-19 11:15:01 -04:00
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
GFXPrimitiveBuffer * findVolatilePBO ( U32 numIndices , U32 numPrimitives ) ; ///< Returns an existing volatile PB which has >= numIndices, or creates a new PB if necessary
2016-12-23 13:59:55 +10:00
void vsyncCallback ( ) ; ///< Vsync callback
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
void initGLState ( ) ; ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
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
2024-03-13 22:23:01 +00:00
2012-09-19 11:15:01 -04:00
void setPB ( GFXGLPrimitiveBuffer * pb ) ; ///< Sets mCurrentPB
2014-11-08 17:41:17 +01:00
GFXGLStateCache * mOpenglStateCache ;
GFXWindowTargetRef * mWindowRT ;
bool mUseGlMap ;
2012-09-19 11:15:01 -04:00
} ;
2014-11-08 17:41:17 +01:00
# define GFXGL static_cast<GFXGLDevice*>(GFXDevice::get())
2012-09-19 11:15:01 -04:00
# endif