1 / 12

Instanced Rendering in Direct3D: A Guide to Efficient Game Development Techniques

This document outlines the process of implementing instanced rendering in a Direct3D game project. It covers the creation of vertex and instance buffers, the configuration of shaders for instancing, and the dynamic generation of model matrices for multiple instances. The tutorial includes code snippets demonstrating how to set up the rendering pipeline, manage resources, and apply transformations while ensuring optimal performance. Designed for game developers, it combines practical advice with programming examples in C++ and HLSL.

dasan
Télécharger la présentation

Instanced Rendering in Direct3D: A Guide to Efficient Game Development Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GraphGamegg010-Instancing Geometry instancing SzécsiLászló

  2. gg010-Instancingproject • copy-paste-renamegg009-Guifolder • vcxproj, filters átnevezés • solution/add existing project • rename project • working dir: $(SolutionDir) • Project Properties/ConfigurationProperties/Debugging/CommandArguments --solutionPath:"$(SolutionDir)" --projectPath:"$(ProjectDir)" • build, run

  3. Game.cpp #include "Mesh/Instanced.h"

  4. Mesh::Instanced • index buffer + vertex bufferek + instance bufferek + elemleírás • konstruktor összebuherálja az egyes bufferekhez kapott elemleírásokat • draw metódus • context->IASetIndexBuffer(indexBuffer, indexFormat, 0); • context->IASetVertexBuffers(0, nVertexBuffers, vertexBuffers, vertexStrides, zeros); • context->DrawIndexedInstanced(nIndices, nInstances, 0, 0, 0);

  5. Game::createResources using namespace Egg::Math; Egg::Math::float4x4 modelMatrices[200]; for(intiInstance=0; iInstance<200; iInstance++ ) modelMatrices[iInstance] = float4x4::translation( float3::random(0, 20) ).transpose();

  6. Game::createResources D3D11_INPUT_ELEMENT_DESC modelMatrixElements[4] = { { "INSTANCEMODEL", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0 * sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1}, { "INSTANCEMODEL", 1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 1 * sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1}, { "INSTANCEMODEL", 2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 2 * sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1}, { "INSTANCEMODEL", 3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 3 * sizeof(float4), D3D11_INPUT_PER_INSTANCE_DATA, 1} }; Egg::Mesh::InstanceBufferDescinstanceBufferDesc; instanceBufferDesc.elements = modelMatrixElements; instanceBufferDesc.nElements = 4; instanceBufferDesc.instanceStride = sizeof(float4x4); instanceBufferDesc.instanceData = modelMatrices;

  7. Game::createResources Egg::Mesh::Instanced::P instancedMesh= Egg::Mesh::Instanced::create( device, 200, &instanceBufferDesc, 1, indexedMesh); ID3DX11EffectPass* basicPass = effect->GetTechniqueByName("instanced")->GetPassByName("instanced"); Egg::Mesh::Material::P envmappedMaterial = Egg::Mesh::Material::create(basicPass, 0); shadedMesh = binder->bindMaterial(envmappedMaterial, instancedMesh);

  8. #9.0 fx/main.fx structIaosInstanced { float4 pos: POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD; float4x4 instanceModelMatrix: INSTANCEMODEL; }; VsosTrafovsInstanced(IaosInstanced input) { VsosTrafooutput = (VsosTrafo)0; output.worldPos= mul(input.pos, input.instanceModelMatrix); output.pos = mul(output.worldPos, modelViewProjMatrix); output.normal= mul( float4(input.normal.xyz, 0.0), input.instanceModelMatrix); output.tex = input.tex; return output; } modellel nemkéne szorozni, mertugye már megvolt modelinversetransposed csak akkor egyezik meg a modellel, ha csak eltolás+forgatás+izometrikus skálázás van

  9. #9.0 fx/main.fx technique11 instanced { pass instanced { SetVertexShader( CompileShader(vs_5_0, vsInstanced() ) ); SetPixelShader( CompileShader(ps_5_0, psEnvMapped() ) ); } }

  10. Zsiráfok

  11. FELADAT: elforgatottzsiráfok

  12. FELADAT: forgó zsiráfok • instancedMeshtagvaltozóba • animateből elérjük, releaseResourcesbanreset • animate • instancebuffer lekérése • device->GetImmediateContext • instancebuffermappelése • feltöltése időfüggő (de nem random!) adattal • unmap • contextrelease

More Related