Sfoglia il codice sorgente

opengl ubo setup

opengl can now compile with ubo buffer objects similar to cbuffers on dx side.

cleaned double up of data from both sides, gfxhandles only need to use the desc info instead of holding onto its own.
marauder2k7 1 anno fa
parent
commit
9dc5ae833b

+ 4 - 10
Engine/source/gfx/D3D11/gfxD3D11Shader.cpp

@@ -95,12 +95,9 @@ HRESULT gfxD3D11Include::Close( THIS_ LPCVOID pData )
 
 GFXD3D11ShaderConstHandle::GFXD3D11ShaderConstHandle(GFXD3D11Shader* shader)
    : mShader(shader),
-   mSize(0),
-   mSampler(-1),
-   mArraySize(1),
    mInstancingConstant(false)
 {
-   mType = GFXSCT_ConstBuffer;
+   dMemset(&mDesc, 0, sizeof(mDesc));
    mValid = false;
    mStageFlags = 0;
 }
@@ -108,18 +105,15 @@ GFXD3D11ShaderConstHandle::GFXD3D11ShaderConstHandle(GFXD3D11Shader* shader)
 GFXD3D11ShaderConstHandle::GFXD3D11ShaderConstHandle(GFXD3D11Shader* shader,
                                                       const GFXShaderConstDesc& desc)
    : mShader(shader),
-   mSize(desc.size),
-   mSampler(desc.samplerReg),
-   mType(desc.constType),
-   mArraySize(desc.arraySize),
+   mDesc(desc),
    mInstancingConstant(false)
 {
    if (desc.constType == GFXSCT_ConstBuffer)
       mValid = false;
    else
       mValid = true;
-   addDesc(desc.shaderStage, desc);
 
+   addDesc(desc.shaderStage, desc);
    mStageFlags = desc.shaderStage;
 }
 
@@ -976,7 +970,7 @@ void GFXD3D11Shader::_getShaderConstants( ID3D11ShaderReflection* refTable,
                varDesc.constType = convertConstType(shaderTypeDesc);
 
 #ifdef D3D11_DEBUG_SPEW
-               Con::printf("Variable Name %s:, offset: %d, size: %d, constantDesc.Elements: %d", varDesc.name.c_str(), varDesc.StartOffset, varDesc.Size, varDesc.arraySize);
+               Con::printf("Variable Name %s:, offset: %d, size: %d, constantDesc.Elements: %d", varDesc.name.c_str(), varDesc.offset, varDesc.size, varDesc.arraySize);
 #endif
                mShaderConsts.push_back(varDesc);
             }

+ 6 - 10
Engine/source/gfx/D3D11/gfxD3D11Shader.h

@@ -69,15 +69,15 @@ public:
    virtual ~GFXD3D11ShaderConstHandle();
    void addDesc(GFXShaderStage stage, const GFXShaderConstDesc& desc);
    const GFXShaderConstDesc getDesc(GFXShaderStage stage);
-   const String& getName() const { return mName; }
-   GFXShaderConstType getType() const { return mType; }
-   U32 getArraySize() const { return mArraySize; }
+   const String& getName() const { return mDesc.name; }
+   GFXShaderConstType getType() const { return mDesc.constType; }
+   U32 getArraySize() const { return mDesc.arraySize; }
 
-   U32 getSize() const { return mSize; }
+   U32 getSize() const { return mDesc.size; }
    void setValid(bool valid) { mValid = valid; }
    /// @warning This will always return the value assigned when the shader was
    /// initialized.  If the value is later changed this method won't reflect that.
-   S32 getSamplerRegister() const { return (!isSampler() || !mValid) ? -1 : mSampler; }
+   S32 getSamplerRegister() const { return (!isSampler() || !mValid) ? -1 : mDesc.samplerReg; }
 
    // Returns true if this is a handle to a sampler register.
    bool isSampler() const
@@ -93,13 +93,9 @@ public:
       mValid = false;
    }
 
+   GFXShaderConstDesc mDesc;
    GFXD3D11Shader* mShader;
    DescMap mDescMap;
-   String mName;
-   GFXShaderConstType mType;
-   U32 mSize;
-   U32 mArraySize;
-   S32 mSampler; // sampler number, will be -1 if not a sampler.
    U32 mStageFlags;
    bool mInstancingConstant;
 };

+ 6 - 6
Engine/source/gfx/gfxShader.h

@@ -81,12 +81,12 @@ struct GFXShaderConstDesc
 public:
    String name;
    GFXShaderConstType constType;
-   U32 arraySize;    // > 1 means it is an array!
-   S32 bindPoint;    // bind point used for ubo/cb.
-   S32 samplerReg;   // sampler register.
-   U32 offset;       // offset for vars
-   U32 size;         // size of buffer/type
-   GFXShaderStage shaderStage;  // only used dx side.
+   U32 arraySize = 0;            // > 1 means it is an array!
+   S32 bindPoint = -1;           // bind point used for ubo/cb
+   S32 samplerReg = -1;          // sampler register.
+   U32 offset = 0;               // offset for vars
+   U32 size = 0;                 // size of buffer/type
+   GFXShaderStage shaderStage;   // only used dx side.
 };
 
 /// This is an opaque handle used by GFXShaderConstBuffer clients to set individual shader constants.

+ 99 - 87
Engine/source/gfx/gl/gfxGLDevice.cpp

@@ -57,7 +57,7 @@
 #include "gfx/gl/tGL/tXGL.h"
 #endif
 
