Exemplo n.º 1
0
bool cDirectX9HLShader::getShaderConstHandle( bool bVs, const s8 *sVarName, h32 &out )
{
	ID3DXConstantTable* pkTable = bVs ? mpkVsTable : mpkPsTable;
		/* check top level params */
	out = (h32)pkTable->GetConstantByName(0,sVarName);
	if( XISVALIDHANDLE(out) )
		return true;
	return false;
}
bool DX9VertexShaderConstantsCompiler::compile( const std::string& shaderCode, const char* entryFunction, std::vector< ShaderConstantDesc >& outConstants )
{
   // create a renderer
   IDirect3D9* d3d9 = Direct3DCreate9( D3D_SDK_VERSION );
   DX9Initializer initializer( *d3d9 );

   IDirect3DDevice9* compileDevice = initializer.createNullDevice();
   const char* shaderProfile = D3DXGetVertexShaderProfile( compileDevice );

   DWORD flags = D3DXSHADER_DEBUG;

   ID3DXBuffer* shaderBuf = NULL;
   ID3DXBuffer* errorsBuf = NULL;
   ID3DXConstantTable* shaderConstants = NULL;
   DX9ShaderIncludeLoader includesLoader;
   HRESULT res = D3DXCompileShader( shaderCode.c_str(), shaderCode.length(), NULL, &includesLoader, entryFunction, shaderProfile, flags, &shaderBuf, &errorsBuf, &shaderConstants );

   // interpret the results
   bool result = true;
   if ( FAILED(res) || shaderBuf == NULL )
   {
      if ( errorsBuf != NULL )
      {
         std::string compilationErrors = ( const char* )errorsBuf->GetBufferPointer();
         errorsBuf->Release();
         m_errorMsg = std::string( "Shader compilation error: " ) + compilationErrors;
      }
      else
      {
         m_errorMsg = translateDxError( "Error while compiling a shader", res );
      }

      result = false;
   }
   else
   {
      std::vector< D3DXCONSTANT_DESC > constants;
      ShaderConstantDesc newConstant;
      result = ShaderCompilerUtils::parseConstants( shaderConstants, m_errorMsg, constants );
      if ( result )
      {
         unsigned int constantsCount = constants.size();
         for ( unsigned int i = 0; i < constantsCount; ++i )
         {
            if ( createConstant( constants[i], newConstant ) )
            {
               outConstants.push_back( newConstant );
            }
         }
      }
   }

   // cleanup
   if ( shaderConstants )
   {
      shaderConstants->Release();
   }
   compileDevice->Release();
   d3d9->Release();

   return result;
}
Exemplo n.º 3
0
bool cDirectX9HLShader::setShaderConst( bool bVs, h32 in, const f32 *val, s32 count )
{
	ID3DXConstantTable* pkTable = bVs ? mpkVsTable : mpkPsTable;
	return FAILED( pkTable->SetFloatArray( gpkDirect3DDevice, reinterpret_cast<D3DXHANDLE>(in), val, count ) ) ?false : true;
}
Exemplo n.º 4
0
BOOL RenderMultiTex::Init(UINT width, UINT height, HWND hwnd, BOOL windowed, D3DDEVTYPE devType) {
    HRESULT hr = Render::Init(width, height, hwnd, windowed, devType);
    SGL_FAILED_DO(hr, MYTRACE_DX("Render::Init", hr); return FALSE);

    // Create geometry.

    IDirect3DVertexBuffer9* vb;
    m_D3DDev->CreateVertexBuffer(6 * sizeof(MultiTexVertex), D3DUSAGE_WRITEONLY, MultiTexVertex::FVF, D3DPOOL_MANAGED, &vb, NULL);
    m_QuadVB.Attach(vb);

    MultiTexVertex* v = NULL;
    vb->Lock(0, 0, (void**) &v, 0);

    v[0] = MultiTexVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
    v[1] = MultiTexVertex(-10.0f,  10.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
    v[2] = MultiTexVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);

    v[3] = MultiTexVertex(-10.0f, -10.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
    v[4] = MultiTexVertex( 10.0f,  10.0f, 5.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
    v[5] = MultiTexVertex( 10.0f, -10.0f, 5.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);

    vb->Unlock();

    // Compile shader
    ID3DXBuffer* shaderBuf = NULL;
    ID3DXBuffer* errBuf = NULL;

    ID3DXConstantTable* constTbl;
    hr = D3DXCompileShaderFromFile(MultiTexPSFile, NULL, NULL, "Main", "ps_1_1", D3DXSHADER_DEBUG | D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, &shaderBuf, &errBuf, &constTbl);
    // output any error messages
    if (errBuf != NULL) {
        MYTRACEA("D3DXCompileShaderFromFile failed: %s", (char*) errBuf->GetBufferPointer());
        SGL::Release(errBuf);
    }
    SGL_FAILED_DO(hr, MYTRACE_DX("D3DXCompileShaderFromFile", hr); return FALSE);
    m_MultiTexCT.Attach(constTbl);

    // Create Pixel Shader
    IDirect3DPixelShader9* shader;
    hr = m_D3DDev->CreatePixelShader((DWORD*) shaderBuf->GetBufferPointer(), &shader);
    SGL_FAILED_DO(hr, MYTRACE_DX("CreatePixelShader", hr); return FALSE);
    m_MultiTexPS.Attach(shader);

    SGL::Release(shaderBuf);

    // Load textures.

    IDirect3DTexture9* tex;
    D3DXCreateTextureFromFile(m_D3DDev, BaseTexFile, &tex);
    SGL_FAILED_DO(hr, MYTRACE_DX("D3DXCreateTextureFromFile", hr); return FALSE);
    m_BaseTex.Attach(tex);

    D3DXCreateTextureFromFile(m_D3DDev, SpotLightTexFile, &tex);
    SGL_FAILED_DO(hr, MYTRACE_DX("D3DXCreateTextureFromFile", hr); return FALSE);
    m_SpotLightTex.Attach(tex);

    D3DXCreateTextureFromFile(m_D3DDev, StringTexFile, &tex);
    SGL_FAILED_DO(hr, MYTRACE_DX("D3DXCreateTextureFromFile", hr); return FALSE);
    m_StringTex.Attach(tex);

    // Set Projection Matrix
    D3DXMATRIX P;
    D3DXMatrixPerspectiveFovLH(&P, D3DX_PI * 0.25f, (float) width / (float) height, 1.0f, 1000.0f);
    m_D3DDev->SetTransform(D3DTS_PROJECTION, &P);

    // Disable lighting.
    m_D3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);

    // Get Handles
    m_BaseTexHandle      = constTbl->GetConstantByName(NULL, "BaseTex");
    m_SpotLightTexHandle = constTbl->GetConstantByName(NULL, "SpotLightTex");
    m_StringTexHandle    = constTbl->GetConstantByName(NULL, "StringTex");

    // Set constant descriptions:
    UINT count;
    constTbl->GetConstantDesc(m_BaseTexHandle, &m_BaseTexDesc, &count);
    constTbl->GetConstantDesc(m_SpotLightTexHandle, &m_SpotLightTexDesc, &count);
    constTbl->GetConstantDesc(m_StringTexHandle, &m_StringTexDesc, &count);
    constTbl->SetDefaults(m_D3DDev);

    if (!InitFont())
        return FALSE;

    return TRUE;
}