//-----------------------------------------------------------------------------
void CPUTMaterialDX11::BindTextures( CPUTShaderParameters &params, const CPUTModel *pModel, int meshIndex )
{
    CPUTAssetLibraryDX11 *pAssetLibrary = (CPUTAssetLibraryDX11*)CPUTAssetLibrary::GetAssetLibrary();

    for(params.mTextureCount=0; params.mTextureCount < params.mTextureParameterCount; params.mTextureCount++)
    {
        cString textureName;
        UINT textureCount = params.mTextureCount;
        cString tagName = params.mpTextureParameterName[textureCount];
        CPUTConfigEntry *pValue = mConfigBlock.GetValueByName(tagName);
        if( !pValue->IsValid() )
        {
            // We didn't find our property in the file.  Is it in the global config block?
            pValue = mGlobalProperties.GetValueByName(tagName);
        }
        ASSERT( pValue->IsValid(), L"Can't find texture '" + tagName + L"'." ); //  TODO: fix message
        textureName = pValue->ValueAsString();
        // If the texture name not specified.  Load default.dds instead
        if( 0 == textureName.length() ) { textureName = _L("default.dds"); }

        UINT bindPoint = params.mpTextureParameterBindPoint[textureCount]; 
        ASSERT( bindPoint < CPUT_MATERIAL_MAX_TEXTURE_SLOTS, _L("Texture bind point out of range.") );

        if( textureName[0] == '@' )
        {
            // This is a per-mesh value.  Add to per-mesh list.
            textureName += ptoc(pModel) + itoc(meshIndex);
        } else if( textureName[0] == '#' )
        {
            // This is a per-mesh value.  Add to per-mesh list.
            textureName += ptoc(pModel);
        }

        // Get the sRGB flag (default to true)
        cString SRGBName = tagName+_L("sRGB");
        CPUTConfigEntry *pSRGBValue = mConfigBlock.GetValueByName(SRGBName);
        bool loadAsSRGB = pSRGBValue->IsValid() ?  loadAsSRGB = pSRGBValue->ValueAsBool() : true;

        if( !mpTexture[textureCount] )
        {
            mpTexture[textureCount] = pAssetLibrary->GetTexture( textureName, false, loadAsSRGB );
            ASSERT( mpTexture[textureCount], _L("Failed getting texture ") + textureName);
        }

        // The shader file (e.g. .fx) can specify the texture bind point (e.g., t0).  Those specifications 
        // might not be contiguous, and there might be gaps (bind points without assigned textures)
        // TODO: Warn about missing bind points?
        params.mppBindViews[bindPoint] = ((CPUTTextureDX11*)mpTexture[textureCount])->GetShaderResourceView();
		params.mppBindViews[bindPoint]->AddRef();
    }
}
void CPUTMaterialEffectOGL::BindUAVs( CPUTShaderParameters &params, const CPUTModel *pModel, int meshIndex )
{
#ifdef CPUT_SUPPORT_IMAGE_STORE

    CPUTAssetLibraryOGL *pAssetLibrary = (CPUTAssetLibraryOGL*)CPUTAssetLibrary::GetAssetLibrary();

    for(params.mUAVCount=0; params.mUAVCount < params.mUAVParameterCount; params.mUAVCount++)
    {
        cString UAVName;
        unsigned int UAVCount = params.mUAVCount;

// Dirty fix
#ifndef CPUT_OS_WINDOWS
        cString tagName = params.mpUAVParameterNames[UAVCount];
#else
        cString tagName = s2ws(params.mpUAVParameterNames[UAVCount].c_str());
#endif

        CPUTConfigEntry *pValue = mConfigBlock.GetValueByName(tagName);
        if( !pValue->IsValid() )
        {
            // We didn't find our property in the file.  Is it in the global config block?
            pValue = CPUTMaterial::mGlobalProperties.GetValueByName(tagName);
        }
        ASSERT( pValue->IsValid(), L"Can't find UAV '" + tagName + L"'." ); //  TODO: fix message
        UAVName = pValue->ValueAsString();
        // If the UAV name not specified.  Load default.dds instead
        if( 0 == UAVName.length() ) 
        { 
            UAVName = _L("default.dds"); 
        }

        UINT bindPoint = params.mUAVCount;//params.mpUAVParameterBindPoints[UAVCount];
        params.mpUAVParameterBindPoints.push_back(bindPoint);
//        UINT bindPoint = params.mpUAVParameterBindPoint[UAVCount]; mpUAVParameterBindPoints
        ASSERT( bindPoint < CPUT_MATERIAL_MAX_UAV_SLOTS, _L("UAV bind point out of range.") );

        params.mBindViewMin = std::min( params.mBindViewMin, bindPoint );
        params.mBindViewMax = std::max( params.mBindViewMax, bindPoint );

        if( UAVName[0] == '@' )
        {
            // This is a per-mesh value.  Add to per-mesh list.
            UAVName += ptoc(pModel) + itoc(meshIndex);
        } else if( UAVName[0] == '#' )
        {
            // This is a per-mesh value.  Add to per-mesh list.
            UAVName += ptoc(pModel);
        }

        // Get the sRGB flag (default to true)
//#ifndef CPUT_OS_WINDOWS
        cString SRGBName = tagName + _L("sRGB");
//#else
//        cString SRGBName = tagName + L"sRGB";
//#endif
        CPUTConfigEntry *pSRGBValue = mConfigBlock.GetValueByName(SRGBName);
        bool loadAsSRGB = pSRGBValue->IsValid() ?  loadAsSRGB = pSRGBValue->ValueAsBool() : true;

        if( !params.mpUAV[UAVCount] )
        {
            params.mpUAV[UAVCount] = pAssetLibrary->GetTexture( UAVName, false, loadAsSRGB );
            ASSERT( params.mpUAV[UAVCount], _L("Failed getting UAV ") + UAVName);
        }

        cString ReadName = tagName + _L("READ");
        CPUTConfigEntry *pRead = mConfigBlock.GetValueByName(ReadName);
        bool read = pRead->IsValid() ? pRead->ValueAsBool() : true;
        cString WriteName = tagName + _L("WRITE");
        CPUTConfigEntry *pWrite = mConfigBlock.GetValueByName(WriteName);
        bool write = pWrite->IsValid() ? pWrite  ->ValueAsBool() : true;
        if(write && read)
            params.mpUAVMode[UAVCount] = GL_READ_WRITE;
        else if(read)
            params.mpUAVMode[UAVCount] = GL_READ_ONLY;
        else
            params.mpUAVMode[UAVCount] = GL_WRITE_ONLY;

        // The shader file (e.g. .fx) can specify the UAV bind point (e.g., t0).  Those specifications 
        // might not be contiguous, and there might be gaps (bind points without assigned UAVs)
        // TODO: Warn about missing bind points?
//        params.mppBindViews[bindPoint] = ((CPUTTextureOGL*)mpTexture[textureCount])->GetShaderResourceView();
//        params.mppBindViews[bindPoint]->AddRef();

        //
        // Match up the UAV name in any UAV samplers given in the renderstate file. If there wasn't
        // one specified for a particular UAV then it just uses the default sampler.
        //
        CPUTRenderStateOGL *pRenderState;
        pRenderState = ((CPUTRenderStateBlockOGL*)mpRenderStateBlock)->GetState();
        
        mSamplerIDs[UAVCount] = pRenderState->DefaultSamplerID;
        for (int renderStateIDX = 0; renderStateIDX < NUM_SAMPLERS_PER_RENDERSTATE; renderStateIDX++) {
			if(renderStateIDX<((CPUTRenderStateBlockOGL*)mpRenderStateBlock)->GetNumSamplers())
            {
                mSamplerIDs[UAVCount] = pRenderState->SamplerIDs[renderStateIDX];
            }
        }
    }
#endif

}
bool CPUTGUIElement::Load(CPUTConfigBlock* pBlock, int* pParent)
{
    CPUTConfigEntry* pEntry = NULL;

    pEntry = pBlock->GetValueByName(NAME);
    if (pEntry->IsValid())
        mName= pEntry->ValueAsString();

    pEntry = pBlock->GetValueByName(PARENT);
    if(pEntry->IsValid())
        *pParent = pEntry->ValueAsInt();

    pEntry = pBlock->GetValueByName(WIDTH);
    if(pEntry->IsValid())
        mWidth = pEntry->ValueAsInt();

    pEntry = pBlock->GetValueByName(HEIGHT);
    if(pEntry->IsValid())
        mHeight = pEntry->ValueAsInt();
    
    pEntry = pBlock->GetValueByName(POS_X);
    if(pEntry->IsValid())
        mRelX = pEntry->ValueAsInt();

    pEntry = pBlock->GetValueByName(POS_Y);
    if(pEntry->IsValid())
        mRelY = pEntry->ValueAsInt();
    
    pEntry = pBlock->GetValueByName(PARENT_RELATIVE);
    if(pEntry->IsValid())
        mParentRelative = pEntry->ValueAsBool();

    pEntry = pBlock->GetValueByName(VISIBLE);
    if(pEntry->IsValid())
        mVisible = pEntry->ValueAsBool();

    pEntry = pBlock->GetValueByName(FOREGROUND_COLOR);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsFloatArray((float*)&mForegroundColor, 4);
        mForegroundHighlightColor = mForegroundColor;
    }

    pEntry = pBlock->GetValueByName(BACKGROUND_COLOR);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsFloatArray((float*)&mBackgroundColor, 4);
        mBackgroundHighlightColor = mBackgroundColor;
    }
    pEntry = pBlock->GetValueByName(FOREGROUND_HIGHLIGHT_COLOR);
    if(pEntry->IsValid())
        pEntry->ValueAsFloatArray((float*)&mForegroundHighlightColor, 4);

    pEntry = pBlock->GetValueByName(BACKGROUND_HIGHLIGHT_COLOR);
    if(pEntry->IsValid())
        pEntry->ValueAsFloatArray((float*)&mBackgroundHighlightColor, 4);

    pEntry = pBlock->GetValueByName(MATERIAL);
    if(pEntry->IsValid())
    {
        cString materialName = pEntry->ValueAsString();
        mpSprite = CPUTSprite::CreateSprite(0.0, 0.0, 1.0, 1.0, materialName);
    }

    pEntry = pBlock->GetValueByName(TEXT_MATERIAL);
    if(pEntry->IsValid())
    {
        cString materialName = pEntry->ValueAsString();
        mpTextMaterial = CPUTAssetLibrary::GetAssetLibrary()->GetMaterial(materialName);
    }
    const cString FONT = _L("font");
    pEntry = pBlock->GetValueByName(FONT);
    if(pEntry->IsValid())
    {
        cString fontName;
        pEntry->ValueAsString(&fontName);
        mpFont = (CPUTFont*)CPUTAssetLibrary::GetAssetLibrary()->GetFontByName(fontName);
        if(mpFont == NULL)
        {
            DEBUG_PRINT(_L("Failed to load font: %s"), fontName.c_str());
        }
    }
    pEntry = pBlock->GetValueByName(TEXT);
    cString string;
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&string);
        SetText(string);
    }   

    pEntry = pBlock->GetValueByName(HIGHLIGHT);
    if(pEntry->IsValid())
    {
        mHighlighted = pEntry->ValueAsBool();
    }   

    pEntry = pBlock->GetValueByName(MOUSE_CLICK);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mClick.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(MOUSE_UP);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mUp.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(MOUSE_DOWN);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mDown.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(MOUSE_OUT);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mOut.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(MOUSE_IN);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mIn.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(HOTKEY);
    if(pEntry->IsValid())
    {
        cString key;
        pEntry->ValueAsString(&key);
        mHotkey = MapKey(key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(HOTKEY_UP);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mKeyUp.key);
        mEventHandler = true;
    }
    pEntry = pBlock->GetValueByName(HOTKEY_DOWN);
    if(pEntry->IsValid())
    {
        pEntry->ValueAsString(&mKeyDown.key);
        mEventHandler = true;
    }

    mParentMatrix = float4x4Translation((float)mPosX, (float)mPosY, 0.0);
    return true;
}