static ID3D10ShaderResourceView *BrightnessImage(ID3D10ShaderResourceView *pTexture, sImageInfo *pInfo) { ID3D10Device *device = GutGetGraphicsDeviceDX10(); int w = pInfo->m_iWidth/4; int h = pInfo->m_iHeight/4; float fTexelW = 1.0f/(float)w; float fTexelH = 1.0f/(float)h; D3D10_VIEWPORT vp = {0, 0, w, h, 0.0f, 1.0f}; device->RSSetViewports(1, &vp); device->VSSetShader(g_pBlurVS); device->PSSetShader(g_pBrightnessPS); ID3D10Buffer *buffer_array[2] = {g_pVSConstantBuffer, g_pBrightnessConstantBuffer}; device->VSSetConstantBuffers(0, 2, buffer_array); device->PSSetConstantBuffers(0, 2, buffer_array); UINT stride = sizeof(Vertex_VT); UINT offset = 0; g_pDevice->IASetInputLayout(g_pVertexLayout); g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); device->OMSetRenderTargets(1, &g_pFrameRTView[1], NULL); Vector4 *pConstants; g_pBrightnessConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstants); pConstants[0] = g_vBrightnessOffset; pConstants[1] = g_vBrightnessScale; g_pBrightnessConstantBuffer->Unmap(); g_pDevice->PSSetShaderResources(0, 1, &pTexture); g_pDevice->Draw(4, 0); return g_pFrameSRView[1]; }
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; }
static ID3D10ShaderResourceView *BlurImage(ID3D10ShaderResourceView *pTexture, sImageInfo *pInfo) { int w = pInfo->m_iWidth/4; int h = pInfo->m_iHeight/4; float fTexelW = 1.0f/(float)w; float fTexelH = 1.0f/(float)h; D3D10_VIEWPORT vp = {0, 0, w, h, 0.0f, 1.0f}; g_pDevice->RSSetViewports(1, &vp); const int num_samples = KERNELSIZE; Vector4 vTexOffsetX[num_samples]; Vector4 vTexOffsetY[num_samples]; for ( int i=0; i<num_samples; i++ ) { vTexOffsetX[i].Set(g_uv_offset_table[i] * fTexelW, 0.0f, 0.0f, g_weight_table[i]); vTexOffsetY[i].Set(0.0f, g_uv_offset_table[i] * fTexelH, 0.0f, g_weight_table[i]); } Vector4 *pConstants; ID3D10Device *device = GutGetGraphicsDeviceDX10(); device->VSSetShader(g_pBlurVS); device->PSSetShader(g_pBlurPS); ID3D10Buffer *buffer_array[2] = {g_pVSConstantBuffer, g_pBlurConstantBuffer}; device->VSSetConstantBuffers(0, 2, buffer_array); device->PSSetConstantBuffers(0, 2, buffer_array); UINT stride = sizeof(Vertex_VT); UINT offset = 0; g_pDevice->IASetInputLayout(g_pVertexLayout); g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); g_pDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // 水平模糊 { device->OMSetRenderTargets(1, &g_pFrameRTView[0], NULL); g_pBlurConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstants); memcpy(pConstants, vTexOffsetX, sizeof(Vector4)*num_samples); g_pBlurConstantBuffer->Unmap(); g_pDevice->PSSetShaderResources(0, 1, &pTexture); g_pDevice->Draw(4, 0); } // 垂直模糊 { device->OMSetRenderTargets(1, &g_pFrameRTView[1], NULL); g_pBlurConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, (void **) &pConstants); memcpy(pConstants, vTexOffsetY, sizeof(Vector4)*num_samples); g_pBlurConstantBuffer->Unmap(); g_pDevice->PSSetShaderResources(0, 1, &g_pFrameSRView[0]); g_pDevice->Draw(4, 0); } return g_pFrameSRView[1]; }
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; }
bool CDX10Shader::Execute(std::vector<ID3D10RenderTargetView*> *vecRT, unsigned int vertexIndexStep) { ID3D10Device* pDevice = m_rendererSystem->GetDevice(); ID3D10RenderTargetView* oldRTV = 0; ID3D10DepthStencilView* oldDSV = 0; if (vecRT!=NULL && !vecRT->empty()) pDevice->OMGetRenderTargets(1, &oldRTV, &oldDSV); D3D10_TECHNIQUE_DESC techDesc; m_effect.GetTechnique()->GetDesc(&techDesc); unsigned int cPasses = techDesc.Passes; unsigned int stride = m_vertsize; unsigned int offset = 0; ID3D10Buffer* buffer = m_vb.GetVertexBuffer(); pDevice->IASetVertexBuffers(0, 1, &buffer, &stride, &offset); if (m_vb.m_type == D3DVertexBuffer::INDEXED_BUFFER) { pDevice->IASetIndexBuffer(m_vb.GetIndexBuffer(), DXGI_FORMAT_R32_UINT, 0); pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); } else { pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); } pDevice->IASetInputLayout(m_inputLayout); for (unsigned int iPass = 0; iPass < cPasses; iPass++) { if (!m_effect.BeginPass(iPass)) { LOGERR("Failed to apply and begin pass %u in technique %s", iPass, techDesc.Name); break; } if (vecRT != NULL && vecRT->size() > iPass) pDevice->OMSetRenderTargets(1, &(*vecRT)[iPass], NULL); if (m_vb.m_type == D3DVertexBuffer::INDEXED_BUFFER && m_primitivesCount > 0) for (int i = 0; i < m_primitivesCount; i++) pDevice->DrawIndexed(m_indexCount, 0, iPass*vertexIndexStep+i*((m_vbsize/m_vertsize)/m_primitivesCount)); else pDevice->Draw(m_primitivesCount, iPass*vertexIndexStep); } if (oldRTV != 0) { if (oldDSV != 0) { pDevice->OMSetRenderTargets(1, &oldRTV, oldDSV); oldRTV->Release(); oldRTV->Release(); } else { pDevice->OMSetRenderTargets(1, &oldRTV, NULL); oldRTV->Release(); } } pDevice->RSSetViewports(1, &m_rendererSystem->GetViewPort()); return true; }