void LightDemo::InitFX() { std::ifstream fin("fx/Lighting.fxo", std::ios::binary); OC_ASSERT(fin.good()); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, m_dxDevice.Get(), m_fx.GetAddressOf())); m_tech = m_fx->GetTechniqueByName("LightTech"); m_fxWorldViewProj = m_fx->GetVariableByName("gWorldViewProj")->AsMatrix(); m_fxWorld = m_fx->GetVariableByName("gWorld")->AsMatrix(); m_fxWorldInvTranspose = m_fx->GetVariableByName("gWorldInvTranspose")->AsMatrix(); m_fxEyePosW = m_fx->GetVariableByName("gEyePosW")->AsVector(); m_fxDirLight = m_fx->GetVariableByName("gDirLight"); m_fxPointLight = m_fx->GetVariableByName("gPointLight"); m_fxSpotLight = m_fx->GetVariableByName("gSpotLight"); m_fxMaterial = m_fx->GetVariableByName("gMaterial"); }
HRESULT Effect::CreateEffectFromMemory(ID3D11Device* device, const std::wstring& filename) { std::ifstream fin(filename.c_str(), std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); if (size == -1) return STG_E_FILENOTFOUND; std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DCOMPILE_DEBUG; #endif return D3DX11CreateEffectFromMemory(&compiledShader[0], size, dwShaderFlags, device, &(m_Effect)); }
void BoxApp::buildShaders() { // // Read compiled vertex and pixel shaders and create them. // std::ifstream fin; fin.open("HLSL/VertexShader.cso", std::ios::binary); fin.seekg(0, std::ios_base::end); size_t size = static_cast<size_t> (fin.tellg()); fin.seekg(0, std::ios_base::beg); ; std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); buildVertexLayout(compiledShader); HRESULT result = mDevice->CreateVertexShader(&compiledShader[0], size, nullptr, &mVertexShader); DxErrorChecker(result); fin.open("HLSL/PixelShader.cso", std::ios::binary); fin.seekg(0, std::ios_base::end); size = static_cast<size_t> (fin.tellg()); fin.seekg(0, std::ios_base::beg); compiledShader.resize(size); fin.read(&compiledShader[0], size); fin.close(); result = mDevice->CreatePixelShader(&compiledShader[0], size, nullptr, &mPixelShader); DxErrorChecker(result); // Bind shaders to the rendering pipeline mImmediateContext->VSSetShader(mVertexShader, nullptr, 0); mImmediateContext->PSSetShader(mPixelShader, nullptr, 0); }
void Renderable::createEffect(const char* filename, ID3D11Device* _dxDev, ID3DX11Effect** fx) { std::vector<char> compiledShader(0); // shaders if (!loadEffect(filename, compiledShader)) MessageBoxA(0, "Error Loading Effect", 0, 0); HRESULT hr = D3DX11CreateEffectFromMemory(&compiledShader[0], compiledShader.size(), 0, _dxDev, fx); if (FAILED(hr)) MessageBoxA(0, "Error creating FX", 0, 0); }
JF::Effect::Effect(ID3D11Device* device, const std::wstring& filename) : mFX(0) { auto fin = JFAPI::FILE::Open(filename.c_str(), eFILE_OPERATION::READ); size_t size = JFAPI::FILE::GetLength(fin); std::vector<char> compiledShader(size); JFAPI::FILE::Read(fin, size, &compiledShader[0]); JFAPI::FILE::Close(fin); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, &mFX)); }
CComPtr<ID3DBlob> SpriteGame::compileShader(const char* filename, const char* entryPoint, const char* shaderVersion) { CComPtr<ID3DBlob> compiledShader(nullptr); CComPtr<ID3DBlob> compileErrors(nullptr); HRESULT result = D3DX11CompileFromFile(filename, nullptr, nullptr, entryPoint, shaderVersion, 0, 0, nullptr, &compiledShader, &compileErrors, nullptr); if(compileErrors != nullptr) MessageBox(nullptr, static_cast<char*>(compileErrors->GetBufferPointer()), "Shader Compile Error", MB_OK); if(FAILED(result)) return nullptr; return compiledShader; }
Effect::Effect(ID3D11Device* device, const std::wstring& filename) : mFX(0) { std::ifstream fin(filename, std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, &mFX)); }
bool MyApp::createEffect(const char* filename, ID3DX11Effect** fx) { // Load and create the pre-compiled shader code std::vector<char> compiledShader(0); if (!loadEffects(filename, compiledShader)) { OutputDebugStringA("Error: effect file not loaded"); return false; } HRESULT hr = D3DX11CreateEffectFromMemory(&compiledShader[0], compiledShader.size(), 0, _dxDev, fx); if (FAILED(hr)) return false; return true; }
Effect::Effect(ID3D11Device* device, const std::string& filename) : m_fx(nullptr) { std::ifstream fin(filename, std::ios::binary); OC_ASSERT(fin.good()); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, m_fx.GetAddressOf())); }
void Shape::LoadFX() { std::ifstream fin("fx/color.fxo", std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, mD3DDevice, &mFX)); mTech = mFX->GetTechniqueByName("ColorTech"); mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix(); }
const CompiledShaderPtr ShaderCompiler::Compile(const char*const pszShader, const GLint shaderType) { const char* shaderCode[1] = { pszShader }; CompiledShaderPtr compiledShader(new CompiledShader(shaderType)); GL_CHECKERR(glShaderSource(compiledShader->GetShaderHandle(), 1, shaderCode, nullptr)); shaderCode[0] = nullptr; // Lets compile the shader GL_CHECKERR(glCompileShader(compiledShader->GetShaderHandle())); GLint status; GL_CHECKERR(glGetShaderiv(compiledShader->GetShaderHandle(), GL_COMPILE_STATUS, &status)); // Dump debug info (source and log) if compilation failed. if (status != GL_TRUE && mbVerbose) DumpDebugInfo(compiledShader->GetShaderHandle()); return compiledShader; }
BillboardEffect::BillboardEffect(ID3D11Device* device, const std::wstring& filename, ID3DX11Effect* effect = 0) { if(effect == 0) { std::ifstream fin(filename, std::ios::binary); fin.seekg(0, std::ios_base::end); int size = (int)fin.tellg(); fin.seekg(0, std::ios_base::beg); std::vector<char> compiledShader(size); fin.read(&compiledShader[0], size); fin.close(); HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, device, &mFX)); } else mFX = effect; Light1Tech = mFX->GetTechniqueByName("Light1"); Light2Tech = mFX->GetTechniqueByName("Light2"); Light3Tech = mFX->GetTechniqueByName("Light3"); Light0TexTech = mFX->GetTechniqueByName("Light0Tex"); Light1TexTech = mFX->GetTechniqueByName("Light1Tex"); Light2TexTech = mFX->GetTechniqueByName("Light2Tex"); Light3TexTech = mFX->GetTechniqueByName("Light3Tex"); Light0TexAlphaClipTech = mFX->GetTechniqueByName("Light0TexAlphaClip"); Light1TexAlphaClipTech = mFX->GetTechniqueByName("Light1TexAlphaClip"); Light2TexAlphaClipTech = mFX->GetTechniqueByName("Light2TexAlphaClip"); Light3TexAlphaClipTech = mFX->GetTechniqueByName("Light3TexAlphaClip"); Light1FogTech = mFX->GetTechniqueByName("Light1Fog"); Light2FogTech = mFX->GetTechniqueByName("Light2Fog"); Light3FogTech = mFX->GetTechniqueByName("Light3Fog"); Light0TexFogTech = mFX->GetTechniqueByName("Light0TexFog"); Light1TexFogTech = mFX->GetTechniqueByName("Light1TexFog"); Light2TexFogTech = mFX->GetTechniqueByName("Light2TexFog"); Light3TexFogTech = mFX->GetTechniqueByName("Light3TexFog"); Light0TexAlphaClipFogTech = mFX->GetTechniqueByName("Light0TexAlphaClipFog"); Light1TexAlphaClipFogTech = mFX->GetTechniqueByName("Light1TexAlphaClipFog"); Light2TexAlphaClipFogTech = mFX->GetTechniqueByName("Light2TexAlphaClipFog"); Light3TexAlphaClipFogTech = mFX->GetTechniqueByName("Light3TexAlphaClipFog"); Light1ReflectTech = mFX->GetTechniqueByName("Light1Reflect"); Light2ReflectTech = mFX->GetTechniqueByName("Light2Reflect"); Light3ReflectTech = mFX->GetTechniqueByName("Light3Reflect"); Light0TexReflectTech = mFX->GetTechniqueByName("Light0TexReflect"); Light1TexReflectTech = mFX->GetTechniqueByName("Light1TexReflect"); Light2TexReflectTech = mFX->GetTechniqueByName("Light2TexReflect"); Light3TexReflectTech = mFX->GetTechniqueByName("Light3TexReflect"); Light0TexAlphaClipReflectTech = mFX->GetTechniqueByName("Light0TexAlphaClipReflect"); Light1TexAlphaClipReflectTech = mFX->GetTechniqueByName("Light1TexAlphaClipReflect"); Light2TexAlphaClipReflectTech = mFX->GetTechniqueByName("Light2TexAlphaClipReflect"); Light3TexAlphaClipReflectTech = mFX->GetTechniqueByName("Light3TexAlphaClipReflect"); Light1FogReflectTech = mFX->GetTechniqueByName("Light1FogReflect"); Light2FogReflectTech = mFX->GetTechniqueByName("Light2FogReflect"); Light3FogReflectTech = mFX->GetTechniqueByName("Light3FogReflect"); Light0TexFogReflectTech = mFX->GetTechniqueByName("Light0TexFogReflect"); Light1TexFogReflectTech = mFX->GetTechniqueByName("Light1TexFogReflect"); Light2TexFogReflectTech = mFX->GetTechniqueByName("Light2TexFogReflect"); Light3TexFogReflectTech = mFX->GetTechniqueByName("Light3TexFogReflect"); Light0TexAlphaClipFogReflectTech = mFX->GetTechniqueByName("Light0TexAlphaClipFogReflect"); Light1TexAlphaClipFogReflectTech = mFX->GetTechniqueByName("Light1TexAlphaClipFogReflect"); Light2TexAlphaClipFogReflectTech = mFX->GetTechniqueByName("Light2TexAlphaClipFogReflect"); Light3TexAlphaClipFogReflectTech = mFX->GetTechniqueByName("Light3TexAlphaClipFogReflect"); WorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix(); World = mFX->GetVariableByName("gWorld")->AsMatrix(); WorldInvTranspose = mFX->GetVariableByName("gWorldInvTranspose")->AsMatrix(); TexTransform = mFX->GetVariableByName("gTexTransform")->AsMatrix(); EyePosW = mFX->GetVariableByName("gEyePosW")->AsVector(); FogColor = mFX->GetVariableByName("gFogColor")->AsVector(); FogStart = mFX->GetVariableByName("gFogStart")->AsScalar(); FogRange = mFX->GetVariableByName("gFogRange")->AsScalar(); DirLights = mFX->GetVariableByName("gDirLights"); Mat = mFX->GetVariableByName("gMaterial"); DiffuseMap = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource(); CubeMap = mFX->GetVariableByName("gCubeMap")->AsShaderResource(); NormalMap = mFX->GetVariableByName("gNormalMap")->AsShaderResource(); ssflag = mFX->GetVariableByName("SSFlag")->AsScalar(); sobelflag = mFX->GetVariableByName("SobelFlag")->AsScalar(); toonflag = mFX->GetVariableByName("ToonFlag")->AsScalar(); fogflag = mFX->GetVariableByName("FogFlag")->AsScalar(); GlitterFlag = mFX->GetVariableByName("GlitterFlag")->AsScalar(); RandomNumber = mFX->GetVariableByName("RandomNumber")->AsScalar(); GlassFlag = mFX->GetVariableByName("GlassFlag")->AsScalar(); }
bool SpriteGame::loadContent() { CComPtr<ID3DBlob> compiledShader(compileShader("SpriteShader.fx", "VS_Main", "vs_4_0")); if(compiledShader == nullptr) return false; HRESULT result = device->CreateVertexShader(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), nullptr, &vertexShader); if(FAILED(result)) return false; D3D11_INPUT_ELEMENT_DESC vertexProperties[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXEL", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; result = device->CreateInputLayout(vertexProperties, 2, compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), &inputLayout); if(FAILED(result)) return false; compiledShader = compileShader("SpriteShader.fx", "PS_Main", "ps_4_0"); if(compiledShader == nullptr) return false; result = device->CreatePixelShader(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), nullptr, &pixelShader); if(FAILED(result)) return false; result = D3DX11CreateShaderResourceViewFromFile(device, "Resources/Sprite.dds", nullptr, nullptr, &texture, nullptr); if(FAILED(result)) return false; D3D11_SAMPLER_DESC samplerDescriptor; ZeroMemory(&samplerDescriptor, sizeof(samplerDescriptor)); samplerDescriptor.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDescriptor.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDescriptor.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDescriptor.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDescriptor.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDescriptor.MaxLOD = D3D11_FLOAT32_MAX; result = device->CreateSamplerState(&samplerDescriptor, &textureSampler); if(FAILED(result)) return false; CComPtr<ID3D11Resource> textureResource; texture->GetResource(&textureResource); D3D11_TEXTURE2D_DESC textureDescriptor; ((ID3D11Texture2D*)(textureResource.p))->GetDesc(&textureDescriptor); float halfWidth = textureDescriptor.Width/2.0f; float halfHeight = textureDescriptor.Height/2.0f; Vertex vertices[] = { { XMFLOAT3(-halfWidth, halfHeight, 1.0f), XMFLOAT2(0.0f, 0.0f) }, { XMFLOAT3( halfWidth, halfHeight, 1.0f), XMFLOAT2(1.0f, 0.0f) }, { XMFLOAT3(-halfWidth, -halfHeight, 1.0f), XMFLOAT2(0.0f, 1.0f) }, { XMFLOAT3( halfWidth, -halfHeight, 1.0f), XMFLOAT2(1.0f, 1.0f) } }; D3D11_BUFFER_DESC bufferDescriptor = {0}; bufferDescriptor.Usage = D3D11_USAGE_DEFAULT; bufferDescriptor.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDescriptor.ByteWidth = sizeof(vertices); D3D11_SUBRESOURCE_DATA vertexData = {0}; vertexData.pSysMem = vertices; result = device->CreateBuffer(&bufferDescriptor, &vertexData, &vertexBuffer); if(FAILED(result)) return false; D3D11_BUFFER_DESC matrixBufferDescriptor = {0}; matrixBufferDescriptor.Usage = D3D11_USAGE_DEFAULT; matrixBufferDescriptor.BindFlags = D3D11_BIND_CONSTANT_BUFFER; matrixBufferDescriptor.ByteWidth = sizeof(XMMATRIX); result = device->CreateBuffer(&matrixBufferDescriptor, nullptr, &mvpMatrixBuffer); if(FAILED(result)) return false; sprites[0].setPosition(100, 300); sprites[1].setPosition(400, 100); vpMatrix = XMMatrixIdentity() * XMMatrixOrthographicOffCenterLH(0.0f, 600.0f, 0.0f, 600.0f, 0.1f, 100.0f); D3D11_BLEND_DESC blendDescriptor = {0}; blendDescriptor.RenderTarget[0].BlendEnable = true; blendDescriptor.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; blendDescriptor.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendDescriptor.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; blendDescriptor.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; blendDescriptor.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; blendDescriptor.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; blendDescriptor.RenderTarget[0].RenderTargetWriteMask = 0x0F; float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; device->CreateBlendState(&blendDescriptor, &blendState); deviceContext->OMSetBlendState(blendState, blendFactor, 0xFFFFFFFF); return true; }