direct3d11 kickstart

Download Direct3D11  KickStart

Post on 23-Feb-2016

31 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Direct3D11 KickStart. Sz écsi László. Direct3D11 SDK. June 2010 DirectX SDK Visual Studio 2010. GPU pipeline input. vertex bufferek index buffer rajzolási állapot egyes pipelineelemek működési beállításai programozható elemek shader programjai - PowerPoint PPT Presentation

TRANSCRIPT

Direct3D11 KickStart

Direct3D11 KickStartSzcsi LszlDirect3D11 SDKJune 2010 DirectX SDKVisual Studio 2010GPU pipeline inputvertex bufferekindex bufferrajzolsi llapotegyes pipelineelemek mkdsi belltsaiprogramozhat elemek shader programjaierforrsok globlis memriban adatokgloblis (uniform) vltozktextrkadatbufferek

csak olvashatGPU pipeline outputkprender targetframe buffertextramlysgbuffer (+stencil)adatbufferstream out targetread/write (GpGpu-hoz)unordered access viewGPU pipelineIAVSHSDSGSPSOMRSTesstessfactortessfactorIASO+mindeki olvashatja a uniform vltozkat, textrkat, buffereketRajzolsi llapotIAOMRSTesstessfactorIASOinput layoutprimitivetopologyrasterizerstatedepth-stencilstateblendstateviewportAlapinterface-ekID3D11Device (1 van)erforrsok ltrehozsa, felszabadtsatextra, buffer, shader, state blockID3D11DeviceContext (1 immediate, * deferred)rajzolsi llapot, I/O, shader belltsrajzolsID3DXEffect (*)shaderek fordtsa, menedzselseshaderek, rajzolsi llapotok belltsa scriptbl

ID3D11Device interface metdusokerforrsok ltrehozsaCreateBufferCreateTexture2Derforrsokhoz nzetek ltrehozsaCreateShaderResourceViewCreateRenderTargetViewshaderek s rajzolsillapot-struktrkCreateVertexShaderCreateBlendState

ID3DDeviceContextbemenetek s kimenetek belltsaIASetIndexBuffer, IASetVertexBuffersOMSetRenderTargetsrajzolsi llapot belltsaRSSetState, OMSetBlendStateshaderek, uniform paramterek belltsa VSSetShader, PSSetShaderrajzolsDraw, DrawIndexed, DrawIndexInstancedDXUTmint a GLUTablakozs, esemnykezels, callback fggvnyekcsom minden mskamera osztlyokuser interfacenem kln lib, hanem forrsban ott van minden Empty projectben alapblDXUT Callback bellts DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );itt mg nincs sz rla, hogy melyik mit csinljon11DXUT initDXUTInit( true, true, NULL ); DXUTSetCursorSettings( true, true );DXUTCreateWindow( L"KickStart11" );

DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 640, 480 );DXUTMainLoop(); // Enter into the DXUT ren der loopEmpty projectElz kt din lv kd mr benne leszStart Menu/DirectX SDK (June 2010)/DirectX Sample Browserinstall EmptyProject11project neve: KickStart11Visual Studio 2010KickStart11_2010.sln megnyitsaDirectX 9 tmogats kidobsa (opcionlis)delete EmptyProject9.cppminden D3D9-et tartalmaz sor trlse a KickStart11.cpp-bl9-es libek kidobsa a linker inputblInclude s lib knyvtrak belltsaproject properties (ez csak erre a projectre lesz rvnyes)Configuration properties/c/c++/general/additional include directoriesC:\Program Files\Microsoft DirectX SDK (June 2010)\IncludeConfiguration properties/linker/general/additional library directoriesC:\Program Files\Microsoft DirectX SDK (June 2010)\Lib\x86Include s lib knyvtrak belltsagp alapon (minden projectre rvnyes lesz)view menu: property managerproperty manager ablakbanpl. Debug | Win32Microsoft.Cpp.Win32.User duplaklikkVC++ directories al az elz dikon szerepl knyvtrak belltsaItt tartunk res ablak

KickStart11.cppgloblis callback fggvnyekeszkzesemnyekOnD3D11CreateDeviceprogram indulOnD3D11ResizedSwapChainablak ltrejttOnD3D11ReleasingSwapChainablak megszntOnD3D11DestroyDeviceprogram vgevezrlsi esemnyekOnD3D11FrameRenderrajzolsOnFrameMoveanimciMsgProcwindows zenet

Dxa::Base interfaceclszer, hogy a globlis callback fggvnyek egy sajt objektum metdusait hvjkott tudjuk megvalstani a funkcitnem kell a KickStart11.cpp-be nylklniahhoz, hogy a sajt objektum knnyen cserlhet legyen, legyen egy sinterfaceminden legyen a Dxa namespace-benaz sinterface a Dxa::Baseimplementl osztly Dxa::Sas : public Dxa::Base

