Beispiel #1
0
void InitD3D(HWND winHandle)
{
	DXGI_SWAP_CHAIN_DESC swapDesc;
	ZeroMemory(&swapDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

	swapDesc.BufferCount = 1;
	swapDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapDesc.OutputWindow = winHandle;
	swapDesc.SampleDesc.Count = 1;
	swapDesc.SampleDesc.Quality = 0;
	swapDesc.Windowed = true;

	UINT flags = 0;

#ifdef _DEBUG
	flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif // _DEBUG

	CHECKDX(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, nullptr, 0, D3D11_SDK_VERSION, &swapDesc, &swapChain, &device, nullptr, &context));

	ID3D11Texture2D* pBackBuffer;
	swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);

	backBuffer = nullptr;
	CHECKDX(device->CreateRenderTargetView(pBackBuffer, nullptr, &backBuffer));
	pBackBuffer->Release();

	D3D11_TEXTURE2D_DESC depthStencilDesc;

	depthStencilDesc.Width = WINDOW_WIDTH;
	depthStencilDesc.Height = WINDOW_HEIGHT;
	depthStencilDesc.MipLevels = 1;
	depthStencilDesc.ArraySize = 1;
	depthStencilDesc.Format = DXGI_FORMAT_R32_TYPELESS;
	depthStencilDesc.SampleDesc.Count = 1;
	depthStencilDesc.SampleDesc.Quality = 0;
	depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
	depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
	depthStencilDesc.CPUAccessFlags = 0;
	depthStencilDesc.MiscFlags = 0;

	D3D11_DEPTH_STENCIL_VIEW_DESC dsv_desc;
	dsv_desc.Flags = 0;
	dsv_desc.Format = DXGI_FORMAT_D32_FLOAT;
	dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
	dsv_desc.Texture2D.MipSlice = 0;

	D3D11_SHADER_RESOURCE_VIEW_DESC sr_desc;
	sr_desc.Format = DXGI_FORMAT_R32_FLOAT;
	sr_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	sr_desc.Texture2D.MostDetailedMip = 0;
	sr_desc.Texture2D.MipLevels = -1;

	ID3D11Texture2D* DSB;
	CHECKDX(device->CreateTexture2D(&depthStencilDesc, NULL, &DSB));
	CHECKDX(device->CreateDepthStencilView(DSB, &dsv_desc, &depthStencilView));
	CHECKDX(device->CreateShaderResourceView(DSB, &sr_desc, &depthBufferView));
	DSB->Release();

	//Create renderTagets for GBuffer
	colorRT.CreateRenderTarget(WINDOW_WIDTH, WINDOW_HEIGHT, DXGI_FORMAT_R8G8B8A8_UNORM);
	normalRT.CreateRenderTarget(WINDOW_WIDTH, WINDOW_HEIGHT, DXGI_FORMAT_R16G16B16A16_FLOAT);
	positionRT.CreateRenderTarget(WINDOW_WIDTH, WINDOW_HEIGHT, DXGI_FORMAT_R16G16B16A16_FLOAT);

	
}
Beispiel #2
0
void CreateVoxelBuffers()
{
	D3D11_BUFFER_DESC cbDesc;
	cbDesc.ByteWidth = voxelElementCount * sizeof(uint32);
	cbDesc.Usage = D3D11_USAGE_DEFAULT;
	cbDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE;
	cbDesc.CPUAccessFlags = 0;
	cbDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
	cbDesc.StructureByteStride = 0;

	if (voxelStructure)
		voxelStructure->Release();

	CHECKDX(device->CreateBuffer(&cbDesc, nullptr, &voxelStructure));


	D3D11_BUFFER_DESC voxelClearDesc;
	voxelClearDesc.ByteWidth = voxelElementCount * sizeof(uint32);
	voxelClearDesc.Usage = D3D11_USAGE_IMMUTABLE;
	voxelClearDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	voxelClearDesc.CPUAccessFlags = 0;
	voxelClearDesc.MiscFlags = 0;
	voxelClearDesc.StructureByteStride = 0;

	std::vector<uint32> initVector(voxelElementCount, 0);
	D3D11_SUBRESOURCE_DATA voxinitdata;
	voxinitdata.pSysMem = &initVector[0];
	voxinitdata.SysMemPitch = voxelElementCount * sizeof(uint32);
	voxinitdata.SysMemSlicePitch = 0;
	
	if (voxelClearBuffer)
		voxelClearBuffer->Release();

	CHECKDX(device->CreateBuffer(&voxelClearDesc, &voxinitdata, &voxelClearBuffer));

	D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc;
	uavDesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
	uavDesc.Buffer.FirstElement = 0;
	uavDesc.Buffer.NumElements = voxelElementCount;
	uavDesc.Format = DXGI_FORMAT_R32_TYPELESS;
	uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;

	if (voxelUAV)
		voxelUAV->Release();

	CHECKDX(device->CreateUnorderedAccessView(voxelStructure, &uavDesc, &voxelUAV));

	D3D11_SHADER_RESOURCE_VIEW_DESC voxelstructsdesc;
	voxelstructsdesc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW;
	voxelstructsdesc.BufferEx.FirstElement = 0;
	voxelstructsdesc.BufferEx.NumElements = voxelElementCount;
	voxelstructsdesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
	voxelstructsdesc.Format = DXGI_FORMAT_R32_TYPELESS;

	if (voxelSRV)
		voxelSRV->Release();

	CHECKDX(device->CreateShaderResourceView(voxelStructure, &voxelstructsdesc, &voxelSRV));

	D3D11_BUFFER_DESC voxelStagingDesc;
	voxelStagingDesc.ByteWidth = voxelElementCount * sizeof(uint32);
	voxelStagingDesc.Usage = D3D11_USAGE_STAGING;
	voxelStagingDesc.BindFlags = 0;
	voxelStagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
	voxelStagingDesc.MiscFlags = 0;
	voxelStagingDesc.StructureByteStride = 0;

	if (voxelStagingStructure)
		voxelStagingStructure->Release();

	CHECKDX(device->CreateBuffer(&voxelStagingDesc, nullptr, &voxelStagingStructure));

	voxelVoidRT.CreateRenderTarget(numVoxels, numVoxels, DXGI_FORMAT_R8G8_UNORM);
}