// 深度・ステンシルセット IZ_BOOL CGraphicsDevice::PushDepthStencil(CRenderTarget* rt) { IZ_BOOL ret = IZ_FALSE; CRenderTarget* curDepthRT = GetDepthSrencil(); // 現在の深度をプッシュ if ((rt != IZ_NULL) && (curDepthRT != rt)) { ret = m_DepthMgr.Push(curDepthRT); } if (ret) { SetDepthStencil(rt); } return ret; }
void TextureFilter( const Ptr<ShaderResourceView> & src, const Ptr<RenderTargetView> & dst, const std::vector<float2> & uvOffsets, const std::vector<float> & weights, const Ptr<class Sampler> & sampler) { ToyGE_ASSERT(uvOffsets.size() == weights.size()); ToyGE_ASSERT(src); ToyGE_ASSERT(dst); int32_t numSamples = (int32_t)uvOffsets.size(); if (numSamples <= 0) return; auto rc = Global::GetRenderEngine()->GetRenderContext(); rc->SetViewport(GetTextureQuadViewport(dst->GetResource()->Cast<Texture>())); auto filterVS = Shader::FindOrCreate<FilterVS>({ { "NUM_SAMPLES", std::to_string(numSamples) } }); auto filterPS = Shader::FindOrCreate<FilterPS>({ { "NUM_SAMPLES", std::to_string(numSamples) } }); filterVS->SetScalar("samplesOffsets", &uvOffsets[0], (int32_t)(sizeof(uvOffsets[0]) * uvOffsets.size())); filterVS->Flush(); filterPS->SetScalar("samplesWeights", &weights[0], (int32_t)(sizeof(weights[0]) * weights.size())); filterPS->SetSRV("filterTex", src); filterPS->SetSampler("filterSampler", sampler ? sampler : SamplerTemplate<>::Get()); filterPS->Flush(); rc->SetRenderTargets({ dst }); rc->SetDepthStencil(nullptr); rc->SetDepthStencilState(DepthStencilStateTemplate<false>::Get()); rc->SetVertexBuffer({ GetQuadVBs()[0] }); rc->SetIndexBuffer(GetQuadIB()); rc->SetPrimitiveTopology(PRIMITIVE_TOPOLOGY_TRIANGLELIST); rc->DrawIndexed(0, 0); }
void SkyBox::Render(const Ptr<RenderTargetView> & target, const Ptr<DepthStencilView> & dsv, const Ptr<RenderView> & view) { if (!_tex) return; auto rc = Global::GetRenderEngine()->GetRenderContext(); auto translationMat = translation(view->GetCamera()->GetPos()); auto transformMat = mul(translationMat, view->GetCamera()->GetViewProjMatrix()); auto vs = Shader::FindOrCreate<SkyBoxVS>(); auto ps = Shader::FindOrCreate<SkyBoxPS>(); vs->SetScalar("transform", transformMat); ps->SetSRV("skyBoxTex", _tex->GetShaderResourceView(0, 0, 0, 0, true)); ps->SetSampler("linearSampler", SamplerTemplate<>::Get()); vs->Flush(); ps->Flush(); rc->SetViewport(GetTextureQuadViewport(target->GetResource()->Cast<Texture>())); rc->SetRenderTargets({ target }); rc->SetDepthStencil(dsv); rc->SetRasterizerState(RasterizerStateTemplate<FILL_SOLID, CULL_NONE>::Get()); rc->SetDepthStencilState(DepthStencilStateTemplate<true, DEPTH_WRITE_ZERO, COMPARISON_LESS_EQUAL>::Get()); rc->SetVertexBuffer(_sphereMesh->GetRenderData()->GetMeshElements()[0]->GetVertexBuffer()); rc->SetIndexBuffer(_sphereMesh->GetRenderData()->GetMeshElements()[0]->GetIndexBuffer()); rc->SetPrimitiveTopology(PRIMITIVE_TOPOLOGY_TRIANGLELIST); rc->DrawIndexed(0, 0); rc->SetRasterizerState(nullptr); rc->SetDepthStencilState(nullptr); }
void DrawQuad( const std::vector< Ptr<class RenderTargetView> > & rtvs, float topLeftX, float topLeftY, float width, float height, float topLeftU, float topLeftV, float uvWidth, float uvHeight, const Ptr<class DepthStencilView> & dsv) { /*if (rtvs.size() == 0) return;*/ auto & quadVBs = GetQuadVBs(); auto & quadIB = GetQuadIB(); auto rc = Global::GetRenderEngine()->GetRenderContext(); // Viewport if (width == 0.0f) { if (rtvs.size() > 0) { auto tex = rtvs[0]->GetResource()->Cast<Texture>(); auto & mipSize = tex->GetMipSize(rtvs[0]->Cast<TextureRenderTargetView>()->mipLevel); width = (float)mipSize.x(); } else if (dsv) { auto tex = dsv->GetResource()->Cast<Texture>(); auto & mipSize = tex->GetMipSize(dsv->Cast<TextureDepthStencilView>()->mipLevel); width = (float)mipSize.x(); } } if (height == 0.0f) { if (rtvs.size() > 0) { auto tex = rtvs[0]->GetResource()->Cast<Texture>(); auto & mipSize = tex->GetMipSize(rtvs[0]->Cast<TextureRenderTargetView>()->mipLevel); height = (float)mipSize.y(); } else if (dsv) { auto tex = dsv->GetResource()->Cast<Texture>(); auto & mipSize = tex->GetMipSize(dsv->Cast<TextureDepthStencilView>()->mipLevel); height = (float)mipSize.y(); } } RenderViewport vp; vp.topLeftX = static_cast<float>(topLeftX); vp.topLeftY = static_cast<float>(topLeftY); vp.width = static_cast<float>(width); vp.height = static_cast<float>(height); vp.minDepth = 0.0f; vp.maxDepth = 1.0f; rc->SetViewport(vp); // Update uvs float2 uvMap[4]; uvMap[0] = float2(topLeftU, topLeftV); uvMap[1] = float2(topLeftU + uvWidth, topLeftV); uvMap[2] = float2(topLeftU, topLeftV + uvHeight); uvMap[3] = float2(topLeftU + uvWidth, topLeftV + uvHeight); auto uvBufMappedData = quadVBs[1]->Map(MAP_WRITE_DISCARD); memcpy(uvBufMappedData.pData, uvMap, sizeof(float2) * 4); quadVBs[1]->UnMap(); // Set vbs, ib rc->SetVertexBuffer(quadVBs); rc->SetIndexBuffer(quadIB); rc->SetPrimitiveTopology(PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Set rtv rc->SetRenderTargets(rtvs); // Set dsv rc->SetDepthStencil(dsv); if (!dsv) rc->SetDepthStencilState(DepthStencilStateTemplate<false>::Get()); // Bind shader auto drawQuadVS = Shader::FindOrCreate<DrawQuadVS>(); drawQuadVS->Flush(); // Draw rc->DrawIndexed(0, 0); if (!dsv) rc->SetDepthStencilState(nullptr); }
void ImageBasedLensFlare::LensBlur(const Ptr<Texture> & setupTex, const Ptr<Texture> & target) { int32_t tileSize = 9; //Extract Sprite Points int32_t extractWidth = (setupTex->GetDesc().width + tileSize - 1) / tileSize; int32_t extractHeight = (setupTex->GetDesc().height + tileSize - 1) / tileSize; RenderBufferDesc spPointsBufDesc; spPointsBufDesc.bindFlag = BUFFER_BIND_SHADER_RESOURCE | BUFFER_BIND_UNORDERED_ACCESS; spPointsBufDesc.elementSize = sizeof(float2) + sizeof(float3); spPointsBufDesc.numElements = extractWidth * extractHeight; spPointsBufDesc.cpuAccess = 0; spPointsBufDesc.bStructured = true; auto spPointsBufRef = BufferPool::Instance().FindFree(spPointsBufDesc); auto spPointsBuf = spPointsBufRef->Get()->Cast<RenderBuffer>(); { auto ps = Shader::FindOrCreate<ExtractSpritePointsPS>(); ps->SetScalar("spriteThreshold", _spriteThreshold); ps->SetSRV("setupTex", setupTex->GetShaderResourceView()); ps->SetUAV("spPointsBuf", spPointsBuf->GetUnorderedAccessView(0, 0, RENDER_FORMAT_UNKNOWN, BUFFER_UAV_APPEND)); ps->Flush(); DrawQuad({}, 0.0f, 0.0f, (float)extractWidth, (float)extractHeight); } //Render Sprites if (!_indirectAgsBuf) { RenderBufferDesc indirectArgsBufDesc; indirectArgsBufDesc.bindFlag = BUFFER_BIND_INDIRECT_ARGS; indirectArgsBufDesc.elementSize = 16; indirectArgsBufDesc.numElements = 1; indirectArgsBufDesc.cpuAccess = 0; indirectArgsBufDesc.bStructured = false; uint32_t initData[] = { 0, 1, 0, 0 }; _indirectAgsBuf = Global::GetRenderEngine()->GetRenderFactory()->CreateBuffer(); _indirectAgsBuf->SetDesc(indirectArgsBufDesc); _indirectAgsBuf->Init(initData); } spPointsBuf->CopyStructureCountTo(_indirectAgsBuf, 0, 0, spPointsBuf->GetDesc().numElements, RENDER_FORMAT_UNKNOWN, BUFFER_UAV_APPEND); { auto vs = Shader::FindOrCreate<LensBlurVS>(); auto gs = Shader::FindOrCreate<LensBlurGS>(); auto ps = Shader::FindOrCreate<LensBlurPS>(); vs->SetScalar("texSize", target->GetTexSize()); gs->SetScalar("texSize", target->GetTexSize()); gs->SetScalar("flareIntensity", _flareIntensity); vs->SetSRV("spPointsRenderBuf", spPointsBuf->GetShaderResourceView(0, 0, RENDER_FORMAT_UNKNOWN)); auto lensTexAsset = Asset::Find<TextureAsset>("Textures/Bokeh_Circle.dds"); if (!lensTexAsset->IsInit()) lensTexAsset->Init(); auto lensTex = lensTexAsset->GetTexture(); ps->SetSRV("lensTex", lensTex->GetShaderResourceView()); ps->SetSampler("linearSampler", SamplerTemplate<>::Get()); vs->Flush(); gs->Flush(); ps->Flush(); auto rc = Global::GetRenderEngine()->GetRenderContext(); rc->SetVertexBuffer({}); rc->SetIndexBuffer(nullptr); rc->SetPrimitiveTopology(PRIMITIVE_TOPOLOGY_POINTLIST); rc->SetViewport(GetTextureQuadViewport(target)); rc->SetRenderTargets({ target->GetRenderTargetView(0, 0, 1) }); rc->SetDepthStencil(nullptr); rc->SetDepthStencilState(DepthStencilStateTemplate<false>::Get()); rc->SetBlendState(BlendStateTemplate<false, false, true, BLEND_PARAM_SRC_ALPHA, BLEND_PARAM_ONE, BLEND_OP_ADD>::Get()); rc->DrawInstancedIndirect(_indirectAgsBuf, 0); rc->ResetShader(SHADER_GS); rc->SetBlendState(nullptr); } }
KIHX_API void kiRenderToBuffer( void* buffer, int width, int height, int bpp ) { if ( buffer == nullptr ) { return; } __UNDONE( temporal testing code ); // Create color and depth stencil buffers. ColorFormat colorFormat = kih::GetSuitableColorFormatFromBpp( bpp ); static std::shared_ptr<Texture> renderTarget = Texture::Create( width, height, colorFormat, buffer ); static std::shared_ptr<Texture> depthStencil = Texture::Create( width, height, ColorFormat::D32F, NULL ); static std::shared_ptr<UnorderedAccessView<OutputMergerInputStream>> omUAV = std::make_shared<UnorderedAccessView<OutputMergerInputStream>>( width * height * 2 ); // Create rendering contexts as many of Thread::HardwareConcurrency. const int Concurrency = 8; // HACK static std::array<std::shared_ptr<RenderingContext>, Concurrency> contexts; if ( contexts[0] == nullptr ) { int index = 0; LoopUnroll<Concurrency>::Work( [&index]() { auto context = RenderingDevice::GetInstance()->CreateRenderingContext(); context->SetViewport( 0, 0, static_cast<unsigned short>( renderTarget->Width() ), static_cast<unsigned short>( renderTarget->Height() ) ); context->SetFixedPipelineMode( false ); context->SetRenderTarget( renderTarget, 0 ); context->SetDepthStencil( depthStencil ); contexts[index++] = context; } ); } auto context = contexts[0]; context->Clear( 0, 0, 0, 255 ); context->SetUnorderedAccessView( nullptr ); // Draw the world. const Matrix4& matWorld = RenderingDevice::GetInstance()->GetWorldMatrix(); // Grid arragement if ( grid_scene.Bool() ) { // threading or not if ( concurrency.Int() <= 1 ) { DrawGridScene( context, matWorld ); } else { // Copy array contexts to vector one. StlVector<std::shared_ptr<RenderingContext>> concurrencyContexts; concurrencyContexts.resize( concurrency.Int() - 1 ); // the other thread is for resolve. for ( size_t i = 0; i < concurrencyContexts.size(); ++i ) { contexts[i]->SetUnorderedAccessView( omUAV ); concurrencyContexts[i] = contexts[i]; } DrawGridSceneInParallel( concurrencyContexts, matWorld ); } } else { context->GetSharedConstantBuffer().SetMatrix4( ConstantBuffer::WorldMatrix, matWorld ); context->GetSharedConstantBuffer().SetVector4( ConstantBuffer::DiffuseColor, Vector4( 1.0f, 1.0f, 1.0f, 1.0f ) ); context->Draw( GetMeshes()[0] ); } }