沒想到(啟用dx12旗艦版功能)啟用dx11并設(shè)置超級(jí)文理,DX12渲染引擎(八)常量緩存,梅婷的丈夫是誰(shuí),
目錄:
1.啟用dx12有什么用
2.開啟dx12會(huì)提升幀數(shù)嗎
3.怎么開啟dx12模式
4.如何開啟dx12u
5.開啟dx12
6.dx12開啟有什么用
7.打開dx12
8.怎么啟用dx12
9.游戲開啟dx12
10.如何打開dx12
1.啟用dx12有什么用
我們可以通過常量緩存把數(shù)據(jù)傳遞給著色器,這篇文章咱們就使用常量緩存?zhèn)鬟f一個(gè)位置的偏移給著色器,讓我們的四邊形動(dòng)起來(lái)常量緩存常量緩存區(qū)結(jié)構(gòu)體首先定義咱們需要傳遞給著色器的結(jié)構(gòu)體,這個(gè)結(jié)構(gòu)體大小必須是256字節(jié)對(duì)齊。
2.開啟dx12會(huì)提升幀數(shù)嗎
structSceneConstantBuffer{XMFLOAT4offset;};添加一個(gè)256字節(jié)對(duì)齊的方法康德樂大藥房官網(wǎng)買藥templateconstexprUINTCalcConstantBufferByteSize
3.怎么開啟dx12模式
(){// Constant buffers must be a multiple of the minimum hardware // allocation size (usually 256 bytes). So round up to nearest
4.如何開啟dx12u
// multiple of 256. We do this by adding 255 and then masking off // the lower 2 bytes康德樂大藥房官網(wǎng)買藥 which store all bits < 256.
5.開啟dx12
// Example: Suppose byteSize = 300. // (300 + 255) & ~255 // 555 & ~255 // 0x022B & ~0x00ff // 0x022B & 0xff00
6.dx12開啟有什么用
// 0x0200 // 512 UINTbyteSize=sizeof(T);return(byteSize+255)&~255;}常量緩存堆創(chuàng)建常量緩存堆和其他的堆差不多D3D12_D康德樂大藥房官網(wǎng)買藥ESCRIPTOR_HEAP_DESC。
7.打開dx12
cbvHeapDesc={};cbvHeapDesc.NumDescriptors=1;cbvHeapDesc.Type=D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;cbvHeapDesc
8.怎么啟用dx12
.Flags=D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;ThrowIfFailed(device->CreateDescriptorHeap(&cbvHeapDesc
9.游戲開啟dx12
,IID_PPV_ARGS(&cbvHeap)));常量緩存constUI康德樂大藥房官網(wǎng)買藥NTconstantBufferSize=CalcConstantBufferByteSize
10.如何打開dx12
>();CD3DX12_RESOURCE_DESCconstantResourceDes=CD3DX12_RESOURCE_DESC::Buffer(constantBufferSize);ThrowIfFailed
(device->CreateCommittedResource(&heapProperties,D3D12_HEAP_FLAG_NONE,&constantResourceDes,D3D12_RESOURCE_STATE_GENERIC_READ
,nullptr,I康德樂大藥房官網(wǎng)買藥ID_PPV_ARGS(&constantBuffer)));D3D12_CONSTANT_BUFFER_VIEW_DESCcbvDesc={};cbvDesc.BufferLocation
=constantBuffer->GetGPUVirtualAddress();cbvDesc.SizeInBytes=constantBufferSize;device->CreateConstantBufferView
(&cbvDesc,cbvHeap->GetCPUDescriptorHandleForHeapStart());ThrowIfFailed(constantBuffer->Map(0,&康德樂大藥房官網(wǎng)買藥readRange
,reinterpret_cast(&pCbvDataBegin)));memcpy(pCbvDataBegin,&constantBufferData,sizeof(constantBufferData
));根簽名根簽名由根參數(shù)組成,根參數(shù)可以是根常量、根描述符和描述符表,在這里創(chuàng)建一個(gè)描述符表,存放了咱們的常量緩存視圖D3D12_FEATURE_DATA_ROOT_SIGNATUREfeatureData。
={};featureData.HighestVersion=D3D_ROOT_SIGNATURE_VERSION_1_1;if(FAILED(device->Chec康德樂大藥房官網(wǎng)買藥kFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE
,&featureData,sizeof(featureData)))){featureData.HighestVersion=D3D_ROOT_SIGNATURE_VERSION_1_0;}CD3DX12_DESCRIPTOR_RANGE1
ranges[1];CD3DX12_ROOT_PARAMETER1rootParameters[1];ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV,1,0
,0,D3D12_DESCRIPTOR_RANGE_FLAG_DAT康德樂大藥房官網(wǎng)買藥A_STATIC);rootParameters[0].InitAsDescriptorTable(1,&ranges[0],D3D12_SHADER_VISIBILITY_VERTEX
);D3D12_ROOT_SIGNATURE_FLAGSrootSignatureFlags=D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
|D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS|D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER康德樂大藥房官網(wǎng)買藥_ROOT_ACCESS
|D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS|D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS
;CD3DX12_VERSIONED_ROOT_SIGNATURE_DESCrootSignatureDesc;rootSignatureDesc.Init_1_1(_countof(rootParameters
),rootParameters,0,nullptr,rootSignatureFlags);ComPtrsignature;ComP康德樂大藥房官網(wǎng)買藥trerror;ThrowIfFailed
(D3DX12SerializeVersionedRootSignature(&rootSignatureDesc,featureData.HighestVersion,&signature,&error
));ThrowIfFailed(device->CreateRootSignature(0,signature->GetBufferPointer(),signature->GetBufferSize
(),IID_PPV_ARGS(&rootSignature)));命令列表咱們還需要添加一些代碼去設(shè)置咱們的常量緩存堆ID3D12Descriptor康德樂大藥房官網(wǎng)買藥Heap*ppHeaps[]={cbvHeap.Get
()};commandList->SetDescriptorHeaps(_countof(ppHeaps),ppHeaps);commandList->SetGraphicsRootDescriptorTable
(0,cbvHeap->GetGPUDescriptorHandleForHeapStart());OnUpdate方法咱們?cè)贠nUpdate方法里面對(duì)常量緩存里面的數(shù)據(jù)進(jìn)行更新constfloattranslationSpeed
=0.005f;constfloatoffsetBounds=1.25f;constantBufferD康德樂大藥房官網(wǎng)買藥ata.offset.x+=translationSpeed;if(constantBufferData
.offset.x>offsetBounds){constantBufferData.offset.x=-offsetBounds;}memcpy(pCbvDataBegin,&constantBufferData
,sizeof(constantBufferData));shaders.hlsl在著色器代碼里面添加相應(yīng)的結(jié)構(gòu)體,拿來(lái)接收咱們傳遞過來(lái)的數(shù)據(jù),如何加到咱們的頂點(diǎn)位置上面去,這樣咱們的四邊形就能動(dòng)起來(lái)了。
cbufferSceneConstantBuffer:register(b0)康德樂大藥房官網(wǎng)買藥{float4offset;}structPSInput{float4position:SV_POSITION;float4
color:COLOR;};PSInputVSMain(float4position:POSITION,float4color:COLOR){PSInputresult;result.position=
position+offset;result.color=color;returnresult;}float4PSMain(PSInputinput):SV_TARGET{returninput.color
;}完整代碼#include#include#include#inc康德樂大藥房官網(wǎng)買藥lude#include#include
#include#include"../DXUtils/d3dx12.h"#pragma comment(lib, "d3d12.lib") #pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib") usingnamespaceMicrosoft::WRL;usingnamespaceDirectX;structVertex
{XMFLOAT3position;XMFLOAT4color;};structSceneConstantBuffe康德樂大藥房官網(wǎng)買藥r{XMFLOAT4offset;};constUINTFrameCount=2;UINT
width=800;UINTheight=600;HWNDhwnd;//管線對(duì)象 CD3DX12_VIEWPORTviewport(0.0f,0.0f,width,height);CD3DX12_RECT
scissorRect(0,0,width,height);ComPtrswapChain;ComPtrdevice;ComPtr
>renderTargets[FrameCount];ComPtrcommandAllocator;ComPtrcommandQueue
;ComPtrrootS康德樂大藥房官網(wǎng)買藥ignature;ComPtrrtvHeap;ComPtr
>dsvHeap;ComPtrcbvHeap;ComPtrpipelineState;ComPtr
>commandList;UINTrtvDescriptorSize;ComPtrvertexBuffer;D3D12_VERTEX_BUFFER_VIEWvertexBufferView
;ComPtrindexBuffer;D3D12_INDEX_BUFFER_VIEWindexBufferView;ComPtrdepthStencilBuffer
;ComPtrconstantBuffer;SceneConstantBufferconst康德樂大藥房官網(wǎng)買藥antBufferData;UINT8*pCbvDataBegin;// 同步對(duì)象
UINTframeIndex;HANDLEfenceEvent;ComPtrfence;UINT64fenceValue;floatcolor[3];boolisRAdd=true
;boolisGAdd=true;boolisBAdd=true;templateconstexprUINTCalcConstantBufferByteSize(){// Constant buffers must be a multiple of the minimum hardware
// allocation size (usua康德樂大藥房官網(wǎng)買藥lly 256 bytes). So round up to nearest // multiple of 256. We do this by adding 255 and then masking off
// the lower 2 bytes which store all bits < 256. // Example: Suppose byteSize = 300. // (300 + 255) & ~255
// 555 & ~255 // 0x022B & ~0x00ff // 0x022B & 0xff康德樂大藥房官網(wǎng)買藥00 // 0x0200 // 512 UINTbyteSize=sizeof(T);return(byteSize
+255)&~255;}std::stringHrToString(HRESULThr){chars_str[64]={};sprintf_s(s_str,"HRESULT of 0x%08X",static_cast
(hr));returnstd::string(s_str);}classHrException:publicstd::runtime_error{public:HrException(HRESULT
hr):std::康德樂大藥房官網(wǎng)買藥runtime_error(HrToString(hr)),m_hr(hr){}HRESULTError()const{returnm_hr;}private:constHRESULT
m_hr;};voidThrowIfFailed(HRESULThr){if(FAILED(hr)){throwHrException(hr);}}IDXGIAdapter1*GetSupportedAdapter
(ComPtr&dxgiFactory,constD3D_FEATURE_LEVELfeatureLevel){IDXGIAdapter1*adapter=nullptr;
for(std::uint32康德樂大藥房官網(wǎng)買藥_tadapterIndex=0U;;++adapterIndex){IDXGIAdapter1*currentAdapter=nullptr;if(DXGI_ERROR_NOT_FOUND
==dxgiFactory->EnumAdapters1(adapterIndex,¤tAdapter)){break;}constHRESULThres=D3D12CreateDevice
(currentAdapter,featureLevel,_uuidof(ID3D12Device),nullptr);if(SUCCEEDED(hres)){adapter=currentAdapter
;break;}康德樂大藥房官網(wǎng)買藥currentAdapter->Release();}returnadapter;}voidLoadPipeline(){#if defined(_DEBUG) {ComPtr
>debugController;if(SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))){debugController
->EnableDebugLayer();}}#endif ComPtrmDxgiFactory;ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS
(mDxgiFactory.G康德樂大藥房官網(wǎng)買藥etAddressOf())));D3D_FEATURE_LEVELfeatureLevels[]={D3D_FEATURE_LEVEL_12_1,D3D_FEATURE_LEVEL_12_0
,D3D_FEATURE_LEVEL_11_1,D3D_FEATURE_LEVEL_11_0,};IDXGIAdapter1*adapter=nullptr;for(std::uint32_ti=0U;
i<_countof(featureLevels);++i){adapter=GetSupportedAdapter(mDxgiFactory,featureLevels[i]);if(adapter!=
n康德樂大藥房官網(wǎng)買藥ullptr){break;}}if(adapter!=nullptr){D3D12CreateDevice(adapter,D3D_FEATURE_LEVEL_12_1,IID_PPV_ARGS(device
.GetAddressOf()));}D3D12_COMMAND_QUEUE_DESCqueueDesc={};queueDesc.Flags=D3D12_COMMAND_QUEUE_FLAG_NONE
;queueDesc.Type=D3D12_COMMAND_LIST_TYPE_DIRECT;ThrowIfFailed(device->CreateCommandQueue(&queue康德樂大藥房官網(wǎng)買藥Desc,IID_PPV_ARGS
(&commandQueue)));DXGI_SWAP_CHAIN_DESC1swapChainDesc={};swapChainDesc.BufferCount=FrameCount;swapChainDesc
.Width=width;swapChainDesc.Height=height;swapChainDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;swapChainDesc
.BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT;swapChainDesc.SwapEffect=DXGI_康德樂大藥房官網(wǎng)買藥SWAP_EFFECT_FLIP_DISCARD;swapChainDesc
.SampleDesc.Count=1;ComPtrswapChain1;ThrowIfFailed(mDxgiFactory->CreateSwapChainForHwnd
(commandQueue.Get(),hwnd,&swapChainDesc,nullptr,nullptr,&swapChain1));ThrowIfFailed(swapChain1.As(&swapChain
));frameIndex=swapChain->GetCurrentBackBufferIndex();{D3D12_DESCRIP康德樂大藥房官網(wǎng)買藥TOR_HEAP_DESCrtvHeapDesc={};rtvHeapDesc
.NumDescriptors=FrameCount;rtvHeapDesc.Type=D3D12_DESCRIPTOR_HEAP_TYPE_RTV;rtvHeapDesc.Flags=D3D12_DESCRIPTOR_HEAP_FLAG_NONE
;ThrowIfFailed(device->CreateDescriptorHeap(&rtvHeapDesc,IID_PPV_ARGS(&rtvHeap)));rtvDescriptorSize=device
->GetDescriptorHandleIncrementS康德樂大藥房官網(wǎng)買藥ize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);// D3D12_DESCRIPTOR_HEAP_DESCdsvHeapDesc
={};dsvHeapDesc.NumDescriptors=1;dsvHeapDesc.Type=D3D12_DESCRIPTOR_HEAP_TYPE_DSV;dsvHeapDesc.Flags=D3D12_DESCRIPTOR_HEAP_FLAG_NONE
;ThrowIfFailed(device->CreateDescriptorHeap(&dsvHeapDesc,IID_PPV_ARGS(&dsvHeap)));// D3D12_DES康德樂大藥房官網(wǎng)買藥CRIPTOR_HEAP_DESC
cbvHeapDesc={};cbvHeapDesc.NumDescriptors=1;cbvHeapDesc.Type=D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;cbvHeapDesc
.Flags=D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;ThrowIfFailed(device->CreateDescriptorHeap(&cbvHeapDesc
,IID_PPV_ARGS(&cbvHeap)));}CD3DX12_CPU_DESCRIPTOR_HANDLErtvHandle(rt康德樂大藥房官網(wǎng)買藥vHeap->GetCPUDescriptorHandleForHeapStart
());for(UINTn=0;nGetBuffer(n,IID_PPV_ARGS(&renderTargets[n])));
device->CreateRenderTargetView(renderTargets[n].Get(),nullptr,rtvHandle);rtvHandle.Offset(1,rtvDescriptorSize
);}ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,IID_PPV_康德樂大藥房官網(wǎng)買藥ARGS(&commandAllocator
)));}voidLoadAsset(){{D3D12_FEATURE_DATA_ROOT_SIGNATUREfeatureData={};featureData.HighestVersion=D3D_ROOT_SIGNATURE_VERSION_1_1
;if(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE,&featureData,sizeof(featureData))))
{featureData.HighestVersion=D3D_ROOT_SIGNATURE_V康德樂大藥房官網(wǎng)買藥ERSION_1_0;}CD3DX12_DESCRIPTOR_RANGE1ranges[1];CD3DX12_ROOT_PARAMETER1
rootParameters[1];ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV,1,0,0,D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC
);rootParameters[0].InitAsDescriptorTable(1,&ranges[0],D3D12_SHADER_VISIBILITY_VERTEX);D3D12_ROOT_SIGNATURE_FLAGS
rootSig康德樂大藥房官網(wǎng)買藥natureFlags=D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT|D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS
|D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS|D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS
|D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS;康德樂大藥房官網(wǎng)買藥CD3DX12_VERSIONED_ROOT_SIGNATURE_DESCrootSignatureDesc
;rootSignatureDesc.Init_1_1(_countof(rootParameters),rootParameters,0,nullptr,rootSignatureFlags);ComPtr
signature;ComPtrerror;ThrowIfFailed(D3DX12SerializeVersionedRootSignature(&rootSignatureDesc
,featureData.HighestVersion,&signature,&error));Th康德樂大藥房官網(wǎng)買藥rowIfFailed(device->CreateRootSignature(0,signature
->GetBufferPointer(),signature->GetBufferSize(),IID_PPV_ARGS(&rootSignature)));}{ComPtrvertexShader
;ComPtrpixelShader;#if defined(_DEBUG) UINTcompileFlags=D3DCOMPILE_DEBUG|D3DCOMPILE_SKIP_OPTIMIZATION
;#else UINTcompileFlags=0;#endif ThrowIfFailed(D3康德樂大藥房官網(wǎng)買藥DCompileFromFile(std::wstring(L"Assets/shaders.hlsl"
).c_str(),nullptr,nullptr,"VSMain","vs_5_0",compileFlags,0,&vertexShader,nullptr));ThrowIfFailed(D3DCompileFromFile
(std::wstring(L"Assets/shaders.hlsl").c_str(),nullptr,nullptr,"PSMain","ps_5_0",compileFlags,0,&pixelShader
,nullptr));D3D12_INPUT_ELE康德樂大藥房官網(wǎng)買藥MENT_DESCinputElementDescs[]={{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0
,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0},{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,12,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA
,0}};D3D12_GRAPHICS_PIPELINE_STATE_DESCpsoDesc={};psoDesc.InputLayout={inputElementDescs,康德樂大藥房官網(wǎng)買藥_countof(inputElementDescs
)};psoDesc.pRootSignature=rootSignature.Get();psoDesc.VS=CD3DX12_SHADER_BYTECODE(vertexShader.Get());
psoDesc.PS=CD3DX12_SHADER_BYTECODE(pixelShader.Get());psoDesc.RasterizerState=CD3DX12_RASTERIZER_DESC
(D3D12_DEFAULT);psoDesc.BlendState=CD3DX12_BLEND_DESC(D3D12_DEFAULT);pso康德樂大藥房官網(wǎng)買藥Desc.DepthStencilState=CD3DX12_DEPTH_STENCIL_DESC
(D3D12_DEFAULT);psoDesc.SampleMask=UINT_MAX;psoDesc.PrimitiveTopologyType=D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE
;psoDesc.NumRenderTargets=1;psoDesc.RTVFormats[0]=DXGI_FORMAT_R8G8B8A8_UNORM;psoDesc.SampleDesc.Count
=1;ThrowIfFailed(device->CreateGraphic康德樂大藥房官網(wǎng)買藥sPipelineState(&psoDesc,IID_PPV_ARGS(&pipelineState)));}ThrowIfFailed
(device->CreateCommandList(0,D3D12_COMMAND_LIST_TYPE_DIRECT,commandAllocator.Get(),pipelineState.Get(),
IID_PPV_ARGS(&commandList)));ThrowIfFailed(commandList->Close());{VertextriangleVertices[]={{{-0.5f,0.5f
,0.5f},{0.0f,1.0f,0.0f,1康德樂大藥房官網(wǎng)買藥.0f}},{{0.5f,-0.5f,0.5f},{0.0f,1.0f,0.0f,1.0f}},{{-0.5f,-0.5f,0.5f},{0.0f,1.0f
,0.0f,1.0f}},{{0.5f,0.5f,0.5f},{0.0f,1.0f,0.0f,1.0f}},{{-0.75f,0.75f,0.7f},{0.0f,0.0f,1.0f,1.0f}},{{0.0f
,0.0f,0.7f},{0.0f,0.0f,1.0f,1.0f}},{{-0.75f,0.0f,0.7f},{0.0f,0.0f,1.0f,1.0f}},{{0.0f,0.75f,0.7f},{0.0f
,0.0f,1.0f,1.0f}康德樂大藥房官網(wǎng)買藥}};DWORDtriangleIndexs[]{0,1,2,0,3,1};constUINTvertexBufferSize=sizeof(triangleVertices
);constUINTindexBufferSize=sizeof(triangleIndexs);CD3DX12_HEAP_PROPERTIESheapProperties=CD3DX12_HEAP_PROPERTIES
(D3D12_HEAP_TYPE_UPLOAD);CD3DX12_RESOURCE_DESCvertexResourceDes=CD3DX12_RESOURCE_DESC::Buffer(vertexBu康德樂大藥房官網(wǎng)買藥fferSize
);CD3DX12_RESOURCE_DESCindexResourceDes=CD3DX12_RESOURCE_DESC::Buffer(indexBufferSize);ThrowIfFailed(
device->CreateCommittedResource(&heapProperties,D3D12_HEAP_FLAG_NONE,&vertexResourceDes,D3D12_RESOURCE_STATE_GENERIC_READ
,nullptr,IID_PPV_ARGS(&vertexBuffer)));ThrowIfFailed(device->CreateCom康德樂大藥房官網(wǎng)買藥mittedResource(&heapProperties,
D3D12_HEAP_FLAG_NONE,&indexResourceDes,D3D12_RESOURCE_STATE_GENERIC_READ,nullptr,IID_PPV_ARGS(&indexBuffer
)));UINT8*pDataBegin;CD3DX12_RANGEreadRange(0,0);ThrowIfFailed(vertexBuffer->Map(0,&readRange,reinterpret_cast
(&pDataBegin)));memcpy(pDataBegin,triangleVertices,si康德樂大藥房官網(wǎng)買藥zeof(triangleVertices));vertexBuffer->Unmap
(0,nullptr);ThrowIfFailed(indexBuffer->Map(0,&readRange,reinterpret_cast(&pDataBegin)));memcpy
(pDataBegin,triangleIndexs,sizeof(triangleIndexs));indexBuffer->Unmap(0,nullptr);vertexBufferView.BufferLocation
=vertexBuffer->GetGPUVirtualAddress();vertexBufferV康德樂大藥房官網(wǎng)買藥iew.StrideInBytes=sizeof(Vertex);vertexBufferView.
SizeInBytes=vertexBufferSize;indexBufferView.BufferLocation=indexBuffer->GetGPUVirtualAddress();indexBufferView
.Format=DXGI_FORMAT_R32_UINT;indexBufferView.SizeInBytes=indexBufferSize;D3D12_DEPTH_STENCIL_VIEW_DESC
depthStencilDesc={};depthStencilDesc.康德樂大藥房官網(wǎng)買藥Format=DXGI_FORMAT_D32_FLOAT;depthStencilDesc.ViewDimension=D3D12_DSV_DIMENSION_TEXTURE2D
;depthStencilDesc.Flags=D3D12_DSV_FLAG_NONE;D3D12_CLEAR_VALUEdepthOptimizedClearValue={};depthOptimizedClearValue
.Format=DXGI_FORMAT_D32_FLOAT;depthOptimizedClearValue.DepthStencil.Depth=1.0f;depthOptimizedClear康德樂大藥房官網(wǎng)買藥Value
.DepthStencil.Stencil=0;CD3DX12_HEAP_PROPERTIESheapProperties2=CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT
);CD3DX12_RESOURCE_DESCtex2D=CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT,width,height,1,0,1,0,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);ThrowIfFailed(device->CreateCommittedResour康德樂大藥房官網(wǎng)買藥ce(&heapProperties2
,D3D12_HEAP_FLAG_NONE,&tex2D,D3D12_RESOURCE_STATE_DEPTH_WRITE,&depthOptimizedClearValue,IID_PPV_ARGS(
&depthStencilBuffer)));device->CreateDepthStencilView(depthStencilBuffer.Get(),&depthStencilDesc,dsvHeap
->GetCPUDescriptorHandleForHeapStart());constUINTconstantBufferSize=CalcCons康德樂大藥房官網(wǎng)買藥tantBufferByteSize
>();CD3DX12_RESOURCE_DESCconstantResourceDes=CD3DX12_RESOURCE_DESC::Buffer(constantBufferSize);ThrowIfFailed
(device->CreateCommittedResource(&heapProperties,D3D12_HEAP_FLAG_NONE,&constantResourceDes,D3D12_RESOURCE_STATE_GENERIC_READ
,nullptr,IID_PPV_ARGS(&constantBuffer)));D3D12_CON康德樂大藥房官網(wǎng)買藥STANT_BUFFER_VIEW_DESCcbvDesc={};cbvDesc.BufferLocation
=constantBuffer->GetGPUVirtualAddress();cbvDesc.SizeInBytes=constantBufferSize;device->CreateConstantBufferView
(&cbvDesc,cbvHeap->GetCPUDescriptorHandleForHeapStart());ThrowIfFailed(constantBuffer->Map(0,&readRange
,reinterpret_cast(&pCbvDataBegi康德樂大藥房官網(wǎng)買藥n)));memcpy(pCbvDataBegin,&constantBufferData,sizeof(constantBufferData
));}{ThrowIfFailed(device->CreateFence(0,D3D12_FENCE_FLAG_NONE,IID_PPV_ARGS(&fence)));fenceValue=1;fenceEvent
=CreateEvent(nullptr,FALSE,FALSE,nullptr);if(fenceEvent==nullptr){ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError
()));}}}v康德樂大藥房官網(wǎng)買藥oidPopulateCommandList(){ThrowIfFailed(commandAllocator->Reset());ThrowIfFailed(commandList->
Reset(commandAllocator.Get(),pipelineState.Get()));commandList->SetGraphicsRootSignature(rootSignature
.Get());ID3D12DescriptorHeap*ppHeaps[]={cbvHeap.Get()};commandList->SetDescriptorHeaps(_countof(ppHeaps
),康德樂大藥房官網(wǎng)買藥ppHeaps);commandList->SetGraphicsRootDescriptorTable(0,cbvHeap->GetGPUDescriptorHandleForHeapStart());
commandList->RSSetViewports(1,&viewport);commandList->RSSetScissorRects(1,&scissorRect);D3D12_RESOURCE_BARRIER
resBarrier=CD3DX12_RESOURCE_BARRIER::Transition(renderTargets[frameIndex].Get(),D3D12_RE康德樂大藥房官網(wǎng)買藥SOURCE_STATE_PRESENT
,D3D12_RESOURCE_STATE_RENDER_TARGET);commandList->ResourceBarrier(1,&resBarrier);CD3DX12_CPU_DESCRIPTOR_HANDLE
rtvHandle(rtvHeap->GetCPUDescriptorHandleForHeapStart(),frameIndex,rtvDescriptorSize);CD3DX12_CPU_DESCRIPTOR_HANDLE
dsvHandle(dsvHeap->GetCPUDescriptorHandleForHeapStart()康德樂大藥房官網(wǎng)買藥);commandList->OMSetRenderTargets(1,&rtvHandle
,FALSE,&dsvHandle);constfloatclearColor[]={color[0],color[1],color[2],1.0f};commandList->ClearRenderTargetView
(rtvHandle,clearColor,0,nullptr);commandList->ClearDepthStencilView(dsvHeap->GetCPUDescriptorHandleForHeapStart
(),D3D12_CLEAR_FLAG_DEPTH,1.0f,0,康德樂大藥房官網(wǎng)買藥0,nullptr);commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST
);commandList->IASetVertexBuffers(0,1,&vertexBufferView);commandList->IASetIndexBuffer(&indexBufferView
);commandList->DrawIndexedInstanced(6,1,0,0,0);commandList->DrawIndexedInstanced(6,1,0,4,0);resBarrier
=CD3DX12_RESO康德樂大藥房官網(wǎng)買藥URCE_BARRIER::Transition(renderTargets[frameIndex].Get(),D3D12_RESOURCE_STATE_RENDER_TARGET
,D3D12_RESOURCE_STATE_PRESENT);commandList->ResourceBarrier(1,&resBarrier);ThrowIfFailed(commandList->
Close());}voidWaitForPreviousFrame(){constUINT64tempFenceValue=fenceValue;ThrowIfFailed(commandQueue->
Signa康德樂大藥房官網(wǎng)買藥l(fence.Get(),tempFenceValue));fenceValue++;if(fence->GetCompletedValue()
(fence->SetEventOnCompletion(tempFenceValue,fenceEvent));WaitForSingleObject(fenceEvent,INFINITE);}frameIndex
=swapChain->GetCurrentBackBufferIndex();}voidOnUpdate(){if(color[0]<=1.0f&&isRAdd){color[0]+=0.001f;isRAdd
=true;}else{康德樂大藥房官網(wǎng)買藥color[0]-=0.002f;color[0]<=0?isRAdd=true:isRAdd=false;}if(color[1]<=1.0f&&isGAdd){color[1
]+=0.002f;isGAdd=true;}else{color[1]-=0.001f;color[1]<=0?isGAdd=true:isGAdd=false;}if(color[2]<=1.0f&&
isBAdd){color[2]+=0.001f;isBAdd=true;}else{color[2]-=0.001f;color[2]<=0?isBAdd=true:isBAdd=false;}const
floatt康德樂大藥房官網(wǎng)買藥ranslationSpeed=0.005f;constfloatoffsetBounds=1.25f;constantBufferData.offset.x+=translationSpeed
;if(constantBufferData.offset.x>offsetBounds){constantBufferData.offset.x=-offsetBounds;}memcpy(pCbvDataBegin
,&constantBufferData,sizeof(constantBufferData));}voidOnRender(){PopulateCommandList();ID3D12C康德樂大藥房官網(wǎng)買藥ommandList
*ppCommandLists[]={commandList.Get()};commandQueue->ExecuteCommandLists(_countof(ppCommandLists),ppCommandLists
);ThrowIfFailed(swapChain->Present(1,0));WaitForPreviousFrame();}voidOnDestroy(){WaitForPreviousFrame
();CloseHandle(fenceEvent);}LRESULTCALLBACKWindowProc(HWNDhWnd,UINTmessage,WPA康德樂大藥房官網(wǎng)買藥RAMwParam,LPARAMlParam)
{switch(message){caseWM_PAINT:OnUpdate();OnRender();return0;caseWM_DESTROY:PostQuitMessage(0);return0
;}returnDefWindowProc(hWnd,message,wParam,lParam);}intCALLBACKWinMain(_In_HINSTANCEhInstance,_In_opt_
HINSTANCEhPrevInstance,_In_LPSTRlpCmdLine,_In_intnShowCmd){WNDCLASSEXwindow康德樂大藥房官網(wǎng)買藥Class={0};windowClass.cbSize
=sizeof(WNDCLASSEX);windowClass.style=CS_HREDRAW|CS_VREDRAW;windowClass.lpfnWndProc=WindowProc;windowClass
.hInstance=hInstance;windowClass.hCursor=LoadCursor(NULL,IDC_ARROW);windowClass.lpszClassName=L"YSHRenderClass"
;RegisterClassEx(&windowClass);hwnd=CreateWindow(window康德樂大藥房官網(wǎng)買藥Class.lpszClassName,L"YSHRender",WS_OVERLAPPEDWINDOW
,CW_USEDEFAULT,CW_USEDEFAULT,width,height,nullptr,nullptr,hInstance,nullptr);LoadPipeline();LoadAsset
();ShowWindow(hwnd,SW_SHOW);MSGmsg={};while(msg.message!=WM_QUIT){if(PeekMessage(&msg,NULL,0,0,PM_REMOVE
)){TranslateMessage(&msg);DispatchMessage(&康德樂大藥房官網(wǎng)買藥msg);}}OnDestroy();return0;}運(yùn)行結(jié)果
00:00/ 00:00視頻信息鏡像畫面[X]DeviceId:web_88837357501626B3PlayerVersion:1.0.27
結(jié)尾哎,懶癌發(fā)作了,下次在寫了。下一篇