Presentation is loading. Please wait.

Presentation is loading. Please wait.

Render to texture Deferred shading Post-processing blur Szécsi László.

Similar presentations


Presentation on theme: "Render to texture Deferred shading Post-processing blur Szécsi László."— Presentation transcript:

1 Render to texture Deferred shading Post-processing blur Szécsi László

2 Új class EngineDeferred base: EngineCore //.h EngineDeferred( LPDIRECT3DDEVICE9 device); //.cpp EngineDeferred::EngineDeferred( LPDIRECT3DDEVICE9 device) :EngineCore(device){}

3 EngineDeferred.cpp #include "DXUT.h" #include "EngineDeferred.h" #include "Camera.h" #include "NodeGroup.h" #include "ShadedMesh.h"

4 GraphGame.cpp #include "EngineDeferred.h" EngineInterface* EngineInterface:: createEngineInstance( LPDIRECT3DDEVICE9 device) { return new EngineDeferred(device); }

5 Próba minden a régi

6 XML karaktert, vizet dobjuk ki autók, terep maradhat

7 EngineDeferred virtual HRESULT createDefaultResources(); virtual HRESULT releaseDefaultResources(); virtual void render();

8 Deferred bufferek class EngineDeferred{ … LPDIRECT3DSURFACE9 displayColorBuffer; LPDIRECT3DSURFACE9 displayDepthStencilBuffer; LPDIRECT3DTEXTURE9 deferredGeometryTexture; LPDIRECT3DSURFACE9 deferredGeometrySurface; LPDIRECT3DTEXTURE9 deferredBrdfTexture; LPDIRECT3DSURFACE9 deferredBrdfSurface;

9 EngineDeferred:: createDefaultResources HRESULT EngineDeferred:: createDefaultResources() { __super::createDefaultResources(); …

10 Létrehozás: createDefaultResources device->GetRenderTarget(0, &displayColorBuffer); device->GetDepthStencilSurface(& displayDepthStencilBuffer ); D3DSURFACE_DESC viewportDesc; displayColorBuffer->GetDesc(&viewportDesc); device->CreateTexture( viewportDesc.Width, viewportDesc.Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &deferredGeometryTexture, NULL); deferredGeometryTexture->GetSurfaceLevel( 0, &deferredGeometrySurface); // deferredBrdfTexture, surface ugyanígy

11 EngineDeferred:: createDefaultResources vége return S_OK; }

12 Felszabadítás: ReleaseDefaultResources __super::releaseDefaultResources(); displayColorBuffer->Release(); displayDepthStencilBuffer->Release(); deferredGeometryTexture->Release(); deferredGeometrySurface->Release(); deferredBrdfTexture->Release(); deferredBrdfSurface->Release(); return S_OK;

13 EngineDeferred::render ezzel felülírjuk az EngineCore::rendert -deferring bufferek kitöltése -új role -quadok rajzolása -ami kék, azt egyelőre kommentezzük ki

14 EngineDeferred::render void EngineDeferred::render() { if(sceneRoot == NULL) return; effect->SetMatrix("rotProjMatrixInverse", ¤tCamera->second- >getOrientProjMatrixInverse()); effect->SetFloatArray("eyePosition", (float*)¤tCamera->second- >getEyePosition(), 3); device->BeginScene();

15 render folyt: buffer kitöltés device->SetRenderTarget(0, deferredGeometrySurface ); device->SetRenderTarget(1, deferredBrdfSurface ); device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 200, 0), 1.0f, 0); sceneRoot->render(RenderContext(device, effect, currentCamera->second, L"defer")); device->SetRenderTarget(0, displayColorBuffer); device->SetRenderTarget(1, NULL);

16 render folyt. effect->SetTexture("deferredGeometryMap", deferredGeometryTexture); effect->SetTexture("deferredBrdfMap", deferredBrdfTexture); effect->SetTechnique("deferred"); UINT np; effect->Begin(&np, 0); effect->BeginPass(0); renderFullViewportQuad(); effect->EndPass(); effect->End();

17 render vége device->EndScene(); }

18 Új szerep: defer EngineDefer::createAutoShadedMesh virtual ShadedMesh* createAutoShadedMesh(const std::wstring& meshName, LPD3DXMESH mesh, LPD3DXBUFFER materialBuffer, unsigned int nSubmeshes);

19 Új szerep: defer EngineDefer::createAutoShadedMesh ShadedMesh* EngineDeferred::createAutoShadedMesh(const std::wstring& meshName, LPD3DXMESH mesh, LPD3DXBUFFER materialBuffer, unsigned int nSubmeshes) { ShadedMesh* shadedMesh = new ShadedMesh(mesh); shadedMeshDirectory[meshName] = shadedMesh; shadedMesh->addRole( L"basic", new Role(materialBuffer, nSubmeshes, textureDirectory, device, "basic")); shadedMesh->addRole( L"defer", new Role(materialBuffer, nSubmeshes, textureDirectory, device, "defer")); return shadedMesh; }

20 defer.fx: Új technique void psDefer(TrafoOutput input, out float4 geometry :COLOR0, out float4 brdf :COLOR1){ float3 normal = normalize(input.normal); geometry.xyz = normal; geometry.w = length(input.worldPos - eyePosition); brdf = kdColor * tex2D(kdMapSampler, input.tex); brdf.w = 1; } technique defer { pass ExamplePass { VertexShader = compile vs_2_0 vsTrafo(); PixelShader = compile ps_3_0 psDefer(); }}

