0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 04:42:18 +02:00

libobs-d3d12: device create

This commit is contained in:
hongqingwan 2024-08-29 14:55:48 +08:00
parent 26ad673832
commit 930d2db103
4 changed files with 68 additions and 66 deletions

View File

@ -23,6 +23,7 @@ add_subdirectory(libobs)
if(OS_WINDOWS) if(OS_WINDOWS)
add_subdirectory(libobs-d3d11) add_subdirectory(libobs-d3d11)
add_subdirectory(libobs-winrt) add_subdirectory(libobs-winrt)
add_subdirectory(libobs-d3d12)
endif() endif()
add_subdirectory(libobs-opengl) add_subdirectory(libobs-opengl)
add_subdirectory(plugins) add_subdirectory(plugins)

View File

@ -19,12 +19,8 @@ target_link_libraries(libobs-d3d12 PRIVATE OBS::libobs)
set_target_properties_obs( set_target_properties_obs(
libobs-d3d12 libobs-d3d12
PROPERTIES PROPERTIES FOLDER core
FOLDER VERSION 0
core SOVERSION ${OBS_VERSION_MAJOR}
VERSION COMPILE_WARNING_AS_ERROR FALSE
0 )
SOVERSION
${OBS_VERSION_MAJOR}
COMPILE_WARNING_AS_ERROR
FALSE)

View File

