void RestartMenu::Render(ID3D11DeviceContext* deviceContext) { ID3D11Buffer* vertexBuffer = buttonGeometry->GetVertexBuffer(); for (unsigned int i = 0; i < buttons.size(); i++) { unsigned int vertexSize = sizeof(float) * 4; unsigned int offset = 0; unsigned int vertexCount = 4; if (currentSelect == i) scaling = DirectX::XMVectorSet(buttons[i]->scale.x + 0.1, buttons[i]->scale.y + 0.1, 1.0f, 1.0f); else scaling = DirectX::XMVectorSet(buttons[i]->scale.x, buttons[i]->scale.y, 1.0f, 1.0f); translation = DirectX::XMVectorSet(buttons[i]->position.x, buttons[i]->position.y, 1.0f, 1.0f); UpdateConstantBuffer(deviceContext, &buttonScale); deviceContext->VSSetConstantBuffers(0, 1, &constantBuffer); deviceContext->PSSetConstantBuffers(0, 1, &constantBuffer); deviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &vertexSize, &offset); deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); SRV = buttons[i]->material->GetTexture(); deviceContext->PSSetShaderResources(0, 1, &SRV); deviceContext->Draw(vertexCount, 0); } }
void LineRenderer::RenderAll(RenderContext* rc) { ID3D11DeviceContext* d3dContext = rc->Context(); // update constant buffer Matrix viewProj = rc->Cam().View() * rc->Cam().Proj(); viewProj.Transpose(); UpdateConstantBuffer(d3dContext,m_perframeCB,&viewProj,sizeof(viewProj)); // set constant buffer to vertex shader ID3D11Buffer* cbuffers[1] = {m_perframeCB}; d3dContext->VSSetConstantBuffers(0,1,cbuffers); // set shaders d3dContext->VSSetShader(m_vsShader,NULL,0); d3dContext->GSSetShader( NULL, NULL, 0 ); d3dContext->PSSetShader(m_psShader,NULL,0); // set vertex layout and primitive topology d3dContext->IASetInputLayout(m_vertexLayoutPC); d3dContext->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_LINELIST ); RenderStateCache* rscache = rc->GetRenderStateCache(); // set state-blocks ( raster, depth, and blend states) d3dContext->RSSetState(rscache->GetRasterState(FillMode::Wireframe,CullMode::BACK)); d3dContext->OMSetDepthStencilState(NULL,0); d3dContext->OMSetBlendState( NULL, NULL, 0xFFFFFFFF ); ID3D11Buffer* vbuffers[1] = {m_vbPC->GetBuffer()}; uint32_t strides[1] = {m_vbPC->GetStride()}; uint32_t offsets[1] = {0}; d3dContext->IASetVertexBuffers( 0, 1, vbuffers, strides, offsets ); uint32_t bufSize = m_vbPC->GetCount(); uint32_t totalVertexCount = (uint32_t) m_vertsPC.size(); uint32_t start = 0; uint32_t count = (totalVertexCount < bufSize) ? totalVertexCount : bufSize; HRESULT hr; D3D11_MAPPED_SUBRESOURCE mappedResource; while(start < totalVertexCount) { hr = d3dContext->Map(m_vbPC->GetBuffer(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if(FAILED(hr)) break; CopyMemory(mappedResource.pData, &m_vertsPC[start], m_vbPC->GetStride() * count); d3dContext->Unmap(m_vbPC->GetBuffer(), 0); d3dContext->Draw(count,0); start += count; if( (start + count) > totalVertexCount) count = totalVertexCount - start; } m_vertsPC.clear(); }
void CPUTGuiController::Resize(int width, int height) { mWidth = width; mHeight = height; mRecalculate = true; mUpdateBuffers = true; UpdateConstantBuffer(); }
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ){ switch( message ){ case WM_DESTROY: PostQuitMessage( 0 ); return 0; break; case WM_KEYDOWN: { switch( wParam ){ case( VK_DOWN ): gAngle -= 0.20f; UpdateConstantBuffer(); break; case( VK_UP ): gAngle += 0.20f; UpdateConstantBuffer(); break; case( VK_RIGHT ): gYAngle -= 0.20f; UpdateConstantBuffer(); break; case( VK_LEFT ): gYAngle += 0.20f; UpdateConstantBuffer(); break; } gRotationMatrix = XMMatrixMultiply( XMMatrixRotationX( gAngle ), XMMatrixRotationY( gYAngle ) ); } } return DefWindowProc( hWnd, message, wParam, lParam ); }
void RenderingContext::Prepare(_In_ const Camera & Camera) { CameraConstantBufferType CameraData = { Camera.GetViewMatrix(), Camera.GetProjectionMatrix() }; UpdateConstantBuffer(CameraConstantBuffer, &CameraData, sizeof(CameraData)); DeviceContext.GetDeviceContext()->VSSetShader(VertexShader.Get(), nullptr, 0); DeviceContext.GetDeviceContext()->PSSetShader(PixelShader.Get(), nullptr, 0); std::array<ID3D11Buffer*, 2> ConstantBuffers = { CameraConstantBuffer.Get(), ObjectConstantBuffer.Get() }; DeviceContext.GetDeviceContext()->VSSetConstantBuffers(0, 2, ConstantBuffers.data()); DeviceContext.GetDeviceContext()->IASetInputLayout(InputLayout.Get()); }
void CBufferDraw::Init() { //ルートシグネチャ作成 pImpl->CreateRootSignature(); //デスクプリタヒープ作成 pImpl->CreateDescriptorHeap(); //コンスタントバッファ作成 pImpl->CreateConstantBuffer(); //パイプラインステート作成 pImpl->CreatePipelineState(); //コマンドリスト作成 pImpl->CreateCommandList(); //コンスタントバッファの更新 UpdateConstantBuffer(); }
void IntroLoop(long sample) { #ifdef _DEBUG static long lastCheck = 0; if (sample - lastCheck > 22100) { lastCheck = sample; if (IsShaderModified(SHADER_HLSL)) { ID3DBlob* pBlob = NULL; RELOAD_CS(SHADER_HLSL, "CS", pBlob, g_pComputeShader); RELOAD_VS(SHADER_HLSL, "VS", pBlob, g_pVertexShader); RELOAD_PS(SHADER_HLSL, "PS", pBlob, g_pPixelShader); } } #endif UINT initCounts = 0; ID3D11ShaderResourceView* srvNullView[2] = { NULL, NULL }; ID3D11UnorderedAccessView* uavNullView[2] = { NULL, NULL }; ID3D11RenderTargetView* rtvNullView[2] = { NULL, NULL }; UpdateConstantBuffer(sample); // Compute g_pImmediateContext->CSSetShader(g_pComputeShader, NULL, 0); g_pImmediateContext->CSSetConstantBuffers(0, 1, &g_pStaticConstantBuffer); g_pImmediateContext->CSSetConstantBuffers(1, 1, &g_pFrameConstantBuffer); g_pImmediateContext->CSSetUnorderedAccessViews(0, 1, &g_pDataUAV, &initCounts); g_pImmediateContext->Dispatch(GROUPS_X, GROUPS_Y, GROUPS_Z); g_pImmediateContext->CSSetUnorderedAccessViews(0, 1, uavNullView, &initCounts); // Clear float clearColor[4] = { 0.1f, 0.2f, 0.3f, 1.0f }; g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, clearColor); // Draw g_pImmediateContext->VSSetShader(g_pVertexShader, NULL, 0); g_pImmediateContext->PSSetShader(g_pPixelShader, NULL, 0); g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pStaticConstantBuffer); g_pImmediateContext->VSSetConstantBuffers(1, 1, &g_pFrameConstantBuffer); g_pImmediateContext->VSSetShaderResources(0, 1, &g_pDataSRV); g_pImmediateContext->Draw(BUFFER_SIZE*6, 0); g_pImmediateContext->VSSetShaderResources(0, 1, srvNullView); CHK(g_pSwapChain->Present(0, 0)); }
void CubeObject::OnCreate() { CreateBuffers(); m_Scale = Vec3(1.0f, 1.0f, 1.0f); m_Qt.identity(); m_Pos = Vec3(0, 0, 0.0); ///ルートシグネチャ作成 CreateRootSignature(); ///デスクプリタヒープ作成 CreateDescriptorHeap(); ///コンスタントバッファ作成 CreateConstantBuffer(); ///パイプラインステート作成 CreatePipelineState(); ///コマンドリスト作成 CreateCommandList(); //コンスタントバッファの更新 UpdateConstantBuffer(); }
void GObject::SetConstantBuffers(ID3D11DeviceContext* pContext, TObjWM* pUint, int iMesh) { //D3DXMatrixMultiply(&m_pObjWM->m_matCalculation[iMesh], &m_pObjWM->m_matAnim[iMesh], &m_matWorld); if (m_bDefferedMultiThread) { //D3DXMatrixMultiply(&m_pObjWM->m_matCalculation[iMesh], &m_pObjWM->m_matAnim[iMesh], &m_matWorld); // m_pModel은 공용 메쉬의 상수버퍼이기 때문에 동일한 오브젝트를 사용할 경우 멀티쓰래드에서 문제가 생긴다. // 그렇기 때문에 오브젝트 단위의 상수버퍼를 사용한다. SetMatrix(&pUint->m_matCalculation[iMesh], &m_matView, &m_matProj); pContext->UpdateSubresource(m_dxobj.g_pConstantBuffer.Get(), 0, NULL, &m_cbData, 0, 0); pContext->VSSetConstantBuffers(0, 1, m_dxobj.g_pConstantBuffer.GetAddressOf()); } else { D3DXMatrixMultiply(&pUint->m_matCalculation[iMesh], &pUint->m_matAnim[iMesh], &m_matWorld); pUint->m_pModel->SetMatrix(&pUint->m_matCalculation[iMesh], &m_matView, &m_matProj); UpdateConstantBuffer(pContext, pUint->m_pModel); } }
void FRenderD3D11::RenderFrame() { camera.CommitListener(); // ProcessInCPU(); float colorRGBA[4] = { 0.0f, 0.2f, 0.4f, 1.0f }; // clear the back buffer to a deep blue m_pDeviceContext->ClearRenderTargetView(m_pBackbuffer, colorRGBA); UpdateVertexBuffer(); UpdateIndexBuffer(); UpdateConstantBuffer(); // select which vertex buffer to display UINT stride = sizeof(VERTEX); UINT offset = 0; m_pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset); m_pDeviceContext->IASetIndexBuffer(m_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); m_pDeviceContext->VSSetConstantBuffers(0, 1, &m_pCBuffer); // select which primtive type we are using m_pDeviceContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); D3D11_RASTERIZER_DESC desc; ZeroMemory(&desc, sizeof(D3D11_RASTERIZER_DESC)); desc.CullMode = D3D11_CULL_BACK; desc.FillMode = D3D11_FILL_WIREFRAME; desc.FrontCounterClockwise = false; desc.DepthClipEnable = true; ID3D11RasterizerState* pRasterizerState = NULL; m_pDevice->CreateRasterizerState(&desc, &pRasterizerState); m_pDeviceContext->RSSetState(pRasterizerState); // draw the vertex buffer to the back buffer m_pDeviceContext->DrawIndexed(triCorn.indices.size(), 0, 0); // switch the back buffer and the front buffer m_pSwapchain->Present(0, 0); }
void CBufferDraw::OnDraw() { UpdateConstantBuffer(); DrawObject(); }
void InitGraphics( SFLOAT width, SFLOAT height ){ //creating vertex buffer Vertex vertices[] = { Vertex( 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ), Vertex( 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ), Vertex( -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ), Vertex( -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f ), Vertex( 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f ), Vertex( 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f ), Vertex( -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f ), Vertex( -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 1.0f ) }; D3D11_BUFFER_DESC bufferDesc; ZeroMemory( &bufferDesc, sizeof( bufferDesc ) ); bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.ByteWidth = sizeof( vertices ); bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; gDevice->CreateBuffer( &bufferDesc, NULL, &gVBuffer ); D3D11_MAPPED_SUBRESOURCE mappedSR; gDeviceContext->Map( gVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mappedSR ); memcpy( mappedSR.pData, vertices, sizeof( vertices) ); gDeviceContext->Unmap( gVBuffer, NULL ); UINT stride = sizeof( Vertex ); UINT offset = 0; gDeviceContext->IASetVertexBuffers( 0, 1, &gVBuffer, &stride, &offset ); //indexBuffer SSHORT indices[] = { 7, 4, 5, 7, 5, 6, 0, 3, 2, 0, 2, 1, 4, 0, 1, 4, 1, 5, 3, 7, 6, 3, 6, 2, 3, 0, 4, 3, 4, 7, 6, 5, 1, 6, 1, 2 }; gNumTriangles = ( sizeof(indices)/sizeof(SSHORT) ) / 3; ZeroMemory( &bufferDesc, sizeof( bufferDesc ) ); bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.ByteWidth = sizeof( indices ); bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; gDevice->CreateBuffer( &bufferDesc, NULL, &gIBuffer ); gDeviceContext->Map( gIBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mappedSR ); memcpy( mappedSR.pData, indices, sizeof( indices ) ); gDeviceContext->Unmap( gIBuffer, NULL ); gDeviceContext->IASetIndexBuffer( gIBuffer , DXGI_FORMAT_R16_UINT, 0 ); //create the constant buffer bufferDesc.Usage = D3D11_USAGE_DEFAULT; bufferDesc.ByteWidth = sizeof( ConstantBuffer ); bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bufferDesc.CPUAccessFlags = 0; gDevice->CreateBuffer( &bufferDesc, NULL, &gConstantBuffer ); gDeviceContext->VSSetConstantBuffers( 0, 1, &gConstantBuffer ); // initialize the world matrix gRotationMatrix = XMMatrixIdentity(); gTranslationMatrix = XMMatrixIdentity(); //initializing the view matrix XMVECTOR eye = XMVectorSet( 0.0f, 0.0f, -5.0f, 0.0f ); XMVECTOR at = XMVectorSet( 0.0f, 0.0f, 1.0f, 0.0f ); XMVECTOR up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f ); gViewMatrix = XMMatrixLookAtLH( eye, at, up ); //initializing the projection matrixf gProjectionMatrix = XMMatrixPerspectiveFovLH( XM_PIDIV2, width / (FLOAT)height, 0.01f, 100.0f ); UpdateConstantBuffer(); }
void RenderingContext::SetObjectMatrix(_In_ const DirectX::XMFLOAT4X4 & ObjectMatrix) { UpdateConstantBuffer(ObjectConstantBuffer, &ObjectMatrix, sizeof(ObjectMatrix)); }
bool DX11::ComputeShader::SetExtraParameters(ID3D11DeviceContext * direct3Dcontext, Gpu::Effect * effect) { unsigned numParams = effect->shader->paramSpecs.size(); if(paramMappings.size() < numParams) { OutputDebugString(L"Not enough parameter mappings for the specified parameters!\n"); return false; } #if INTEL_HACK_COPYSTRUCTURECOUNT ZeroMemory(cpuImmutableConstBuffers, NUM_PARAM_BUFFERS * sizeof(bool)); #endif for(unsigned i = 0; i < paramMappings.size(); ++i) { ParamMapping & mapping = paramMappings[i]; Gpu::ShaderParamSpec & spec = effect->shader->paramSpecs[mapping.paramIndex]; Gpu::ShaderParam * param = effect->params[mapping.paramIndex]; if(!param) return false; if(param->type != spec.type) { OutputDebugString(L"Parameter type does not match specified parameter type!\n"); return false; } #if INTEL_HACK_COPYSTRUCTURECOUNT if(cpuImmutableConstBuffers[mapping.registerIndex] && (spec.type != Gpu::ShaderParam::TypeParamBuffer || mapping.attribute != BufferAttribute::Size)) { OutputDebugString(L"Can only upload ParamBuffer sizes to this buffer!\n"); return false; } #endif switch(spec.type) { case Gpu::ShaderParam::TypeFloat: { std::vector<float> & constants = paramConstData[mapping.registerIndex]; while(constants.size() <= mapping.bufferOffset) { constants.push_back(0.0f); } constants[mapping.bufferOffset] = param->fvalue; break; } case Gpu::ShaderParam::TypeFloatArray: { std::vector<float> & data = paramConstData[mapping.registerIndex]; const unsigned minBufferSize = mapping.bufferOffset + param->avalue->numFloats; if(data.size() < minBufferSize) data.resize(minBufferSize); memcpy(&(data[mapping.bufferOffset]), param->avalue->floats, param->avalue->numFloats * sizeof(float)); break; } case Gpu::ShaderParam::TypeParamBuffer: { DX11::ParamBuffer * paramBuffer = static_cast<DX11::ParamBuffer*>(param->bvalue); if(mapping.attribute == BufferAttribute::Size) { // Prepare the constant buffer, set nothing, wait until buffer has been created on GPU std::vector<float> & constants = paramConstData[mapping.registerIndex]; while(constants.size() <= mapping.bufferOffset) { constants.push_back(0.0f); } #if INTEL_HACK_COPYSTRUCTURECOUNT cpuImmutableConstBuffers[mapping.registerIndex] = true; #endif } else { if(mapping.writeable) { // Set the structure count of the UAV direct3Dcontext->CSSetUnorderedAccessViews(mapping.registerIndex, 1, ¶mBuffer->uav, ¶mBuffer->uavCountToUpload); paramBuffer->uavCountToUpload = -1; } else { direct3Dcontext->CSSetShaderResources(mapping.registerIndex, 1, ¶mBuffer->srv); } } break; } default: ApplyTextureParameter(direct3Dcontext, mapping.shader, mapping.registerIndex, param); break; } } if(numParams > 0) { // Upload CPU constants to GPU, creating buffers if necessary for(unsigned i = 0; i < NUM_PARAM_BUFFERS; ++i) { #if INTEL_HACK_COPYSTRUCTURECOUNT if(paramConstData[i].size() > 0 && (!cpuImmutableConstBuffers[i] || !paramConstBuffers[i])) #else if(paramConstData[i].size() > 0) #endif { UpdateConstantBuffer(direct3Dcontext, paramConstData[i], ¶mConstBuffers[i]); } } // Overwrite GPU constants with GPU ParamBuffer sizes for(unsigned i = 0; i < paramMappings.size(); ++i) { ParamMapping & mapping = paramMappings[i]; Gpu::ShaderParamSpec & spec = effect->shader->paramSpecs[mapping.paramIndex]; Gpu::ShaderParam * param = effect->params[mapping.paramIndex]; if(spec.type == Gpu::ShaderParam::TypeParamBuffer && mapping.attribute == BufferAttribute::Size) { DX11::ParamBuffer * paramBuffer = static_cast<DX11::ParamBuffer*>(param->bvalue); //{ // static ID3D11Buffer * debugBuf = 0; // static unsigned debugData[2] = { 0 }; // if(!debugBuf) // { // ID3D11Device * device = 0; // direct3Dcontext->GetDevice(&device); // D3D11_SUBRESOURCE_DATA vbInitData; // vbInitData.pSysMem = debugData; // device->CreateBuffer( // &CD3D11_BUFFER_DESC(2 * sizeof(unsigned), 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ), // &vbInitData, // &debugBuf); // } // direct3Dcontext->CopyStructureCount(debugBuf, 0 * sizeof(unsigned), paramBuffer->uav); // D3D11_MAPPED_SUBRESOURCE mappedData = { 0 }; // direct3Dcontext->Map(debugBuf, 0, D3D11_MAP_READ, 0, &mappedData); // memcpy(((char*)debugData), mappedData.pData, 2 * sizeof(unsigned)); // direct3Dcontext->Unmap(debugBuf, 0); // std::wstringstream debugOutput; // for(unsigned i = 0; i < 2; ++i) debugOutput << debugData[i] << ", "; // debugOutput << std::endl; // OutputDebugString(debugOutput.str().c_str()); //} direct3Dcontext->CopyStructureCount(paramConstBuffers[mapping.registerIndex], mapping.bufferOffset * sizeof(unsigned), paramBuffer->uav); //{ // static ID3D11Buffer * debugBuf = 0; // static unsigned debugData[4] = { 0 }; // if(!debugBuf) // { // ID3D11Device * device = 0; // direct3Dcontext->GetDevice(&device); // D3D11_SUBRESOURCE_DATA vbInitData; // vbInitData.pSysMem = debugData; // device->CreateBuffer( // &CD3D11_BUFFER_DESC(4 * sizeof(unsigned), 0, D3D11_USAGE_STAGING, D3D11_CPU_ACCESS_READ), // &vbInitData, // &debugBuf); // } // direct3Dcontext->CopyResource(debugBuf, paramConstBuffers[mapping.registerIndex]); // D3D11_MAPPED_SUBRESOURCE mappedData = { 0 }; // direct3Dcontext->Map(debugBuf, 0, D3D11_MAP_READ, 0, &mappedData); // memcpy(((char*)debugData), mappedData.pData, 4 * sizeof(unsigned)); // direct3Dcontext->Unmap(debugBuf, 0); // std::wstringstream debugOutput; // for(unsigned i = 0; i < 4; ++i) debugOutput << debugData[i] << ", "; // debugOutput << std::endl; // OutputDebugString(debugOutput.str().c_str()); //} } } // Set the buffers into the pipeline direct3Dcontext->CSSetConstantBuffers(0, NUM_PARAM_BUFFERS, paramConstBuffers); } return true; }
bool DX11::TextureShader::SetExtraParameters(ID3D11DeviceContext * direct3Dcontext, Gpu::Effect * effect) { unsigned numParams = effect->shader->paramSpecs.size(); if(paramMappings.size() > numParams) { OutputDebugString(L"Too many parameter mappings for the specified parameters!\n"); return false; } for(unsigned i = 0; i < paramMappings.size(); ++i) { ParamMapping & mapping = paramMappings[i]; Gpu::ShaderParamSpec & spec = effect->shader->paramSpecs[mapping.paramIndex]; Gpu::ShaderParam * param = effect->params[mapping.paramIndex]; if(!param) return false; if(param->type != spec.type) { OutputDebugString(L"Parameter type does not match specified parameter type!\n"); return false; } switch(spec.type) { case Gpu::ShaderParam::TypeFloat: { std::vector<float> & constants = paramConstData[mapping.registerIndex - 1]; while(constants.size() <= mapping.bufferOffset) { constants.push_back(0.0f); } constants[mapping.bufferOffset] = param->fvalue; break; } case Gpu::ShaderParam::TypeFloatArray: { if(param->avalue == 0) { OutputDebugString(L"Unset shader parameter!\n"); return false; } std::vector<float> & constants = paramConstData[mapping.registerIndex - 1]; const unsigned minBufferSize = mapping.bufferOffset + param->avalue->numFloats; if(constants.size() < minBufferSize) constants.resize(minBufferSize); memcpy(&(constants[mapping.bufferOffset]), param->avalue->floats, param->avalue->numFloats * sizeof(float)); break; } default: ApplyTextureParameter(direct3Dcontext, mapping.shader, mapping.registerIndex, param); break; } } if(numParams > 0) { for(unsigned i = 0; i < NUM_PARAM_BUFFERS; ++i) { if(paramConstData[i].size() > 0) { UpdateConstantBuffer(direct3Dcontext, paramConstData[i], ¶mConstBuffers[i]); } } direct3Dcontext->PSSetConstantBuffers(1, NUM_PARAM_BUFFERS, paramConstBuffers); } return true; }
bool DX11::ModelShader::Technique::SetExtraParameters(ID3D11DeviceContext * direct3Dcontext, Gpu::Effect * effect) { unsigned numParams = effect->shader->paramSpecs.size(); if(paramMappings.size() > numParams) { OutputDebugString(L"Too many parameter mappings for the specified parameters!\n"); return false; } for(unsigned i = 0; i < paramMappings.size(); ++i) { ParamMapping & mapping = paramMappings[i]; Gpu::ShaderParamSpec & spec = effect->shader->paramSpecs[mapping.paramIndex]; Gpu::ShaderParam * param = effect->params[mapping.paramIndex]; if(!param) return false; if(param->type != spec.type) return false; std::vector<float> * stageParamConstData = 0; switch(mapping.shader) { case ShaderStage::Vertex: stageParamConstData = vertexParamConstData; break; case ShaderStage::Geometry: stageParamConstData = geometryParamConstData; break; case ShaderStage::Pixel: stageParamConstData = pixelParamConstData; break; default: OutputDebugString(L"Non vertex/geometry/pixel shader states not yet implemented!\n"); return false; } switch(spec.type) { case Gpu::ShaderParam::TypeFloat: { if(mapping.registerIndex < NUM_STANDARD_BUFFERS || mapping.registerIndex >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT) { OutputDebugString(L"Invalid shader parameter mapping register index!\n"); return false; } std::vector<float> & constants = stageParamConstData[mapping.registerIndex - NUM_STANDARD_BUFFERS]; while(constants.size() <= mapping.bufferOffset) constants.push_back(0.0f); constants[mapping.bufferOffset] = param->fvalue; break; } case Gpu::ShaderParam::TypeFloatArray: { if(mapping.registerIndex < NUM_STANDARD_BUFFERS || mapping.registerIndex >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT) { OutputDebugString(L"Invalid shader parameter mapping register index!\n"); return false; } if(param->avalue == 0) { OutputDebugString(L"Unset shader parameter!\n"); return false; } std::vector<float> & constants = stageParamConstData[mapping.registerIndex - NUM_STANDARD_BUFFERS]; constants.resize(mapping.bufferOffset + param->avalue->numFloats + 1); memcpy(&(constants[mapping.bufferOffset]), param->avalue->floats, param->avalue->numFloats * sizeof(float)); break; } case Gpu::ShaderParam::TypeParamBuffer: { DX11::ParamBuffer * paramBuffer = static_cast<DX11::ParamBuffer*>(param->bvalue); if(mapping.writeable) { OutputDebugString(L"Cannot assign a writeable param buffer to a non-compute shader!\n"); return false; } if(param->bvalue == 0) { OutputDebugString(L"Unset shader parameter!\n"); return false; } switch(mapping.shader) { case ShaderStage::Vertex: direct3Dcontext->VSSetShaderResources(mapping.registerIndex, 1, ¶mBuffer->srv); break; case ShaderStage::Geometry: direct3Dcontext->GSSetShaderResources(mapping.registerIndex, 1, ¶mBuffer->srv); break; case ShaderStage::Pixel: direct3Dcontext->PSSetShaderResources(mapping.registerIndex, 1, ¶mBuffer->srv); break; } break; } default: ApplyTextureParameter(direct3Dcontext, mapping.shader, mapping.registerIndex, param); break; } } for(unsigned i = 0; i < NUM_PARAM_BUFFERS; ++i) { // Not sure if faster to SetConstantBuffers specifically for those that have data, or all in one go regardless of data if(vertexParamConstData[i].size() > 0) { UpdateConstantBuffer(direct3Dcontext, vertexParamConstData[i], &vertexParamConstBuffers[i]); direct3Dcontext->VSSetConstantBuffers(i + NUM_STANDARD_BUFFERS, 1, &vertexParamConstBuffers[i]); } if(geometryParamConstData[i].size() > 0) { UpdateConstantBuffer(direct3Dcontext, geometryParamConstData[i], &geometryParamConstBuffers[i]); direct3Dcontext->GSSetConstantBuffers(i + NUM_STANDARD_BUFFERS, 1, &geometryParamConstBuffers[i]); } if(pixelParamConstData[i].size() > 0) { UpdateConstantBuffer(direct3Dcontext, pixelParamConstData[i], &pixelParamConstBuffers[i]); direct3Dcontext->PSSetConstantBuffers(i + NUM_STANDARD_BUFFERS, 1, &pixelParamConstBuffers[i]); } } return true; }
void CubeObject::OnDraw() { //コンスタントバッファの更新 UpdateConstantBuffer(); //描画 DrawObject(); }