21 defer.fx: deferred technique samplerek texture deferredGeometryMap; sampler2D deferredGeometrySampler = sampler_state{ texture = ; MinFilter = POINT; MagFilter = POINT; MipFilter = POINT; }; texture deferredBrdfMap; sampler2D deferredBrdfSampler = sampler_state{ texture = ; MinFilter = POINT; MagFilter = POINT; MipFilter = POINT; };

22 deferred pixel shader float4 psDeferred(QuadOutput input) : COLOR0 { float4 geometry = tex2D( deferredGeometrySampler, input.tex); float4 brdf = tex2D( deferredBrdfSampler, input.tex); float3 viewDir = normalize(input.worldPosMinusEye); float3 worldPos = eyePosition + viewDir * geometry.w; //... return brdf * geometry.y; };

23 Technique technique deferred { pass ExamplePass { VertexShader = compile vs_2_0 vsQuad(); PixelShader = compile ps_3_0 psDeferred(); }}

24 Próba látjuk a normálvektorokat renderben kék kód visszakommentezése hiba: nem töröltük a képet kell egy clear a rendertarget visszaállítás után hiba: quad textúra koordináták psDeferred: input.tex.yx * float2(-1,1) normál rajzolás (psDeferred)

25 mindenkinek kell defer role pl. terrain void psTerrainDefer(vsTerrainOutput input, out float4 geometry : COLOR0, out float4 brdf : COLOR1) { … brdf = lerp(c1, c2, input.tileOffset / tileRatio.y - p1); geometry = 0; } technique terrainDefer { pass ExamplePass { CullMode = CW; VertexShader = compile vs_3_0 vsTerrain(); PixelShader = compile ps_3_0 psTerrainDefer(); }

26 XML

27 Blur

28 Blur EngineDeferred.h LPDIRECT3DTEXTURE9 postProcTexture; LPDIRECT3DSURFACE9 postProcSurface;

29 EngineDeferred:: createDefaultResources device-> CreateTexture( viewportDesc.Width, viewportDesc.Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &postProcTexture, NULL); postProcTexture->GetSurfaceLevel(0, &postProcSurface);

30 World::releaseDefaultResources postProcTexture->Release(); postProcSurface->Release();

31 deferres menjen a PostProc bufferbe EngineDeferred::render device->SetRenderTarget(0, postProcSurface); device->SetRenderTarget(1, NULL); effect- >SetTexture("deferredGeometryMap", deferredGeometryTexture); effect->SetTexture("deferredBrdfMap", deferredBrdfTexture); effect->SetTechnique("deferred"); …

32 H és V blur lépések device->SetRenderTarget(0, deferredGeometrySurface); effect->SetTexture("deferredGeometryMap", postProcTexture); effect->SetTechnique("blurHorizontal"); effect->Begin(&np, 0); effect->BeginPass(0); renderFullViewportQuad(); effect->EndPass(); effect->End(); device->SetRenderTarget(0, displayColorBuffer); effect->SetTexture("deferredGeometryMap", deferredGeometryTexture); effect->SetTechnique("blurVertical"); effect->Begin(&np, 0); effect->BeginPass(0); renderFullViewportQuad(); effect->EndPass(); effect->End();

33 .fx-be sampler2D postProcSampler = sampler_state{ texture = ; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; };

34 blur vertex shader struct BlurInput { float4 pos: POSITION; float2 tex: TEXCOORD0; }; struct BlurOutput { float4 pos: POSITION; float2 tex: TEXCOORD0; }; BlurOutput vsBlur(BlurInput input) { BlurOutput output = (BlurOutput)0; output.pos = input.pos; output.tex = input.tex.yx * float(-1, 1); return output; };

35 horiz pixel shader float4 psBlurHorizontal(BlurOutput input) : COLOR0 { float4 filtered = tex2D( postProcSampler, input.tex + float2(8.5/640.0, 0.5/480.0)) + tex2D( postProcSampler, input.tex + float2(4.5/640.0, -0.5/480.0)) * 2 + tex2D( postProcSampler, input.tex + float2(0.5/640.0, 0.5/480.0)) * 3 + tex2D( postProcSampler, input.tex + float2(- 3.5/640.0, -0.5/480.0)) * 3 + tex2D( postProcSampler, input.tex + float2(- 7.5/640.0, 0.5/480.0)) * 2 + tex2D( postProcSampler, input.tex + float2(- 11.5/640.0, -0.5/480.0)); return filtered / 12; };

36 technique technique blurHorizontal { pass P0 { ZENABLE = FALSE; ZWRITEENABLE = FALSE; CULLMODE = NONE; VertexShader = compile vs_3_0 vsBlur(); PixelShader = compile ps_3_0 psBlurHorizontal(); } };

37 Vertical float4 psBlurVertical(BlurOutput input) : COLOR0 { float4 filtered = tex2D( postProcSampler, input.tex + float2(8.5/640.0, 0.5/480.0).yx) + tex2D( postProcSampler, input.tex + float2(4.5/640.0, -0.5/480.0).yx) * 2 + tex2D( postProcSampler, input.tex + float2(0.5/640.0, 0.5/480.0).yx) * 3 + tex2D( postProcSampler, input.tex + float2(-3.5/640.0, -0.5/480.0).yx) * 3 + tex2D( postProcSampler, input.tex + float2(-7.5/640.0, 0.5/480.0).yx) * 2 + tex2D( postProcSampler, input.tex + float2(-11.5/640.0, -0.5/480.0).yx) ; return filtered / 12; }; technique blurVertical { pass P0 { ZENABLE = FALSE; ZWRITEENABLE = FALSE; CULLMODE = NONE; VertexShader = compile vs_3_0 vsBlur(); PixelShader = compile ps_3_0 psBlurVertical(); } };


Download ppt "Render to texture Deferred shading Post-processing blur Szécsi László."

Similar presentations


Ads by Google