2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Copyright 2014 Dolphin Emulator Project
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Licensed under GPLv2+
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Refer to the license.txt file included.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "VideoBackends/D3D/BoundingBox.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "VideoCommon/VideoConfig.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								namespace DX11
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								static ID3D11Buffer* s_bbox_buffer;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								static ID3D11Buffer* s_bbox_staging_buffer;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								static ID3D11UnorderedAccessView*  s_bbox_uav;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								ID3D11UnorderedAccessView* &BBox::GetUAV()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return s_bbox_uav;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void BBox::Init()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (g_ActiveConfig.backend_info.bSupportsBBox)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									{
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										// Create 2 buffers here.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 23:54:34 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										// First for unordered access on default pool.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										auto desc = CD3D11_BUFFER_DESC(4 * sizeof(s32), D3D11_BIND_UNORDERED_ACCESS, D3D11_USAGE_DEFAULT, 0, 0, 4);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										int initial_values[4] = { 0, 0, 0, 0 };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										D3D11_SUBRESOURCE_DATA data;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										data.pSysMem = initial_values;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										data.SysMemPitch = 4 * sizeof(s32);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										data.SysMemSlicePitch = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										HRESULT hr;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										hr = D3D::device->CreateBuffer(&desc, &data, &s_bbox_buffer);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										CHECK(SUCCEEDED(hr), "Create BoundingBox Buffer.");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 23:54:34 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										D3D::SetDebugObjectName(s_bbox_buffer, "BoundingBox Buffer");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										// Second to use as a staging buffer.
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										desc.Usage = D3D11_USAGE_STAGING;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										desc.BindFlags = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										hr = D3D::device->CreateBuffer(&desc, nullptr, &s_bbox_staging_buffer);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										CHECK(SUCCEEDED(hr), "Create BoundingBox Staging Buffer.");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 23:54:34 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										D3D::SetDebugObjectName(s_bbox_staging_buffer, "BoundingBox Staging Buffer");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-07 20:28:27 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										// UAV is required to allow concurrent access.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										D3D11_UNORDERED_ACCESS_VIEW_DESC UAVdesc = {};
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										UAVdesc.Format = DXGI_FORMAT_R32_SINT;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										UAVdesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										UAVdesc.Buffer.FirstElement = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										UAVdesc.Buffer.Flags = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										UAVdesc.Buffer.NumElements = 4;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										hr = D3D::device->CreateUnorderedAccessView(s_bbox_buffer, &UAVdesc, &s_bbox_uav);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										CHECK(SUCCEEDED(hr), "Create BoundingBox UAV.");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 23:54:34 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										D3D::SetDebugObjectName(s_bbox_uav, "BoundingBox UAV");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void BBox::Shutdown()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-06 10:46:15 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									SAFE_RELEASE(s_bbox_buffer);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									SAFE_RELEASE(s_bbox_staging_buffer);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									SAFE_RELEASE(s_bbox_uav);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void BBox::Set(int index, int value)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									D3D11_BOX box{ index * sizeof(s32), 0, 0, (index + 1) * sizeof(s32), 1, 1 };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									D3D::context->UpdateSubresource(s_bbox_buffer, 0, &box, &value, 0, 0);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								int BBox::Get(int index)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									int data = 0;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									D3D::context->CopyResource(s_bbox_staging_buffer, s_bbox_buffer);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									D3D11_MAPPED_SUBRESOURCE map;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									HRESULT hr = D3D::context->Map(s_bbox_staging_buffer, 0, D3D11_MAP_READ, 0, &map);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (SUCCEEDED(hr))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										data = ((s32*)map.pData)[index];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-05 18:51:23 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									D3D::context->Unmap(s_bbox_staging_buffer, 0);
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-04 23:01:20 -03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return data;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 |