gam532 dps932 – week 6
DESCRIPTION
GAM532 DPS932 – Week 6. Render Targets and Post Processing. The Back Buffer & Depth Buffer. Buffer Flipping. Frame Buffer. Render. Back Buffer. Depth Buffer. Buffer Description. Back Buffer. Depth Buffer. Type. 2D Texture. 2D Texture. 1440 x 900. 1440 x 900. Resolution. - PowerPoint PPT PresentationTRANSCRIPT
GAM532DPS932 – Week 6Render Targets and Post Processing
The Back Buffer & Depth Buffer
Frame Buffer
Back Buffer Depth Buffer
Render
Buffer Flipping
Buffer Description
1440 x 900
Back Buffer Depth Buffer
2D Texture2D Texture1440 x 900
R8 G8 B8 A8 D24 S8Colors to be display on the monitor
Depth of each pixel for depth testing
TypeResolutionPixel Format
Purpose
What is a Render Target?
Render
Back Buffer
Target Types
Depth Buffer
Color Target
Depth Target
Color Texture
Depth Texture
Target Objects
Bound To
Monitor
System Reserve
Shader Resource
Shader Resource
How to use Render Targets
Color TargetPre-Render Camera
Main Camera
Material
Actor
Scene
2 Renders=
1 Frame
Initializing Color Render Targets
//Create and initialize the textureglGenTextures(1, &textureID);glBindTexture(dim, textureID);glTexStorage2D(dim, 1, iFormat, width, height);
//Create the render target and bind it to the textureglGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,textureID, 0); //Unbind when done glBindTexture(dim, 0);glBindFramebuffer(GL_FRAMEBUFFER, 0);
//First, make a 2D texture we’ll use for the targetD3D11_TEXTURE2D_DESC desc;desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;desc.CPUAccessFlags = 0;desc.Format = //Texture formatdesc.MipLevels = 1;desc.MiscFlags = 0;desc.Usage = D3D11_USAGE_DEFAULT;//Fill in remainder of struct
dev->CreateTexture2D(&desc, 0, &tex2d);dev->CreateShaderResourceView(tex2d, 0, &texture);
//Make the render target and bind it to the textureD3D11_RENDER_TARGET_VIEW_DESC rDesc;rDesc.Format = desc.Format;rDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;rDesc.Texture2D.MipSlice = 0;
dev->CreateRenderTargetView(tex2d, &rDesc, &rend);
Binding Color Render Targets
glBindFramebuffer(GL_FRAMEBUFFER, targetView);
//Clear before usingglClear(GL_COLOR_BUFFER_BIT);
//Indicate the buffer will output a colorGLenum drawBuffer[1] = {GL_COLOR_ATTACHMENT0};glDrawBuffers(1, drawBuffer);
//Adjust the view port to for the dimensions of//the new render target
glViewport(left, top, targetWidth, targetHeight);
//Clear before bindingcon->ClearRenderTargetView(targetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));
//Bind it as the system’s render targetcon->OMSetRenderTargets(1, &targetView, 0);
//Adjust the view port to for the dimensions of//the new render target
D3D11_VIEWPORT v; v.TopLeftX = left;v.TopLeftY = top;v.MinDepth = 0.0f;v.MaxDepth = 1.0f;v.Width = targetWidth;v.Height = targetHeight;
con->RSSetViewports(1, &v);
Unbinding & Deleting Color Render Targets
//Unbind the color targetglBindFramebuffer(GL_FRAMEBUFFER, 0);
//don’t forget to reset the view ports!
//Delete the texture and color targetglDeleteFramebuffers(1, &framebuffer);glDeleteTextures(1, &textureID);
//To unbind a color target, rebind the system’s//back buffercon->OMSetRenderTargets(1, &backbuffer, 0);
//don’t forget to reset the view ports!
//Delete the texture and color targetrend->release();texture->release();
Target Depth Binding DX vs GL
Color Texture
Color Target Depth Target
Depth Texture
DX Context
Color Texture
Color Target(glFramebuffe
r)
Depth Buffer
Depth Texture
GL Context
Initializing Depth Render Targets
glGenTextures(1, &textureID);glBindTexture(dim, textureID);glTexStorage2D(dim, 1, iFormat, width, height);
glGenRenderbuffers(1, &depthBuffer);glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);glRenderbufferStorage(GL_RENDERBUFFER, iFormat, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);glBindTexture(dim, 0);
D3D11_TEXTURE2D_DESC desc;desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;desc.Format = DXGI_FORMAT_R32_TYPELESS;//fill in rest of struct
dev->CreateTexture2D(&desc, 0, &tex2d);
D3D11_SHADER_RESOURCE_VIEW_DESC desc2;desc2.ViewDimension= D3D11_SRV_DIMENSION_TEXTURE2D;desc2.Format = textureFormat;//single channel//fill in rest of struct
dev->CreateShaderResourceView(tex2d, &desc2, &texture);
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;descDSV.Format = depthFormat;descDSV.ViewDimension=D3D11_DSV_DIMENSION_TEXTURE2D;//fill in rest of struct
dev->CreateDepthStencilView(tex2d,&descDSV,&dsv);
Binding Depth Render Targets
//Bind the buffer to the color targetglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthTarget);
//Bind the texture to the color targetglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
//Clear before usingglClear(GL_DEPTH_BUFFER_BIT);
//Since color and depth targets should match in//when bound together, viewport is only set once
//Clear before bindingcon->ClearDepthStencilView(depthTarget, D3D11_CLEAR_DEPTH, 1.0f, 0);
//Bind it as the system’s render target//(also binds the render target)con->OMSetRenderTargets(1, colorTarget, depthTarget);
//Since color and depth targets should match in//when bound together, viewport is only set once
Unbinding & Deleting Color Render Targets
//Unbind the depth target/texture from color targetglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
//Delete the texture and color targetglDeleteRenderbuffers(1, &depthBuffer);glDeleteTextures(1, &textureID);
//To unbind a depth target, set it to zero//Alternative, set it back to the system’s buffercon->OMSetRenderTargets(1, &backbuffer, depthbuffer);
//Delete the texture and color targetdsv->release();texture->release();
Uses of Render Targets
Dynamic Mirrors Post Processing Effects Shadows
Setting Up Dynamically Rear View
Main CameraRear View
Setting Up a Dynamic Mirror
Main Camera
Mirror Camera
Actors
Fragment Isolation
Fragments Processed Concurrently
Limited Scope, no adjacency data
GlowMotion Blur
Following effects are difficult:
Post Processing
Color TargetRender Camera
Post Processing Camera
Screen Space Quad (100%)
Back Buffer
1 Fragment
Can sample from
Any number of Texels
Basic Post Processing Effects
Storing Post Processing Data
Color for Frame BufferRed Green Blue Alpha
Often unused
Red Green Blue Luminosity
Color for Color Target (Post Processing)
Make use of unused variables to store data for post
processing
Alternatively…Hiding Data in Color
1st Color Target 2nd Color Target
Use the 2nd color target to store post processing data which you use to manipulate
the colors in the 1st
Post Processing Effects
God Rays
Screen Distortion Motion Blur
To Do• Read over Lab 5• Finish Proposal Documents and Prepare to Submit• Research your engine enhancement