-GFXAdapter::CreateDeviceInstanceDelegate GFXGLDevice::mCreateDeviceInstance(GFXGLDevice::createInstance); 
+GFXAdapter::CreateDeviceInstanceDelegate GFXGLDevice::mCreateDeviceInstance(GFXGLDevice::createInstance);
 
 GFXDevice *GFXGLDevice::createInstance( U32 adapterIndex )
 {
@@ -76,7 +76,7 @@ void loadGLCore()
    if(coreLoaded)
       return;
    coreLoaded = true;
-   
+
    // Make sure we've got our GL bindings.
    GL::gglPerformBinds();
 }
@@ -87,11 +87,11 @@ void loadGLExtensions(void *context)
    if(extensionsLoaded)
       return;
    extensionsLoaded = true;
-   
+
    GL::gglPerformExtensionBinds(context);
 }
 
-void STDCALL glDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, 
+void STDCALL glDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
 	const GLchar *message, const void *userParam)
 {
     // JTH [11/24/2016]: This is a temporary fix so that we do not get spammed for redundant fbo changes.
@@ -118,24 +118,24 @@ void STDCALL glAmdDebugCallback(GLuint id, GLenum category, GLenum severity, GLs
 }
 
 void GFXGLDevice::initGLState()
-{  
+{
    // We don't currently need to sync device state with a known good place because we are
    // going to set everything in GFXGLStateBlock, but if we change our GFXGLStateBlock strategy, this may
    // need to happen.
-   
+
    // Deal with the card profiler here when we know we have a valid context.
    mCardProfiler = new GFXGLCardProfiler();
-   mCardProfiler->init(); 
+   mCardProfiler->init();
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, (GLint*)&mMaxShaderTextures);
    // JTH: Needs removed, ffp
    //glGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&mMaxFFTextures);
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, (GLint*)&mMaxTRColors);
    mMaxTRColors = getMin( mMaxTRColors, (U32)(GFXTextureTarget::MaxRenderSlotId-1) );
-   
+
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-   
+
    // [JTH 5/6/2016] GLSL 1.50 is really SM 4.0
-   // Setting mPixelShaderVersion to 3.0 will allow Advanced Lighting to run.   
+   // Setting mPixelShaderVersion to 3.0 will allow Advanced Lighting to run.
    mPixelShaderVersion = 3.0;
 
 	// Set capability extensions.
@@ -150,7 +150,7 @@ void GFXGLDevice::initGLState()
    String vendorStr = (const char*)glGetString( GL_VENDOR );
    if( vendorStr.find("NVIDIA", 0, String::NoCase | String::Left) != String::NPos)
       mUseGlMap = false;
-   
+
    // Workaround for all Mac's, has a problem using glMap* with volatile buffers
 #ifdef TORQUE_OS_MAC
    mUseGlMap = false;
@@ -173,7 +173,7 @@ void GFXGLDevice::initGLState()
    else if(gglHasExtension(AMD_debug_output))
    {
       glEnable(GL_DEBUG_OUTPUT);
-      glDebugMessageCallbackAMD(glAmdDebugCallback, NULL);      
+      glDebugMessageCallbackAMD(glAmdDebugCallback, NULL);
       //glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
       GLuint unusedIds = 0;
       glDebugMessageEnableAMD(GL_DONT_CARE, GL_DONT_CARE, 0,&unusedIds, GL_TRUE);
@@ -258,16 +258,17 @@ GFXGLDevice::GFXGLDevice(U32 adapterIndex) :
       mModelViewProjSC[i] = NULL;
 
    mOpenglStateCache = new GFXGLStateCache;
+   mCurrentConstBuffer = NULL;
 }
 
 GFXGLDevice::~GFXGLDevice()
 {
    mCurrentStateBlock = NULL;
 
-   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)      
+   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
       mCurrentVB[i] = NULL;
    mCurrentPB = NULL;
-   
+
    for(U32 i = 0; i < mVolatileVBs.size(); i++)
       mVolatileVBs[i] = NULL;
    for(U32 i = 0; i < mVolatilePBs.size(); i++)
@@ -297,7 +298,7 @@ GFXGLDevice::~GFXGLDevice()
       walk->zombify();
       walk = walk->getNextResource();
    }
-      
+
    if( mCardProfiler )
       SAFE_DELETE( mCardProfiler );
 
@@ -310,12 +311,12 @@ void GFXGLDevice::zombify()
 {
    mTextureManager->zombify();
 
-   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)   
+   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
       if(mCurrentVB[i])
          mCurrentVB[i]->finish();
    if(mCurrentPB)
          mCurrentPB->finish();
-   
+
    //mVolatileVBs.clear();
    //mVolatilePBs.clear();
    GFXResource* walk = mResourceListHead;
@@ -334,12 +335,12 @@ void GFXGLDevice::resurrect()
       walk->resurrect();
       walk = walk->getNextResource();
    }
-   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)   
+   for(int i = 0; i < VERTEX_STREAM_COUNT; ++i)
       if(mCurrentVB[i])
          mCurrentVB[i]->prepare();
    if(mCurrentPB)
       mCurrentPB->prepare();
-   
+
    mTextureManager->resurrect();
 }
 
@@ -366,7 +367,7 @@ GFXPrimitiveBuffer* GFXGLDevice::findVolatilePBO(U32 numIndices, U32 numPrimitiv
    for(U32 i = 0; i < mVolatilePBs.size(); i++)
       if((mVolatilePBs[i]->mIndexCount >= numIndices) && (mVolatilePBs[i]->getRefCount() == 1))
          return mVolatilePBs[i];
-   
+
    // No existing PB, so create one
    StrongRefPtr<GFXGLPrimitiveBuffer> buf(new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, GFXBufferTypeVolatile));
    buf->registerResourceWithDevice(this);