@ -39,7 +39,7 @@ struct UnsupportedHWError : HRError {
#pragma warning(disable : 4316) #pragma warning(disable : 4316)
#endif #endif
static inline void LogD3D11ErrorDetails(HRError error, gs_device_t *device) static inline void LogD3D12ErrorDetails(HRError error, gs_device_t *device)
{ {
if (error.hr == DXGI_ERROR_DEVICE_REMOVED) { if (error.hr == DXGI_ERROR_DEVICE_REMOVED) {
HRESULT DeviceRemovedReason = HRESULT DeviceRemovedReason =
@ -125,15 +125,21 @@ make_swap_desc(gs_device *device, DXGI_SWAP_CHAIN_DESC &desc,
return space; return space;
} }
void gs_device::InitFactory() {} void gs_device::InitFactory()
{
HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&factory));
if (FAILED(hr))
throw UnsupportedHWError("Failed to create DXGIFactory6", hr);
}
void gs_device::InitAdapter(uint32_t adapterIdx) {} void gs_device::InitAdapter(uint32_t adapterIdx)
{
const static D3D_FEATURE_LEVEL featureLevels[] = { HRESULT hr = factory->EnumAdapterByGpuPreference(
D3D_FEATURE_LEVEL_12_0, D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_2}; adapterIdx, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
IID_PPV_ARGS(&adapter));
#define NV12_CX 128 if (FAILED(hr))
#define NV12_CY 128 throw UnsupportedHWError("Failed to enumerate DXGIAdapter4", hr);
}
bool gs_device::HasBadNV12Output() bool gs_device::HasBadNV12Output()
try { try {
@ -147,13 +153,23 @@ try {
return false; return false;
} }
void gs_device::InitDevice(uint32_t adapterIdx) {} void gs_device::InitDevice(uint32_t adapterIdx) {
DXGI_ADAPTER_DESC3 desc;
HRESULT hr = adapter->GetDesc3(&desc);
if (hr != S_OK) {
throw UnsupportedHWError("Adapter4 Failed to GetDesc", hr);
}
// todo log desc info
hr = D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_12_2, IID_PPV_ARGS(&device));
if (FAILED(hr)) {
throw UnsupportedHWError("Failed to D3D12CreateDevice", hr);
}
}
void gs_device::UpdateZStencilState() {} void gs_device::UpdatePipelineState() {
void gs_device::UpdateRasterState() {} }
void gs_device::UpdateBlendState() {}
void gs_device::UpdateViewProjMatrix() void gs_device::UpdateViewProjMatrix()
{ {
@ -172,7 +188,7 @@ void gs_device::UpdateViewProjMatrix()
void gs_device::FlushOutputViews() {} void gs_device::FlushOutputViews() {}
gs_device::gs_device(uint32_t adapterIdx) gs_device::gs_device(uint32_t adapterIdx)
: curToplogy(D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED) : curToplogy(D3D_PRIMITIVE_TOPOLOGY_UNDEFINED)
{ {
matrix4_identity(&curProjMatrix); matrix4_identity(&curProjMatrix);
matrix4_identity(&curViewMatrix); matrix4_identity(&curViewMatrix);
@ -214,8 +230,9 @@ int device_create(gs_device_t **p_device, uint32_t adapter)
int errorcode = GS_SUCCESS; int errorcode = GS_SUCCESS;
try { try {
blog(LOG_INFO, "----------------------------------");
blog(LOG_INFO, "Initializing D3D12...");
device = new gs_device(adapter); device = new gs_device(adapter);
} catch (const UnsupportedHWError &error) { } catch (const UnsupportedHWError &error) {
blog(LOG_ERROR, "device_create (D3D12): %s (%08lX)", error.str, blog(LOG_ERROR, "device_create (D3D12): %s (%08lX)", error.str,
error.hr); error.hr);

View File

@ -521,7 +521,7 @@ struct gs_pixel_shader : gs_shader {
struct gs_swap_chain : gs_obj { struct gs_swap_chain : gs_obj {
HWND hwnd; HWND hwnd;
gs_init_data initData; gs_init_data initData;
DXGI_SWAP_CHAIN_DESC swapDesc = {}; DXGI_SWAP_CHAIN_DESC1 swapDesc = {};
gs_color_space space; gs_color_space space;
gs_texture_2d target; gs_texture_2d target;
@ -534,7 +534,6 @@ struct gs_swap_chain : gs_obj {
void Resize(uint32_t cx, uint32_t cy, gs_color_format format); void Resize(uint32_t cx, uint32_t cy, gs_color_format format);
void Init(); void Init();
// todo // todo
void Rebuild(ID3D12Device *dev);
gs_swap_chain(gs_device *device, const gs_init_data *data); gs_swap_chain(gs_device *device, const gs_init_data *data);
virtual ~gs_swap_chain(); virtual ~gs_swap_chain();
@ -555,25 +554,24 @@ struct BlendState {
inline BlendState() inline BlendState()
: blendEnabled(true), : blendEnabled(true),
srcFactorC(GS_BLEND_SRCALPHA), srcFactorC(GS_BLEND_SRCALPHA),
destFactorC(GS_BLEND_INVSRCALPHA), destFactorC(GS_BLEND_INVSRCALPHA),
srcFactorA(GS_BLEND_ONE), srcFactorA(GS_BLEND_ONE),
destFactorA(GS_BLEND_INVSRCALPHA), destFactorA(GS_BLEND_INVSRCALPHA),
op(GS_BLEND_OP_ADD), op(GS_BLEND_OP_ADD),
redEnabled(true), redEnabled(true),
greenEnabled(true), greenEnabled(true),
blueEnabled(true), blueEnabled(true),
alphaEnabled(true) alphaEnabled(true)
{ {
} }
inline BlendState(const BlendState &state) inline BlendState(const BlendState& state)
{ {
memcpy(this, &state, sizeof(BlendState)); memcpy(this, &state, sizeof(BlendState));
} }
}; };
struct SavedBlendState : BlendState {};
struct StencilSide { struct StencilSide {
gs_depth_test test; gs_depth_test test;
@ -583,9 +581,9 @@ struct StencilSide {
inline StencilSide() inline StencilSide()
: test(GS_ALWAYS), : test(GS_ALWAYS),
fail(GS_KEEP), fail(GS_KEEP),
zfail(GS_KEEP), zfail(GS_KEEP),
zpass(GS_KEEP) zpass(GS_KEEP)
{ {
} }
}; };
@ -602,35 +600,31 @@ struct ZStencilState {
inline ZStencilState() inline ZStencilState()
: depthEnabled(true), : depthEnabled(true),
depthWriteEnabled(true), depthWriteEnabled(true),
depthFunc(GS_LESS), depthFunc(GS_LESS),
stencilEnabled(false), stencilEnabled(false),
stencilWriteEnabled(true) stencilWriteEnabled(true)
{ {
} }
inline ZStencilState(const ZStencilState &state) inline ZStencilState(const ZStencilState& state)
{ {
memcpy(this, &state, sizeof(ZStencilState)); memcpy(this, &state, sizeof(ZStencilState));
} }
}; };
struct SavedZStencilState : ZStencilState {};
struct RasterState { struct RasterState {
gs_cull_mode cullMode; gs_cull_mode cullMode;
bool scissorEnabled; bool scissorEnabled;
inline RasterState() : cullMode(GS_BACK), scissorEnabled(false) {} inline RasterState() : cullMode(GS_BACK), scissorEnabled(false) {}
inline RasterState(const RasterState &state) inline RasterState(const RasterState& state)
{ {
memcpy(this, &state, sizeof(RasterState)); memcpy(this, &state, sizeof(RasterState));
} }
}; };
struct SavedRasterState : RasterState {};
struct mat4float { struct mat4float {
float mat[16]; float mat[16];
}; };
@ -649,10 +643,16 @@ struct gs_monitor_color_info {
} }
}; };
struct SavedPipelineState {
ComPtr<ID3D12PipelineState> pipeline_state;
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc;
};
struct gs_device { struct gs_device {
ComPtr<IDXGIFactory6> factory; ComPtr<IDXGIFactory6> factory;
ComPtr<IDXGIAdapter4> adapter; ComPtr<IDXGIAdapter4> adapter;
ComPtr<ID3D12Device1> device; ComPtr<ID3D12Device1> device;
ComPtr<ID3D12CommandQueue> commandQueue;
ComPtr<ID3D12GraphicsCommandList2> context; ComPtr<ID3D12GraphicsCommandList2> context;
uint32_t adpIdx = 0; uint32_t adpIdx = 0;
bool nv12Supported = false; bool nv12Supported = false;
@ -676,19 +676,16 @@ struct gs_device {
gs_vertex_buffer *lastVertexBuffer = nullptr; gs_vertex_buffer *lastVertexBuffer = nullptr;
gs_vertex_shader *lastVertexShader = nullptr; gs_vertex_shader *lastVertexShader = nullptr;
// pipeline state
bool zstencilStateChanged = true; bool zstencilStateChanged = true;
bool rasterStateChanged = true; bool rasterStateChanged = true;
bool blendStateChanged = true; bool blendStateChanged = true;
ZStencilState zstencilState; ZStencilState zstencilState;
RasterState rasterState; RasterState rasterState;
BlendState blendState; BlendState blendState;
std::vector<SavedZStencilState> zstencilStates; std::vector<SavedPipelineState> SavedPipelineState;
std::vector<SavedRasterState> rasterStates;
std::vector<SavedBlendState> blendStates;
// ID3D12DepthStencilState *curDepthStencilState = nullptr;
// ID3D12RasterizerState *curRasterState = nullptr;
// ID3D12BlendState *curBlendState = nullptr;
D3D_PRIMITIVE_TOPOLOGY curToplogy; D3D_PRIMITIVE_TOPOLOGY curToplogy;
ComPtr<ID3D12PipelineState> currentPipelineState;
gs_rect viewport; gs_rect viewport;
@ -707,19 +704,10 @@ struct gs_device {
void InitAdapter(uint32_t adapterIdx); void InitAdapter(uint32_t adapterIdx);
void InitDevice(uint32_t adapterIdx); void InitDevice(uint32_t adapterIdx);
// ID3D11DepthStencilState *AddZStencilState(); void UpdatePipelineState();
// ID3D11RasterizerState *AddRasterState();
// ID3D11BlendState *AddBlendState();
void UpdateZStencilState();
void UpdateRasterState();
void UpdateBlendState();
void LoadVertexBufferData(); void LoadVertexBufferData();
// void CopyTex(ID3D11Texture2D *dst, uint32_t dst_x, uint32_t dst_y,
// gs_texture_t *src, uint32_t src_x, uint32_t src_y,
// uint32_t src_w, uint32_t src_h);
void UpdateViewProjMatrix(); void UpdateViewProjMatrix();
void FlushOutputViews(); void FlushOutputViews();