bool D10State::init() { static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateEffectFromMemory); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateStateBlock); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10StateBlockMaskEnableAll); if (pD3D10CreateEffectFromMemory == NULL || pD3D10CreateStateBlock == NULL || pD3D10StateBlockMaskEnableAll == NULL) { ods("D3D10: Could not get handles for all required D3D10 state initialization functions"); return false; } HRESULT hr; D3D10_STATE_BLOCK_MASK StateBlockMask; ZeroMemory(&StateBlockMask, sizeof(StateBlockMask)); hr = pD3D10StateBlockMaskEnableAll(&StateBlockMask); if (FAILED(hr)) { ods("D3D10: D3D10StateBlockMaskEnableAll failed"); return false; } hr = pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pOrigStateBlock); if (FAILED(hr)) { ods("D3D10: D3D10CreateStateBlock for pOrigStateBlock failed"); return false; } hr = pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pMyStateBlock); if (FAILED(hr)) { ods("D3D10: D3D10CreateStateBlock for pMyStateBlock failed"); return false; } hr = pOrigStateBlock->Capture(); if (FAILED(hr)) { ods("D3D10: Failed to store original state block during init"); return false; } ID3D10Texture2D *pBackBuffer = NULL; hr = pSwapChain->GetBuffer(0, __uuidof(*pBackBuffer), (LPVOID*)&pBackBuffer); if (FAILED(hr)) { ods("D3D10: pSwapChain->GetBuffer failure!"); return false; } pDevice->ClearState(); D3D10_TEXTURE2D_DESC backBufferSurfaceDesc; pBackBuffer->GetDesc(&backBufferSurfaceDesc); ZeroMemory(&vp, sizeof(vp)); vp.Width = backBufferSurfaceDesc.Width; vp.Height = backBufferSurfaceDesc.Height; vp.MinDepth = 0; vp.MaxDepth = 1; vp.TopLeftX = 0; vp.TopLeftY = 0; pDevice->RSSetViewports(1, &vp); hr = pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV); if (FAILED(hr)) { ods("D3D10: pDevice->CreateRenderTargetView failed!"); return false; } pDevice->OMSetRenderTargets(1, &pRTV, NULL); // Settings for an "over" operation. // https://en.wikipedia.org/w/index.php?title=Alpha_compositing&oldid=580659153#Description D3D10_BLEND_DESC blend; ZeroMemory(&blend, sizeof(blend)); blend.BlendEnable[0] = TRUE; blend.SrcBlend = D3D10_BLEND_ONE; blend.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOp = D3D10_BLEND_OP_ADD; blend.SrcBlendAlpha = D3D10_BLEND_ONE; blend.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOpAlpha = D3D10_BLEND_OP_ADD; blend.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; hr = pDevice->CreateBlendState(&blend, &pBlendState); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBlendState failed!"); return false; } pDevice->OMSetBlendState(pBlendState, NULL, 0xffffffff); hr = pD3D10CreateEffectFromMemory((void *) g_main, sizeof(g_main), 0, pDevice, NULL, &pEffect); if (FAILED(hr)) { ods("D3D10: D3D10CreateEffectFromMemory failed!"); return false; } pTechnique = pEffect->GetTechniqueByName("Render"); if (pTechnique == NULL) { ods("D3D10: Could not get technique for name 'Render'"); return false; } pDiffuseTexture = pEffect->GetVariableByName("txDiffuse")->AsShaderResource(); if (pDiffuseTexture == NULL) { ods("D3D10: Could not get variable by name 'txDiffuse'"); return false; } pTexture = NULL; pSRView = NULL; // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof(layout) / sizeof(layout[0]); // Create the input layout D3D10_PASS_DESC PassDesc; hr = pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); if (FAILED(hr)) { ods("D3D10: Couldn't get pass description for technique"); return false; } hr = pDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &pVertexLayout); if (FAILED(hr)) { ods("D3D10: pDevice->CreateInputLayout failure!"); return false; } pDevice->IASetInputLayout(pVertexLayout); D3D10_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(SimpleVertex) * 4; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; hr = pDevice->CreateBuffer(&bd, NULL, &pVertexBuffer); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBuffer failure!"); return false; } DWORD indices[] = { 0,1,3, 1,2,3, }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(DWORD) * 6; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; ZeroMemory(&InitData, sizeof(InitData)); InitData.pSysMem = indices; hr = pDevice->CreateBuffer(&bd, &InitData, &pIndexBuffer); if (FAILED(hr)) { ods("D3D10: pDevice->CreateBuffer failure!"); return false; } // Set index buffer pDevice->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); // Set primitive topology pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); hr = pMyStateBlock->Capture(); if (FAILED(hr)) { ods("D3D10: Failed to capture newly created state block"); return false; } hr = pOrigStateBlock->Apply(); if (FAILED(hr)) { ods("D3D10: Failed to restore original state block during init"); return false; } pBackBuffer->Release(); return true; }
bool CGutFontDX10::LoadShader(const char *filename) { ID3D10Device *pDevice = GutGetGraphicsDeviceDX10(); ID3D10Blob *pVSCode = NULL; // 載入Vertex Shader m_pVertexShader = GutLoadVertexShaderDX10_HLSL(filename, "VS", "vs_4_0", &pVSCode); if ( NULL==m_pVertexShader ) return false; // 載入Pixel Shader m_pPixelShader = GutLoadPixelShaderDX10_HLSL(filename, "PS", "ps_4_0"); if ( NULL==m_pPixelShader ) return false; // Shader常數的記憶體空間 { D3D10_BUFFER_DESC desc; // desc.ByteWidth = sizeof(Matrix4x4); desc.Usage = D3D10_USAGE_DYNAMIC; desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER; desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; desc.MiscFlags = 0; if ( D3D_OK != pDevice->CreateBuffer( &desc, NULL, &m_pShaderConstant ) ) return false; } int max_characters = m_iNumRows * m_iNumColumns; // vertex buffer { D3D10_BUFFER_DESC desc; desc.ByteWidth = max_characters * sizeof(_FontVertex) * 4; desc.Usage = D3D10_USAGE_DYNAMIC ; desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; desc.MiscFlags = 0; if ( D3D_OK != pDevice->CreateBuffer( &desc, NULL, &m_pVertexBuffer ) ) return false; } // index buffer { D3D10_BUFFER_DESC desc; desc.ByteWidth = max_characters * 6 * sizeof(short); desc.Usage = D3D10_USAGE_DYNAMIC; desc.BindFlags = D3D10_BIND_INDEX_BUFFER; desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; desc.MiscFlags = 0; D3D10_SUBRESOURCE_DATA subDesc; ZeroMemory(&subDesc, sizeof(subDesc)); subDesc.pSysMem = m_pIndexArray; if ( D3D_OK != pDevice->CreateBuffer( &desc, &subDesc, &m_pIndexBuffer ) ) return false; } // rasterizer state物件 { D3D10_RASTERIZER_DESC desc; GutSetDX10DefaultRasterizerDesc(desc); desc.CullMode = D3D10_CULL_NONE; if ( D3D_OK != pDevice->CreateRasterizerState(&desc, &m_pRasterizerState) ) return false; } // 設定Vertex資料格式 { D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0,12, D3D10_INPUT_PER_VERTEX_DATA, 0 } }; // if ( D3D_OK != pDevice->CreateInputLayout(layout, sizeof(layout)/sizeof(D3D10_INPUT_ELEMENT_DESC), pVSCode->GetBufferPointer(), pVSCode->GetBufferSize(), &m_pVertexLayout ) ) return false; } return true; }
ID3D10InputLayout *GutCreateInputLayoutDX10(const sVertexDecl *pVertexDecl, void *pCodeSignature, int CodeSize) { ID3D10Device *pDevice = g_pd3dDevice; D3D10_INPUT_ELEMENT_DESC layout[20]; ZeroMemory(layout, sizeof(layout)); int num_items = 0; int offset = 0; if ( pVertexDecl->m_iPositionOffset >= 0 ) { layout[num_items].SemanticName = "POSITION"; layout[num_items].SemanticIndex = 0; layout[num_items].Format = DXGI_FORMAT_R32G32B32_FLOAT; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_items++; offset += 12; } if ( pVertexDecl->m_iNormalOffset >= 0 ) { layout[num_items].SemanticName = "NORMAL"; layout[num_items].SemanticIndex = 0; layout[num_items].Format = DXGI_FORMAT_R32G32B32_FLOAT; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_items++; offset += 12; } if ( pVertexDecl->m_iColorOffset >= 0 ) { layout[num_items].SemanticName = "COLOR"; layout[num_items].SemanticIndex = 0; layout[num_items].Format = DXGI_FORMAT_R8G8B8A8_UNORM; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_items++; offset += 4; } int num_texcoords = 0; //char buffer[MAX_TEXCOORDS][16]; for ( int t=0; t<MAX_TEXCOORDS; t++ ) { if ( pVertexDecl->m_iTexcoordOffset[t] >=0 ) { layout[num_items].SemanticName = "TEXCOORD"; layout[num_items].SemanticIndex = t; layout[num_items].Format = DXGI_FORMAT_R32G32_FLOAT; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_texcoords++; num_items++; offset += 8; } } if ( pVertexDecl->m_iTangentOffset>0 ) { layout[num_items].SemanticName = "TANGENT"; layout[num_items].SemanticIndex = 0; layout[num_items].Format = DXGI_FORMAT_R32G32B32_FLOAT; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_items++; offset += 12; layout[num_items].SemanticName = "BINORMAL"; layout[num_items].SemanticIndex = 0; layout[num_items].Format = DXGI_FORMAT_R32G32B32_FLOAT; layout[num_items].InputSlot = 0; layout[num_items].AlignedByteOffset = offset; layout[num_items].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; layout[num_items].InstanceDataStepRate = 0; num_items++; offset += 12; } ID3D10InputLayout *pInputLayout = NULL; pDevice->CreateInputLayout( layout, num_items, pCodeSignature, CodeSize, &pInputLayout); return pInputLayout; }
void D10State::init() { static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateEffectFromMemory); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10CreateStateBlock); static HMODREF(GetModuleHandleW(L"D3D10.DLL"), D3D10StateBlockMaskEnableAll); HRESULT hr; dwMyThread = GetCurrentThreadId(); D3D10_STATE_BLOCK_MASK StateBlockMask; ZeroMemory(&StateBlockMask, sizeof(StateBlockMask)); pD3D10StateBlockMaskEnableAll(&StateBlockMask); pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pOrigStateBlock); pD3D10CreateStateBlock(pDevice, &StateBlockMask, &pMyStateBlock); pOrigStateBlock->Capture(); ID3D10Texture2D* pBackBuffer = NULL; hr = pSwapChain->GetBuffer(0, __uuidof(*pBackBuffer), (LPVOID*)&pBackBuffer); pDevice->ClearState(); D3D10_TEXTURE2D_DESC backBufferSurfaceDesc; pBackBuffer->GetDesc(&backBufferSurfaceDesc); ZeroMemory(&vp, sizeof(vp)); vp.Width = backBufferSurfaceDesc.Width; vp.Height = backBufferSurfaceDesc.Height; vp.MinDepth = 0; vp.MaxDepth = 1; vp.TopLeftX = 0; vp.TopLeftY = 0; pDevice->RSSetViewports(1, &vp); hr = pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRTV); pDevice->OMSetRenderTargets(1, &pRTV, NULL); D3D10_BLEND_DESC blend; ZeroMemory(&blend, sizeof(blend)); blend.BlendEnable[0] = TRUE; blend.SrcBlend = D3D10_BLEND_ONE; blend.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOp = D3D10_BLEND_OP_ADD; blend.SrcBlendAlpha = D3D10_BLEND_ONE; blend.DestBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; blend.BlendOpAlpha = D3D10_BLEND_OP_ADD; blend.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; pDevice->CreateBlendState(&blend, &pBlendState); float bf[4]; pDevice->OMSetBlendState(pBlendState, bf, 0xffffffff); pD3D10CreateEffectFromMemory((void *) g_main, sizeof(g_main), 0, pDevice, NULL, &pEffect); pTechnique = pEffect->GetTechniqueByName("Render"); pDiffuseTexture = pEffect->GetVariableByName("txDiffuse")->AsShaderResource(); pTexture = NULL; pSRView = NULL; // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof(layout) / sizeof(layout[0]); // Create the input layout D3D10_PASS_DESC PassDesc; pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc); hr = pDevice->CreateInputLayout(layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &pVertexLayout); pDevice->IASetInputLayout(pVertexLayout); D3D10_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(SimpleVertex) * 4; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; hr = pDevice->CreateBuffer(&bd, NULL, &pVertexBuffer); DWORD indices[] = { 0,1,3, 1,2,3, }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(DWORD) * 6; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; ZeroMemory(&InitData, sizeof(InitData)); InitData.pSysMem = indices; hr = pDevice->CreateBuffer(&bd, &InitData, &pIndexBuffer); // Set index buffer pDevice->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); // Set primitive topology pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pMyStateBlock->Capture(); pOrigStateBlock->Apply(); pBackBuffer->Release(); dwMyThread = 0; }
void PeaksAndValleys::init(int row, int column) { initMesh(row, column); //Create Effect Technique int shaderFlag = D3D10_SHADER_ENABLE_STRICTNESS; #if defined(DEBUG) || defined(_DEBUG ) shaderFlag |= D3D10_SHADER_DEBUG |D3D10_SHADER_SKIP_OPTIMIZATION; #endif ID3D10Device *device = DirectEngine::getInstance()->getDevice(); ID3D10Blob *errorBlob = nullptr; int result = D3DX10CreateEffectFromFile(L"color.fx", nullptr,nullptr, "fx_4_0", shaderFlag, 0, device, nullptr, nullptr, &_effect,&errorBlob,nullptr ); if (result < 0) { if (errorBlob) { MessageBoxA(nullptr, (char*)errorBlob->GetBufferPointer(), nullptr, 0); errorBlob->Release(); } DXTrace(__FILE__, __LINE__, result, L"D3DX10CreateEffectFromFile",true); } _effectTech = _effect->GetTechniqueByName("ColorTech"); _mvpMatrixV = _effect->GetVariableByName("g_MVPMatrix")->AsMatrix(); //Create Layout D3D10_INPUT_ELEMENT_DESC inputDesc[2]; inputDesc[0].SemanticName = "POSITION"; inputDesc[0].SemanticIndex = 0; inputDesc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; inputDesc[0].InputSlot = 0; inputDesc[0].AlignedByteOffset = 0; inputDesc[0].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; inputDesc[0].InstanceDataStepRate = 0; inputDesc[1].SemanticName = "COLOR"; inputDesc[1].SemanticIndex = 0; inputDesc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; inputDesc[1].InputSlot = 0; inputDesc[1].AlignedByteOffset = sizeof(float) * 3; inputDesc[1].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA; inputDesc[1].InstanceDataStepRate = 0; // D3D10_PASS_DESC passDesc; _effectTech->GetPassByName("P0")->GetDesc(&passDesc); result = device->CreateInputLayout(inputDesc, 2, passDesc.pIAInputSignature,passDesc.IAInputSignatureSize,&_inputLayout); assert(result>=0); //Matrix D3DXMatrixIdentity(&_modelMatrix); D3DXMatrixIdentity(&_projMatrix); D3DXMatrixIdentity(&_viewMatrix); //Create Project Matrix auto &winSize = DirectEngine::getInstance()->getWinSize(); D3DXMatrixPerspectiveFovLH(&_projMatrix,M_PI/4,winSize.width/winSize.height,1.0f,400.0f); D3DXVECTOR3 eyePosition(0,80,-120); D3DXVECTOR3 targetPosition(0,0,0); D3DXVECTOR3 upperVec(0,1,0); D3DXMatrixLookAtLH(&_viewMatrix, &eyePosition, &targetPosition, &upperVec); }