@@ -374,18 +375,18 @@ GFXPrimitiveBuffer* GFXGLDevice::findVolatilePBO(U32 numIndices, U32 numPrimitiv
    return buf.getPointer();
 }
 
-GFXVertexBuffer *GFXGLDevice::allocVertexBuffer(   U32 numVerts, 
-                                                   const GFXVertexFormat *vertexFormat, 
-                                                   U32 vertSize, 
+GFXVertexBuffer *GFXGLDevice::allocVertexBuffer(   U32 numVerts,
+                                                   const GFXVertexFormat *vertexFormat,
+                                                   U32 vertSize,
                                                    GFXBufferType bufferType,
-                                                   void* data )  
+                                                   void* data )
 {
    PROFILE_SCOPE(GFXGLDevice_allocVertexBuffer);
    if(bufferType == GFXBufferTypeVolatile)
       return findVolatileVBO(numVerts, vertexFormat, vertSize);
-         
+
    GFXGLVertexBuffer* buf = new GFXGLVertexBuffer( GFX, numVerts, vertexFormat, vertSize, bufferType );
-   buf->registerResourceWithDevice(this);   
+   buf->registerResourceWithDevice(this);
 
    if(data)
    {
@@ -398,10 +399,10 @@ GFXVertexBuffer *GFXGLDevice::allocVertexBuffer(   U32 numVerts,
    return buf;
 }
 
-GFXPrimitiveBuffer *GFXGLDevice::allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data ) 
+GFXPrimitiveBuffer *GFXGLDevice::allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data )
 {
    GFXPrimitiveBuffer* buf;
-   
+
    if(bufferType == GFXBufferTypeVolatile)
    {
       buf = findVolatilePBO(numIndices, numPrimitives);
@@ -411,7 +412,7 @@ GFXPrimitiveBuffer *GFXGLDevice::allocPrimitiveBuffer( U32 numIndices, U32 numPr
       buf = new GFXGLPrimitiveBuffer(GFX, numIndices, numPrimitives, bufferType);
       buf->registerResourceWithDevice(this);
    }
-   
+
    if(data)
    {
       void* dest;
@@ -430,7 +431,7 @@ void GFXGLDevice::setVertexStream( U32 stream, GFXVertexBuffer *buffer )
    {
       // Reset the state the old VB required, then set the state the new VB requires.
       if( mCurrentVB[stream] )
-      {     
+      {
          mCurrentVB[stream]->finish();
       }
 
@@ -460,10 +461,10 @@ void GFXGLDevice::setVertexStreamFrequency( U32 stream, U32 frequency )
 }
 
 GFXCubemap* GFXGLDevice::createCubemap()
-{ 
+{
    GFXGLCubemap* cube = new GFXGLCubemap();
    cube->registerResourceWithDevice(this);
-   return cube; 
+   return cube;
 };
 
 GFXCubemapArray *GFXGLDevice::createCubemapArray()
@@ -480,7 +481,7 @@ GFXTextureArray* GFXGLDevice::createTextureArray()
    return textureArray;
 }
 
-void GFXGLDevice::endSceneInternal() 
+void GFXGLDevice::endSceneInternal()
 {
    // nothing to do for opengl
    mCanCurrentlyRender = false;
@@ -556,9 +557,9 @@ void GFXGLDevice::clear(U32 flags, const LinearColorF& color, F32 z, U32 stencil
 {
    // Make sure we have flushed our render target state.
    _updateRenderTargets();
-   
+
    bool writeAllColors = true;
-   bool zwrite = true;   
+   bool zwrite = true;
    bool writeAllStencil = true;
    const GFXStateBlockDesc *desc = NULL;
    if (mCurrentGLStateBlock)
@@ -568,7 +569,7 @@ void GFXGLDevice::clear(U32 flags, const LinearColorF& color, F32 z, U32 stencil
       writeAllColors = desc->colorWriteRed && desc->colorWriteGreen && desc->colorWriteBlue && desc->colorWriteAlpha;
       writeAllStencil = desc->stencilWriteMask == 0xFFFFFFFF;
    }
-   
+
    glColorMask(true, true, true, true);
    glDepthMask(true);
    glStencilMask(0xFFFFFFFF);
@@ -585,7 +586,7 @@ void GFXGLDevice::clear(U32 flags, const LinearColorF& color, F32 z, U32 stencil
 
    if(!writeAllColors)
       glColorMask(desc->colorWriteRed, desc->colorWriteGreen, desc->colorWriteBlue, desc->colorWriteAlpha);
-   
+
    if(!zwrite)
       glDepthMask(false);
 
@@ -623,20 +624,20 @@ inline GLsizei GFXGLDevice::primCountToIndexCount(GFXPrimitiveType primType, U32
          AssertFatal(false, "GFXGLDevice::primCountToIndexCount - unrecognized prim type");
          break;
    }
-   
+
    return 0;
 }
 
-GFXVertexDecl* GFXGLDevice::allocVertexDecl( const GFXVertexFormat *vertexFormat ) 
+GFXVertexDecl* GFXGLDevice::allocVertexDecl( const GFXVertexFormat *vertexFormat )
 {
    PROFILE_SCOPE(GFXGLDevice_allocVertexDecl);
    typedef Map<void*, GFXGLVertexDecl> GFXGLVertexDeclMap;
-   static GFXGLVertexDeclMap declMap;   
+   static GFXGLVertexDeclMap declMap;
    GFXGLVertexDeclMap::Iterator itr = declMap.find( (void*)vertexFormat->getDescription().c_str() ); // description string are interned, safe to use c_str()
    if(itr != declMap.end())
       return &itr->value;
 
-   GFXGLVertexDecl &decl = declMap[(void*)vertexFormat->getDescription().c_str()];   
+   GFXGLVertexDecl &decl = declMap[(void*)vertexFormat->getDescription().c_str()];
    decl.init(vertexFormat);
    return &decl;
 }
@@ -652,7 +653,7 @@ inline void GFXGLDevice::preDrawPrimitive()
    {
       updateStates();
    }
-   
+
    if(mCurrentShaderConstBuffer)
       setShaderConstBufferInternal(mCurrentShaderConstBuffer);
 
@@ -660,18 +661,18 @@ inline void GFXGLDevice::preDrawPrimitive()
    {
       AssertFatal(mCurrVertexDecl, "");
       const GFXGLVertexDecl* decl = static_cast<const GFXGLVertexDecl*>(mCurrVertexDecl);
-      
+
       for(int i = 0; i < getNumVertexStreams(); ++i)
       {
          if(mCurrentVB[i])
          {
-            mCurrentVB[i]->prepare(i, mCurrentVB_Divisor[i]);    // GL_ARB_vertex_attrib_binding  
+            mCurrentVB[i]->prepare(i, mCurrentVB_Divisor[i]);    // GL_ARB_vertex_attrib_binding
             decl->prepareBuffer_old( i, mCurrentVB[i]->mBuffer, mCurrentVB_Divisor[i] ); // old vertex buffer/format
          }
       }
 
-      decl->updateActiveVertexAttrib( GFXGL->getOpenglCache()->getCacheVertexAttribActive() );         
-   }   
+      decl->updateActiveVertexAttrib( GFXGL->getOpenglCache()->getCacheVertexAttribActive() );
+   }
 
    mNeedUpdateVertexAttrib = false;
 }
@@ -682,26 +683,26 @@ inline void GFXGLDevice::postDrawPrimitive(U32 primitiveCount)
    mDeviceStatistics.mPolyCount += primitiveCount;
 }
 
-void GFXGLDevice::drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount ) 
+void GFXGLDevice::drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount )
 {
    preDrawPrimitive();
-  
+
    if(mCurrentVB[0])
       vertexStart += mCurrentVB[0]->mBufferVertexOffset;
 
    if(mDrawInstancesCount)
       glDrawArraysInstanced(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount), mDrawInstancesCount);
    else
-      glDrawArrays(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount));   
+      glDrawArrays(GFXGLPrimType[primType], vertexStart, primCountToIndexCount(primType, primitiveCount));
 
    postDrawPrimitive(primitiveCount);
 }
 
