bool Material::bind(RenderContext* context) { if( this == context->getBoundMaterial() ) { return false; } ID3DX11Effect* effect = m_shader->getEffect(); context->getImmediateContext()->IASetInputLayout( m_layout ); for(std::map<string, ResourceID>::iterator iter = m_textureRefs.begin(); iter!=m_textureRefs.end(); ++iter) { ID3DX11EffectVariable* var = effect->GetVariableByName(iter->first.c_str()); if(var->IsValid()) { ID3DX11EffectShaderResourceVariable* texResource = var->AsShaderResource(); Texture* tex = TextureManager::singleton()->getTexture(iter->second); if(tex) { texResource->SetResource(tex->getShaderResourceView()); } else { texResource->SetResource(0); } } } return true; }
void D3D11EffectMaterial::SetTextureBySemantic(const char* szName, TexturePtr pTex) { ID3D11ShaderResourceView* pView = NULL; pView = ((D3D11Texture*)pTex.get())->GetShaderResourceView(); ID3DX11EffectVariable* pVal = m_pEffect->GetVariableBySemantic(szName); if(pVal) { pVal->AsShaderResource()->SetResource(pView); } }
bool cSkyBoxEffect::Init( ID3D11Device* pDevice, ID3D11DeviceContext* pContext ) { if ( !Base::LoadFXFile( mEffectFileName, pDevice ) ) { return false; } cMeshGenerator gen; cMeshGenerator::cMesh mesh; gen.CreateSphere( 10000.f, 20, 20, mesh ); unsigned int numVerts = mesh.VerticesA.size(); unsigned int numInds = mesh.Indices.size(); if ( !mBuffers.Init( pDevice, numVerts, numInds ) ) { return false; } // map the vertices sVert_P* verts = mBuffers.BeginVertexMapping( pContext ); for ( unsigned int idxVert = 0; idxVert < numVerts; idxVert++ ) { verts[idxVert].Position = mesh.VerticesA[idxVert].Position; verts[idxVert].PositionPad = 1.f; } mBuffers.FinishVertexMapping( pContext ); // map the indices unsigned int* inds = mBuffers.BeginIndexMapping( pContext ); for ( unsigned int idxIndex = 0; idxIndex < numInds; idxIndex++ ) { inds[idxIndex] = mesh.Indices[idxIndex]; } mBuffers.FinishIndexMapping( pContext ); mTechnique = mEffect->GetTechniqueByIndex(0); if ( !mTechnique->IsValid() ) { return false; } D3DX11_PASS_DESC passDesc; mTechnique->GetPassByIndex(0)->GetDesc(&passDesc); pDevice->CreateInputLayout( sVert_P::GetElementDescPtr(), sVert_P::NUM_ELEMENTS, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mLayout ); mTransform = mEffect->GetVariableByName("WorldViewProj"); if ( mTransform->IsValid() ) { NecessaryPerFrame.push_back( new cMatrixEffectVariable( mTransform ) ); } ID3DX11EffectVariable* boxVar = mEffect->GetVariableByName("CubeMap"); if ( boxVar->IsValid() ) { NecessaryPerFrame.push_back( new cShaderResourceEffectVariable( boxVar ) ); } mSRV = boxVar->AsShaderResource(); return true; }
void hwRendererHelperDX::initializeDrawTextureEffect() { if( fDXContext == NULL ) return; // Create the effect if( fDrawTextureEffect == NULL ) { const char* simpleShaderCode = "Texture2D myTexture; \r\n" \ "SamplerState SamplerLinearWrap \r\n" \ "{ \r\n" \ " Filter = MIN_MAG_MIP_LINEAR; \r\n" \ " AddressU = Wrap; \r\n" \ " AddressV = Wrap; \r\n" \ "}; \r\n" \ "struct APP_TO_VS \r\n" \ "{ \r\n" \ " float3 Pos : POSITION; \r\n" \ " float2 TextCoord : TEXTCOORD; \r\n" \ "}; \r\n" \ "struct VS_TO_PS \r\n" \ "{ \r\n" \ " float4 Pos : SV_Position; \r\n" \ " float2 TextCoord : TEXTCOORD; \r\n" \ "}; \r\n" \ "VS_TO_PS BasicVS(APP_TO_VS IN) \r\n" \ "{ \r\n" \ " VS_TO_PS OUT; \r\n" \ " OUT.Pos = float4(IN.Pos, 1.0f); \r\n" \ " OUT.TextCoord = IN.TextCoord; \r\n" \ " return OUT; \r\n" \ "} \r\n" \ "float4 BasicPS(VS_TO_PS IN) : SV_Target \r\n" \ "{ \r\n" \ " float4 color = myTexture.Sample(SamplerLinearWrap, IN.TextCoord); \r\n" \ " return color; \r\n" \ "} \r\n" \ "technique10 simple \r\n" \ "{ \r\n" \ " pass p0 \r\n" \ " { \r\n" \ " SetVertexShader( CompileShader( vs_4_0, BasicVS() ) ); \r\n" \ " SetGeometryShader( NULL ); \r\n" \ " SetPixelShader( CompileShader( ps_4_0, BasicPS() ) ); \r\n" \ " } \r\n" \ "} \r\n"; const unsigned int simpleShaderLength = (unsigned int)strlen(simpleShaderCode); const D3D10_SHADER_MACRO macros[] = { { "DIRECT3D_VERSION", "0xb00" }, { NULL, NULL } }; #ifdef _DEBUG const unsigned int flags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION; #else const unsigned int flags = 0; #endif ID3DBlob *shader = NULL; ID3DBlob *error = NULL; #if _MSC_VER >= 1700 HRESULT hr = D3DCompile((char*)simpleShaderCode, simpleShaderLength, NULL, macros, NULL, "", "fx_5_0", flags, 0, &shader, &error); #else HRESULT hr = D3DX11CompileFromMemory((char*)simpleShaderCode, simpleShaderLength, NULL, macros, NULL, "", "fx_5_0", flags, 0, NULL, &shader, &error, NULL); #endif if( SUCCEEDED( hr ) && shader != NULL ) { hr = D3DX11CreateEffectFromMemory(shader->GetBufferPointer(), shader->GetBufferSize(), 0, fDXDevice, &fDrawTextureEffect); if( SUCCEEDED( hr ) && fDrawTextureEffect != NULL ) { ID3DX11EffectVariable* textureVariable = fDrawTextureEffect->GetVariableByName( "myTexture" ); if( textureVariable != NULL ) { fDrawTextureShaderVariable = textureVariable->AsShaderResource(); } ID3DX11EffectTechnique* technique = fDrawTextureEffect->GetTechniqueByIndex(0); if( technique ) { fDrawTexturePass = technique->GetPassByIndex(0); } } } else { MString errorStr( (error && error->GetBufferSize() > 0) ? (char*) error->GetBufferPointer() : (char*) NULL ); MGlobal::displayWarning( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kDxErrorEffect, errorStr ) ); } if( shader != NULL ) { shader->Release(); } } // Create the vertex buffers and the input layout if( fDrawTextureInputLayout == NULL && fDrawTexturePass != NULL ) { fDrawTextureVertexBuffersCount = 0; D3D11_INPUT_ELEMENT_DESC inputDesc[MAX_VERTEX_BUFFERS]; HRESULT hr; // Create the position stream { const float position[] = { -1, -1, 0, // bottom-left -1, 1, 0, // top-left 1, 1, 0, // top-right 1, -1, 0 }; // bottom-right const D3D11_BUFFER_DESC bufDesc = { sizeof(position), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { position, 0, 0 }; ID3D11Buffer *buffer = NULL; hr = fDXDevice->CreateBuffer(&bufDesc, &bufData, &buffer); if( SUCCEEDED( hr ) && buffer != NULL ) { inputDesc[fDrawTextureVertexBuffersCount].SemanticName = "POSITION"; inputDesc[fDrawTextureVertexBuffersCount].SemanticIndex = 0; inputDesc[fDrawTextureVertexBuffersCount].Format = DXGI_FORMAT_R32G32B32_FLOAT; inputDesc[fDrawTextureVertexBuffersCount].InputSlot = fDrawTextureVertexBuffersCount; inputDesc[fDrawTextureVertexBuffersCount].AlignedByteOffset = 0; inputDesc[fDrawTextureVertexBuffersCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputDesc[fDrawTextureVertexBuffersCount].InstanceDataStepRate = 0; fDrawTextureVertexBuffers[fDrawTextureVertexBuffersCount] = buffer; fDrawTextureVertexBuffersStrides[fDrawTextureVertexBuffersCount] = 3 * sizeof(float); fDrawTextureVertexBuffersOffsets[fDrawTextureVertexBuffersCount] = 0; ++fDrawTextureVertexBuffersCount; } } // Create the texture coord stream if( SUCCEEDED( hr ) ) { const float textCoord[] = { 0, 1, // bottom-left 0, 0, // top-left 1, 0, // top-right 1, 1 }; // bottom-right const D3D11_BUFFER_DESC bufDesc = { sizeof(textCoord), D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { textCoord, 0, 0 }; ID3D11Buffer *buffer = NULL; hr = fDXDevice->CreateBuffer(&bufDesc, &bufData, &buffer); if( SUCCEEDED( hr ) && buffer != NULL ) { inputDesc[fDrawTextureVertexBuffersCount].SemanticName = "TEXTCOORD"; inputDesc[fDrawTextureVertexBuffersCount].SemanticIndex = 0; inputDesc[fDrawTextureVertexBuffersCount].Format = DXGI_FORMAT_R32G32_FLOAT; inputDesc[fDrawTextureVertexBuffersCount].InputSlot = fDrawTextureVertexBuffersCount; inputDesc[fDrawTextureVertexBuffersCount].AlignedByteOffset = 0; inputDesc[fDrawTextureVertexBuffersCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputDesc[fDrawTextureVertexBuffersCount].InstanceDataStepRate = 0; fDrawTextureVertexBuffers[fDrawTextureVertexBuffersCount] = buffer; fDrawTextureVertexBuffersStrides[fDrawTextureVertexBuffersCount] = 2 * sizeof(float); fDrawTextureVertexBuffersOffsets[fDrawTextureVertexBuffersCount] = 0; ++fDrawTextureVertexBuffersCount; } } if( SUCCEEDED( hr ) ) { D3DX11_PASS_DESC descPass; fDrawTexturePass->GetDesc(&descPass); hr = fDXDevice->CreateInputLayout(inputDesc, fDrawTextureVertexBuffersCount, descPass.pIAInputSignature, descPass.IAInputSignatureSize, &fDrawTextureInputLayout); if( FAILED( hr ) ) { MGlobal::displayWarning( hwApiTextureTestStrings::getString( hwApiTextureTestStrings::kDxErrorInputLayout ) ); } } } // Create the index buffer if( fDrawTextureIndexBuffer == NULL && fDrawTextureVertexBuffersCount > 0 && fDrawTextureInputLayout != NULL ) { const unsigned int indices[] = { 0, 1, 3, 3, 2, 1 }; fDrawTextureIndexBufferCount = _countof(indices); const D3D11_BUFFER_DESC bufDesc = { sizeof(indices), D3D11_USAGE_IMMUTABLE, D3D11_BIND_INDEX_BUFFER, 0, 0, 0 }; const D3D11_SUBRESOURCE_DATA bufData = { indices, 0, 0 }; fDXDevice->CreateBuffer(&bufDesc, &bufData, &fDrawTextureIndexBuffer); } }
void TextureModelDemo::Initialize() { SetCurrentDirectory(Utility::ExecutableDirectory().c_str()); // Compile the shader UINT shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3DCOMPILE_DEBUG; shaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3D10Blob* compiledShader = nullptr; ID3D10Blob* errorMessages = nullptr; HRESULT hr = D3DCompileFromFile(L"..\\source\\Library\\Content\\Effects\\TextureMapping.fx", nullptr, nullptr, nullptr, "fx_5_0", shaderFlags, 0, &compiledShader, &errorMessages); if (FAILED(hr)) { char* errorMessage = (errorMessages != nullptr ? (char*)errorMessages->GetBufferPointer() : "D3DX11CompileFromFile() failed"); GameException ex(errorMessage, hr); ReleaseObject(errorMessages); throw ex; } // Create an effect object from the compiled shader hr = D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, mGame->Direct3DDevice(), &mEffect); if (FAILED(hr)) { throw GameException("D3DX11CreateEffectFromMemory() failed.", hr); } ReleaseObject(compiledShader); // Look up the technique, pass, and WVP variable from the effect mTechnique = mEffect->GetTechniqueByName("main11"); if (mTechnique == nullptr) { throw GameException("ID3DX11Effect::GetTechniqueByName() could not find the specified technique.", hr); } mPass = mTechnique->GetPassByName("p0"); if (mPass == nullptr) { throw GameException("ID3DX11EffectTechnique::GetPassByName() could not find the specified pass.", hr); } ID3DX11EffectVariable* variable = mEffect->GetVariableByName("WorldViewProjection"); if (variable == nullptr) { throw GameException("ID3DX11Effect::GetVariableByName() could not find the specified variable.", hr); } mWvpVariable = variable->AsMatrix(); if (mWvpVariable->IsValid() == false) { throw GameException("Invalid effect variable cast."); } variable = mEffect->GetVariableByName("ColorTexture"); if (variable == nullptr) { throw GameException("ID3DX11Effect::GetVariableByName() could not find the specified variable.", hr); } mColorTextureVariable = variable->AsShaderResource(); if (mColorTextureVariable->IsValid() == false) { throw GameException("Invalid effect variable cast."); } // Create the input layout D3DX11_PASS_DESC passDesc; mPass->GetDesc(&passDesc); D3D11_INPUT_ELEMENT_DESC inputElementDescriptions[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; if (FAILED(hr = mGame->Direct3DDevice()->CreateInputLayout(inputElementDescriptions, ARRAYSIZE(inputElementDescriptions), passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &mInputLayout))) { throw GameException("ID3D11Device::CreateInputLayout() failed.", hr); } // Load the model std::unique_ptr<Model> model(new Model(*mGame, "..\\source\\Library\\Content\\Models\\Sphere.obj", true)); // Create the vertex and index buffers Mesh* mesh = model->Meshes().at(0); CreateVertexBuffer(mGame->Direct3DDevice(), *mesh, &mVertexBuffer); mesh->CreateIndexBuffer(&mIndexBuffer); mIndexCount = mesh->Indices().size(); // Load the texture std::wstring textureName = L"..\\source\\Library\\Content\\Textures\\EarthComposite.jpg"; if (FAILED(hr = DirectX::CreateWICTextureFromFile(mGame->Direct3DDevice(), mGame->Direct3DDeviceContext(), textureName.c_str(), nullptr, &mTextureShaderResourceView))) { throw GameException("CreateWICTextureFromFile() failed.", hr); } }