Dxa.h#pragma oncenamespace Dxa {class Base{protected:ID3D11Device* device;IDXGISwapChain* swapChain;DXGI_SURFACE_DESC backbufferSurfaceDesc;public:Base(ID3D11Device* device){this->device = device; swapChain = NULL;}void setSwapChain(IDXGISwapChain* swapChain, const DXGI_SURFACE_DESC* backbufferSurfaceDesc) { this->swapChain = swapChain; this->backbufferSurfaceDesc = *backbufferSurfaceDesc;}virtual ~Base(){}// insert code from slide #0.1 here};}#0.0Dxa.h// insert this into code on slide #0.0virtual HRESULT createResources(){return S_OK;}virtual HRESULT createSwapChainResources(){return S_OK;}virtual HRESULT releaseResources(){return S_OK;}virtual HRESULT releaseSwapChainResources(){return S_OK;}virtual void animate(double dt, double t){}virtual bool processMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){return false;}virtual void render(ID3D11DeviceContext* context){}#0.1KickStart11.cpp#include "DXUT.h#include "Dxa.h"Dxa::Base* application;#1.0KickStart11.cppHRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {application = new Dxa::Base(pd3dDevice);application->createResources();return S_OK;}#1.1KickStart11.cppHRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ){application->setSwapChain(pSwapChain,pBackBufferSurfaceDesc);application->createSwapChainResources();return S_OK;}#1.2KickStart11.cppvoid CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext ){if(application)application->animate(fElapsedTime, fTime);}#1.3KickStart11.cppvoid CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ){ // Clear render target and the depth stencil float ClearColor[4] = { 0.176f, 0.196f, 0.667f, 0.0f };

ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView(); ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor ); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );}#1.4KickStart11.cppvoid CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ){if(application)application->render(pd3dImmediateContext);}#1.5KickStart11.cppvoid CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext ){application->releaseSwapChainResources();}#1.6KickStart11.cppvoid CALLBACK OnD3D11DestroyDevice( void* pUserContext ){application->releaseResources();delete application;application = NULL;}#1.7KickStart11.cppLRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ){if(application)application->processMessage(hWnd, uMsg, wParam, lParam);return 0;}#1.8

Itt tartunk

kivettk a kpernytrlst! memria-szemt lehet az ablakban elvileg31Hromszg rajzolsaDxa::Triangleerforrs ltrehozs/felszabadts (vertex buffer), render target trls, rajzolsDxa::TriangleDxa::Base nem csinl semmitszrmaztatunk belle Dxa::Triangle-tfelldefiniljuk a virtulis esemnykezel metdusokatcreateResourcesreleaseResourcerender

DxaTriangle.h#pragma once#include "dxa.h"

namespace Dxa {class Triangle :public Dxa::Base{public:Triangle(ID3D11Device* device);HRESULT createResources();HRESULT releaseResources();void render(ID3D11DeviceContext* context);};

}#2.0DxaTriangle.cpp#include "DXUT.h"#include "DxaTriangle.h"

Dxa::Triangle::Triangle(ID3D11Device* device):Base(device){}HRESULT Dxa::Triangle::createResources(){return S_OK;}HRESULT Dxa::Triangle::releaseResources(){return S_OK;}void Dxa::Triangle::render(ID3D11DeviceContext* context){}

#2.1KickStart11.cpp#include "DXUT.h"#include "Dxa.h"Dxa::Base* application;#include "DxaTriangle.h"

#2.3KickStart11.cppHRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ){

application = new //Dxa::Base(pd3dDevice);Dxa::Triangle(pd3dDevice);application->createResources(); return S_OK;}

#2.4

Itt tartunkde mr a Dxa::Triangle metdusai hvdnak38Ezekre lesz szksgIAVSPSOMRSIAvertexbufferinputlayoutvertexshaderpixelshaderprimitivetopologymaraddefaultmaraddefault= [x, y, z][x, y, z][x, y, z, 1][x, y, z, 1][x, y, z, 1]DxaTriangle.hclass Triangle :public Dxa::Base{ID3D11Buffer* vertexBuffer;ID3D11InputLayout* inputLayout;ID3D11VertexShader* vertexShader;ID3D11PixelShader* pixelShader;

public:#2.5DxaTriangle.cppHRESULT Dxa::Triangle::createResources() {D3D11_BUFFER_DESC desc;desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;desc.ByteWidth = sizeof(D3DXVECTOR3) * 3;desc.CPUAccessFlags = 0;desc.MiscFlags = 0;desc.StructureByteStride = sizeof(D3DXVECTOR3);desc.Usage = D3D11_USAGE_IMMUTABLE;D3DXVECTOR3 vertexPositionArray[3] = { D3DXVECTOR3(0, 0, 0.5),D3DXVECTOR3(0, 1, 0.5),D3DXVECTOR3(1, 0, 0.5) };D3D11_SUBRESOURCE_DATA initData;initData.pSysMem = vertexPositionArray;initData.SysMemPitch = 0;initData.SysMemSlicePitch = 0;device->CreateBuffer(&desc, &initData, &vertexBuffer);

#2.6DxaTriangle.cppdevice->CreateBuffer(&desc, &initData, &vertexBuffer);const char* vertexShaderCode = "float4 vsIdle(float4 pos :POSITION ) :SV_Position {return pos;}";ID3DBlob* vertexShaderByteCode;D3DX11CompileFromMemory(vertexShaderCode, strlen(vertexShaderCode), NULL, NULL, NULL, "vsIdle", "vs_5_0", 0, 0, NULL, &vertexShaderByteCode, NULL, NULL);

device->CreateVertexShader(vertexShaderByteCode->GetBufferPointer(), vertexShaderByteCode->GetBufferSize(), NULL, &vertexShader);

#2.7DxaTriangle.cpp// insert after #2.7D3D11_INPUT_ELEMENT_DESC positionElement;positionElemen