-void GFXGLDevice::drawIndexedPrimitive(   GFXPrimitiveType primType, 
-                                          U32 startVertex, 
-                                          U32 minIndex, 
-                                          U32 numVerts, 
-                                          U32 startIndex, 
+void GFXGLDevice::drawIndexedPrimitive(   GFXPrimitiveType primType,
+                                          U32 startVertex,
+                                          U32 minIndex,
+                                          U32 numVerts,
+                                          U32 startIndex,
                                           U32 primitiveCount )
 {
    preDrawPrimitive();
@@ -732,7 +733,7 @@ void GFXGLDevice::setTextureInternal(U32 textureUnit, const GFXTextureObject*tex
    {
       mActiveTextureType[textureUnit] = tex->getBinding();
       tex->bind(textureUnit);
-   } 
+   }
    else if(mActiveTextureType[textureUnit] != GL_ZERO)
    {
       glActiveTexture(GL_TEXTURE0 + textureUnit);
@@ -805,21 +806,21 @@ void GFXGLDevice::setClipRect( const RectI &inRect )
    F32 r = F32(mClip.point.x + mClip.extent.x);
    F32 b = F32(mClip.point.y + mClip.extent.y);
    F32 t = F32(mClip.point.y);
-   
-   // Set up projection matrix, 
+
+   // Set up projection matrix,
    //static Point4F pt;
    pt.set(2.0f / (r - l), 0.0f, 0.0f, 0.0f);
    mProjectionMatrix.setColumn(0, pt);
-   
+
    pt.set(0.0f, 2.0f / (t - b), 0.0f, 0.0f);
    mProjectionMatrix.setColumn(1, pt);
-   
+
    pt.set(0.0f, 0.0f, 1.0f, 0.0f);
    mProjectionMatrix.setColumn(2, pt);
-   
+
    pt.set((l + r) / (l - r), (t + b) / (b - t), 1.0f, 1.0f);
    mProjectionMatrix.setColumn(3, pt);
-   
+
    MatrixF mTempMatrix(true);
    setViewMatrix( mTempMatrix );
    setWorldMatrix( mTempMatrix );
@@ -844,7 +845,7 @@ void GFXGLDevice::setStateBlockInternal(GFXStateBlock* block, bool force)
    GFXGLStateBlock* glCurrent = static_cast<GFXGLStateBlock*>(mCurrentStateBlock.getPointer());
    if (force)
       glCurrent = NULL;
-      
+
    glBlock->activate(glCurrent); // Doesn't use current yet.
    mCurrentGLStateBlock = glBlock;
 }
@@ -863,19 +864,19 @@ GFXFence * GFXGLDevice::createFence()
    GFXFence* fence = _createPlatformSpecificFence();
    if(!fence)
       fence = new GFXGeneralFence( this );
-      
+
    fence->registerResourceWithDevice(this);
    return fence;
 }
 
 GFXOcclusionQuery* GFXGLDevice::createOcclusionQuery()
-{   
+{
    GFXOcclusionQuery *query = new GFXGLOcclusionQuery( this );
    query->registerResourceWithDevice(this);
    return query;
 }
 
-void GFXGLDevice::setupGenericShaders( GenericShaderType type ) 
+void GFXGLDevice::setupGenericShaders( GenericShaderType type )
 {
    AssertFatal(type != GSTargetRestore, "");
 
@@ -927,7 +928,7 @@ void GFXGLDevice::setupGenericShaders( GenericShaderType type )
       Sim::getRootGroup()->addObject(shaderData);
    }
 
-   MatrixF tempMatrix =  mProjectionMatrix * mViewMatrix * mWorldMatrix[mWorldStackSize];  
+   MatrixF tempMatrix =  mProjectionMatrix * mViewMatrix * mWorldMatrix[mWorldStackSize];
    mGenericShaderBuffer[type]->setSafe(mModelViewProjSC[type], tempMatrix);
 
    setShader( mGenericShader[type] );
@@ -959,8 +960,19 @@ void GFXGLDevice::setShader(GFXShader *shader, bool force)
 
 void GFXGLDevice::setShaderConstBufferInternal(GFXShaderConstBuffer* buffer)
 {
-   PROFILE_SCOPE(GFXGLDevice_setShaderConstBufferInternal);
-   static_cast<GFXGLShaderConstBuffer*>(buffer)->activate();
+   if (buffer)
+   {
+      PROFILE_SCOPE(GFXGLDevice_setShaderConstBufferInternal);
+      AssertFatal(static_cast<GFXGLShaderConstBuffer*>(buffer), "Incorrect shader const buffer type for this device!");
+      GFXGLShaderConstBuffer* oglBuffer = static_cast<GFXGLShaderConstBuffer*>(buffer);
+
+      oglBuffer->activate(mCurrentConstBuffer);
+      mCurrentConstBuffer = oglBuffer;
+   }
+   else
+   {
+      mCurrentConstBuffer = NULL;
+   }
 }
 
 U32 GFXGLDevice::getNumSamplers() const
@@ -968,7 +980,7 @@ U32 GFXGLDevice::getNumSamplers() const
    return getMin((U32)GFX_TEXTURE_STAGE_COUNT,mPixelShaderVersion > 0.001f ? mMaxShaderTextures : mMaxFFTextures);
 }
 
-GFXTextureObject* GFXGLDevice::getDefaultDepthTex() const 
+GFXTextureObject* GFXGLDevice::getDefaultDepthTex() const
 {
    if(mWindowRT && mWindowRT->getPointer())
       return static_cast<GFXGLWindowTarget*>( mWindowRT->getPointer() )->mBackBufferDepthTex.getPointer();
@@ -976,9 +988,9 @@ GFXTextureObject* GFXGLDevice::getDefaultDepthTex() const
    return NULL;
 }
 
-U32 GFXGLDevice::getNumRenderTargets() const 
-{ 
-   return mMaxTRColors; 
+U32 GFXGLDevice::getNumRenderTargets() const
+{
+   return mMaxTRColors;
 }
 
 void GFXGLDevice::_updateRenderTargets()
@@ -988,9 +1000,9 @@ void GFXGLDevice::_updateRenderTargets()
       if ( mRTDeactivate )
       {
          mRTDeactivate->deactivate();
-         mRTDeactivate = NULL;   
+         mRTDeactivate = NULL;
       }
-      
+
       // NOTE: The render target changes is not really accurate
       // as the GFXTextureTarget supports MRT internally.  So when
       // we activate a GFXTarget it could result in multiple calls
@@ -1006,31 +1018,31 @@ void GFXGLDevice::_updateRenderTargets()
       else
       {
          GFXGLWindowTarget *win = dynamic_cast<GFXGLWindowTarget*>( mCurrentRT.getPointer() );
-         AssertFatal( win != NULL, 
+         AssertFatal( win != NULL,
                      "GFXGLDevice::_updateRenderTargets() - invalid target subclass passed!" );
-         
+
          win->makeActive();
-         
+
          if( win->mContext != static_cast<GFXGLDevice*>(GFX)->mContext )
          {
             mRTDirty = false;
             GFX->updateStates(true);
          }
       }
-      
+
       mRTDirty = false;
    }
-   
+
    if ( mViewportDirty )
    {
-      glViewport( mViewport.point.x, mViewport.point.y, mViewport.extent.x, mViewport.extent.y ); 
+      glViewport( mViewport.point.x, mViewport.point.y, mViewport.extent.x, mViewport.extent.y );
       mViewportDirty = false;
    }
 }
 
-GFXFormat GFXGLDevice::selectSupportedFormat(   GFXTextureProfile* profile, 
-                                                const Vector<GFXFormat>& formats, 
-                                                bool texture, 
+GFXFormat GFXGLDevice::selectSupportedFormat(   GFXTextureProfile* profile,
+                                                const Vector<GFXFormat>& formats,
+                                                bool texture,
                                                 bool mustblend,
                                                 bool mustfilter )
 {
@@ -1041,10 +1053,10 @@ GFXFormat GFXGLDevice::selectSupportedFormat(   GFXTextureProfile* profile,
          continue;
       if(GFXGLTextureInternalFormat[formats[i]] == GL_ZERO)
          continue;
-      
+
       return formats[i];
    }
-   
+
    return GFXFormatR8G8B8A8;
 }
 
@@ -1055,7 +1067,7 @@ U32 GFXGLDevice::getTotalVideoMemory_GL_EXT()
    {
       GLint mem[4] = {0};
       glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, mem);  // Retrieve the texture pool
-      
+
       /* With mem[0] i get only the total memory free in the pool in KB
       *
       * mem[0] - total memory free in the pool
@@ -1066,7 +1078,7 @@ U32 GFXGLDevice::getTotalVideoMemory_GL_EXT()
 
       return  mem[0] / 1024;
    }
-   
+
    //source http://www.opengl.org/registry/specs/NVX/gpu_memory_info.txt
    else if( gglHasExtension(NVX_gpu_memory_info) )
    {

+ 35 - 34
Engine/source/gfx/gl/gfxGLDevice.h

@@ -43,6 +43,7 @@ class GFXGLCubemap;
 class GFXGLCubemapArray;
 class GFXGLStateCache;
 class GFXGLVertexDecl;
+class GFXGLShaderConstBuffer;
 
 class GFXGLDevice : public GFXDevice
 {
@@ -105,13 +106,13 @@ public:
    /// @{
    virtual F32 getPixelShaderVersion() const { return mPixelShaderVersion; }
    virtual void  setPixelShaderVersion( F32 version ) { mPixelShaderVersion = version; }
-   
+
    virtual void setShader(GFXShader *shader, bool force = false);
-   
+
    /// @attention GL cannot check if the given format supports blending or filtering!
    virtual GFXFormat selectSupportedFormat(GFXTextureProfile *profile,
 	   const Vector<GFXFormat> &formats, bool texture, bool mustblend, bool mustfilter);
-      
+
    /// Returns the number of texture samplers that can be used in a shader rendering pass
    virtual U32 getNumSamplers() const;
 
@@ -128,11 +129,11 @@ public:
 
    virtual void drawPrimitive( GFXPrimitiveType primType, U32 vertexStart, U32 primitiveCount );
 
-   virtual void drawIndexedPrimitive(  GFXPrimitiveType primType, 
-                                       U32 startVertex, 
-                                       U32 minIndex, 
-                                       U32 numVerts, 
-                                       U32 startIndex, 
+   virtual void drawIndexedPrimitive(  GFXPrimitiveType primType,
+                                       U32 startVertex,
+                                       U32 minIndex,
+                                       U32 numVerts,
+                                       U32 startIndex,
                                        U32 primitiveCount );
 
    virtual void setClipRect( const RectI &rect );
@@ -142,15 +143,15 @@ public:
 
    virtual U32 getMaxDynamicVerts() { return GFX_MAX_DYNAMIC_VERTS; }
    virtual U32 getMaxDynamicIndices() { return GFX_MAX_DYNAMIC_INDICES; }
-   
+
    GFXFence *createFence();
-   
+
    GFXOcclusionQuery* createOcclusionQuery();
 
    GFXGLStateBlockRef getCurrentStateBlock() { return mCurrentGLStateBlock; }
-   
+
    virtual void setupGenericShaders( GenericShaderType type = GSColor );
-   
+
    ///
    bool supportsAnisotropic() const { return mCapabilities.anisotropicFiltering; }
 
@@ -158,16 +159,16 @@ public:
 
    GFXTextureObject* getDefaultDepthTex() const;
 
-   /// Returns the number of vertex streams supported by the device.	
+   /// Returns the number of vertex streams supported by the device.
    const U32 getNumVertexStreams() const { return mNumVertexStream; }
 
-   bool glUseMap() const { return mUseGlMap; }   
+   bool glUseMap() const { return mUseGlMap; }
    const char* interpretDebugResult(long result) { return "Not Implemented"; };
-protected:   
+protected:
    /// Called by GFXDevice to create a device specific stateblock
    virtual GFXStateBlockRef createStateBlockInternal(const GFXStateBlockDesc& desc);
    /// Called by GFXDevice to actually set a stateblock.
-   virtual void setStateBlockInternal(GFXStateBlock* block, bool force);   
+   virtual void setStateBlockInternal(GFXStateBlock* block, bool force);
 
    /// Called by base GFXDevice to actually set a const buffer
    virtual void setShaderConstBufferInternal(GFXShaderConstBuffer* buffer);
@@ -184,13 +185,13 @@ protected:
    /// is created.
    virtual void initStates() { }
 
-   virtual GFXVertexBuffer *allocVertexBuffer(  U32 numVerts, 
+   virtual GFXVertexBuffer *allocVertexBuffer(  U32 numVerts,
                                                 const GFXVertexFormat *vertexFormat,
-                                                U32 vertSize, 
+                                                U32 vertSize,
                                                 GFXBufferType bufferType,
                                                 void* data = NULL);
    virtual GFXPrimitiveBuffer *allocPrimitiveBuffer( U32 numIndices, U32 numPrimitives, GFXBufferType bufferType, void* data = NULL );
-   
+
    // 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.
@@ -199,11 +200,11 @@ protected:
    virtual void setVertexDecl( const GFXVertexDecl *decl );
 
    virtual void setVertexStream( U32 stream, GFXVertexBuffer *buffer );
-   virtual void setVertexStreamFrequency( U32 stream, U32 frequency );   
-
+   virtual void setVertexStreamFrequency( U32 stream, U32 frequency );
+   StrongRefPtr<GFXGLShaderConstBuffer> mCurrentConstBuffer;
 private:
    typedef GFXDevice Parent;
-   
+
    friend class GFXGLTextureObject;
    friend class GFXGLCubemap;
    friend class GFXGLCubemapArray;
@@ -215,18 +216,18 @@ private:
    static GFXAdapter::CreateDeviceInstanceDelegate mCreateDeviceInstance;
 
    U32 mAdapterIndex;
-   
+
    StrongRefPtr<GFXGLVertexBuffer> mCurrentVB[VERTEX_STREAM_COUNT];
    U32 mCurrentVB_Divisor[VERTEX_STREAM_COUNT];
    bool mNeedUpdateVertexAttrib;
    StrongRefPtr<GFXGLPrimitiveBuffer> mCurrentPB;
    U32 mDrawInstancesCount;
-   
+
    GFXShader* mCurrentShader;
    GFXShaderRef mGenericShader[GS_COUNT];
    GFXShaderConstBufferRef mGenericShaderBuffer[GS_COUNT];
    GFXShaderConstHandle *mModelViewProjSC[GS_COUNT];
-   
+
    /// Since GL does not have separate world and view matrices we need to track them
    MatrixF m_mCurrentWorld;
    MatrixF m_mCurrentView;
@@ -237,34 +238,34 @@ private:
    F32 mPixelShaderVersion;
 
    U32 mNumVertexStream;
-   
+
    U32 mMaxShaderTextures;
    U32 mMaxFFTextures;
 
    U32 mMaxTRColors;
 
    RectI mClip;
-   
+
    GFXGLStateBlockRef mCurrentGLStateBlock;
-   
+
    GLenum mActiveTextureType[GFX_TEXTURE_STAGE_COUNT];
-   
+
    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();
-   void postDrawPrimitive(U32 primitiveCount);  
-   
+   void postDrawPrimitive(U32 primitiveCount);
+
    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
 
    void vsyncCallback(); ///< Vsync callback
-   
+
    void initGLState(); ///< Guaranteed to be called after all extensions have been loaded, use to init card profiler, shader version, max samplers, etc.
-   
+
    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
-   
+
    void setPB(GFXGLPrimitiveBuffer* pb); ///< Sets mCurrentPB
 
    GFXGLStateCache *mOpenglStateCache;

File diff suppressed because it is too large
+ 507 - 263
Engine/source/gfx/gl/gfxGLShader.cpp


+ 146 - 70
Engine/source/gfx/gl/gfxGLShader.h

@@ -29,96 +29,95 @@
 #include "core/util/tSignal.h"
 #include "core/util/tDictionary.h"
 
-class GFXGLShaderConstHandle;
 class FileStream;
-class GFXGLShaderConstBuffer;
 class GFXGLDevice;
+class GFXGLShader;
 
-class GFXGLShader : public GFXShader
+struct BufferRange
 {
-   typedef Map<String, GFXGLShaderConstHandle*> HandleMap;
-public:
-   GFXGLShader(GFXGLDevice* device);
-   virtual ~GFXGLShader();
-
-   /// @name GFXShader interface
-   /// @{
-   virtual GFXShaderConstHandle* getShaderConstHandle(const String& name);
-   virtual GFXShaderConstHandle* findShaderConstHandle(const String& name);
-
-   /// Returns our list of shader constants, the material can get this and just set the constants it knows about
-   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
-
-   /// Returns the alignment value for constType
-   virtual U32 getAlignmentValue(const GFXShaderConstType constType) const;
+   U32 mBufMin = U32_MAX;
+   U32 mBufMax = 0;
 
-   virtual GFXShaderConstBufferRef allocConstBuffer();
-
-   /// @}
-
-   /// @name GFXResource interface
-   /// @{
-   virtual void zombify();
-   virtual void resurrect() { reload(); }
-   virtual const String describeSelf() const;
-   /// @}
-
-   /// Activates this shader in the GL context.
-   void useProgram();
-
-protected:
+   inline void addSlot(U32 slot)
+   {
+      mBufMin = getMin(mBufMin, slot);
+      mBufMax = getMax(mBufMax, slot);
+   }
 
-   friend class GFXGLShaderConstBuffer;
-   friend class GFXGLShaderConstHandle;
-
-   virtual bool _init();
-
-   bool initShader(  const Torque::Path &file,
-                     GFXShaderStage stage,
-                     const Vector<GFXShaderMacro> &macros );
-
-   void clearShaders();
-   void initConstantDescs();
-   void initHandles();
-   void setConstantsFromBuffer(GFXGLShaderConstBuffer* buffer);
-
-   static char* _handleIncludes( const Torque::Path &path, FileStream *s );
+   inline bool isValid() const { return mBufMin <= mBufMax; }
+};
 
-   static bool _loadShaderFromStream(  GLuint shader,
-                                       const Torque::Path& path,
-                                       FileStream* s,
-                                       const Vector<GFXShaderMacro>& macros );
+struct ConstantBuffer
+{
+   GLuint bufHandle;
+   U8* data;
+   U32 size;
+   bool isDirty;
+};
 
-   /// @name Internal GL handles
-   /// @{
-   GLuint mVertexShader;
-   GLuint mPixelShader;
-   GLuint mGeometryShader;
-   GLuint mProgram;
-   /// @}
+class GFXGLShaderConstHandle : public GFXShaderConstHandle
+{
+   friend class GFXGLShader;
 
-   Vector<GFXShaderConstDesc> mConstants;
-   U32 mConstBufferSize;
-   U8* mConstBuffer;
-   HandleMap mHandles;
-   GFXGLDevice* mDevice;
-   Vector<GFXGLShaderConstHandle*> mValidHandles;
+public:
+   // DX side needs the description map as the same uniform can exist across stages. for gl it is program wide.
+   GFXGLShaderConstHandle(GFXGLShader* shader);
+   GFXGLShaderConstHandle(GFXGLShader* shader,
+      const GFXShaderConstDesc& desc);
+
+   void reinit(const GFXShaderConstDesc& desc);
+
+   virtual ~GFXGLShaderConstHandle();
+   const GFXShaderConstDesc getDesc();
+   const String& getName() const { return mDesc.name; }
+   GFXShaderConstType getType() const { return mDesc.constType; }
+   U32 getArraySize() const { return mDesc.arraySize; }
+
+   U32 getSize() const { return mDesc.size; }
+   void setValid(bool valid) { mValid = valid; }
+   /// @warning This will always return the value assigned when the shader was
+   /// initialized.  If the value is later changed this method won't reflect that.
+   S32 getSamplerRegister() const { return (!isSampler() || !mValid) ? -1 : mDesc.samplerReg; }
+
+   // Returns true if this is a handle to a sampler register.
+   bool isSampler() const
+   {
+      return (getType() >= GFXSCT_Sampler);
+   }
+
+   /// Restore to uninitialized state.
+   void clear()
+   {
+      mShader = NULL;
+      mInstancingConstant = false;
+      mValid = false;
+   }
+
+   GFXShaderConstDesc mDesc;
+   GFXGLShader* mShader;
+   bool mUBOUniform;
+   bool mInstancingConstant;
 };
 
 class GFXGLShaderConstBuffer : public GFXShaderConstBuffer
 {
 public:
-   GFXGLShaderConstBuffer(GFXGLShader* shader, U32 bufSize, U8* existingConstants);
+   // -1 is the global buffer.
+   typedef Map<S32, ConstantBuffer> BufferMap;
+
+   GFXGLShaderConstBuffer(GFXGLShader* shader);
    ~GFXGLShaderConstBuffer();
 
    /// Called by GFXGLDevice to activate this buffer.
-   void activate();
+   void activate(GFXGLShaderConstBuffer* prevShaderBuffer);
+
+   void addBuffer(S32 bufBindingPoint, U32 size);
 
    /// Called when the shader this buffer references is reloaded.
-   void onShaderReload( GFXGLShader *shader );
+   void onShaderReload(GFXGLShader* shader);
 
    // GFXShaderConstBuffer
-   virtual GFXShader* getShader() { return mShader; }
+   virtual GFXShader* getShader();
    virtual void set(GFXShaderConstHandle* handle, const F32 fv);
    virtual void set(GFXShaderConstHandle* handle, const Point2F& fv);
    virtual void set(GFXShaderConstHandle* handle, const Point3F& fv);
@@ -148,8 +147,9 @@ public:
 private:
 
    friend class GFXGLShader;
-   U8* mBuffer;
+
    WeakRefPtr<GFXGLShader> mShader;
+   BufferMap mBufferMap;
 
    template<typename ConstType>
    void internalSet(GFXShaderConstHandle* handle, const ConstType& param);
@@ -158,4 +158,80 @@ private:
    void internalSet(GFXShaderConstHandle* handle, const AlignedArray<ConstType>& fv);
 };
 
+class GFXGLShader : public GFXShader
+{
+   friend class GFXGLShaderConstBuffer;
+   friend class GFXGLShaderConstHandle;
+public:
+   typedef Map<String, GFXGLShaderConstHandle*> HandleMap;
+   typedef Map<String, GFXShaderConstDesc> BufferMap;
+
+   GFXGLShader(GFXGLDevice* device);
+   virtual ~GFXGLShader();
+
+   /// @name GFXShader interface
+   /// @{
+   virtual GFXShaderConstHandle* getShaderConstHandle(const String& name);
+   virtual GFXShaderConstHandle* findShaderConstHandle(const String& name);
+
+   /// Returns our list of shader constants, the material can get this and just set the constants it knows about
+   virtual const Vector<GFXShaderConstDesc>& getShaderConstDesc() const;
+
+   /// Returns the alignment value for constType
+   virtual U32 getAlignmentValue(const GFXShaderConstType constType) const;
+
+   virtual GFXShaderConstBufferRef allocConstBuffer();
+
+   /// @}
+
+   /// @name GFXResource interface
+   /// @{
+   virtual void zombify();
+   virtual void resurrect() { reload(); }
+   virtual const String describeSelf() const;
+   /// @}
+
+   /// Activates this shader in the GL context.
+   void useProgram();
+
+protected:
+   virtual bool _init();
+
+   bool initShader(const Torque::Path& file,
+      GFXShaderStage stage,
+      const Vector<GFXShaderMacro>& macros);
+
+   void clearShaders();
+
+   void initConstantDescs();
+   void initHandles();
+   void setConstantsFromBuffer(U8* buffer);
+
+   static char* _handleIncludes(const Torque::Path& path, FileStream* s);
+
+   static bool _loadShaderFromStream(GLuint shader,
+      const Torque::Path& path,
+      FileStream* s,
+      const Vector<GFXShaderMacro>& macros);
+
+   /// @name Internal GL handles
+   /// @{
+   GLuint mVertexShader;
+   GLuint mPixelShader;
+   GLuint mGeometryShader;
+   GLuint mProgram;
+   /// @}
+
+   U8* mGlobalConstBuffer;
+
+   Vector<GFXShaderConstDesc> mShaderConsts;
+
+   HandleMap mHandles;
+   BufferMap mBuffers;
+
+   GFXGLDevice* mDevice;
+
+   GFXShaderConstType convertConstType(GLenum constType);
+};
+
 #endif // _GFXGLSHADER_H_

Some files were not shown because too many files changed in this diff