From 951c6892b0c3ea35eb76e9529c099f3c6b7bb99a Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 3 Nov 2016 07:37:05 -0700 Subject: [PATCH] libobs-d3d11: Use linked list for all objects (for rebuilding) --- libobs-d3d11/d3d11-duplicator.cpp | 4 +- libobs-d3d11/d3d11-indexbuffer.cpp | 2 +- libobs-d3d11/d3d11-samplerstate.cpp | 2 +- libobs-d3d11/d3d11-shader.cpp | 4 +- libobs-d3d11/d3d11-stagesurf.cpp | 2 +- libobs-d3d11/d3d11-subsystem.cpp | 22 +++++- libobs-d3d11/d3d11-subsystem.hpp | 99 +++++++++++++++++---------- libobs-d3d11/d3d11-texture2d.cpp | 7 +- libobs-d3d11/d3d11-vertexbuffer.cpp | 2 +- libobs-d3d11/d3d11-zstencilbuffer.cpp | 2 +- 10 files changed, 99 insertions(+), 47 deletions(-) diff --git a/libobs-d3d11/d3d11-duplicator.cpp b/libobs-d3d11/d3d11-duplicator.cpp index 62c85dedf..5970b4ae6 100644 --- a/libobs-d3d11/d3d11-duplicator.cpp +++ b/libobs-d3d11/d3d11-duplicator.cpp @@ -53,7 +53,9 @@ void gs_duplicator::Start() } gs_duplicator::gs_duplicator(gs_device_t *device_, int monitor_idx) - : texture(nullptr), device(device_), idx(monitor_idx) + : gs_obj (device_, gs_type::gs_duplicator), + texture (nullptr), + idx (monitor_idx) { Start(); } diff --git a/libobs-d3d11/d3d11-indexbuffer.cpp b/libobs-d3d11/d3d11-indexbuffer.cpp index f5d4c754a..d95139acd 100644 --- a/libobs-d3d11/d3d11-indexbuffer.cpp +++ b/libobs-d3d11/d3d11-indexbuffer.cpp @@ -37,7 +37,7 @@ void gs_index_buffer::InitBuffer() gs_index_buffer::gs_index_buffer(gs_device_t *device, enum gs_index_type type, void *indices, size_t num, uint32_t flags) - : device (device), + : gs_obj (device, gs_type::gs_index_buffer), dynamic ((flags & GS_DYNAMIC) != 0), type (type), num (num), diff --git a/libobs-d3d11/d3d11-samplerstate.cpp b/libobs-d3d11/d3d11-samplerstate.cpp index 989ea955b..67b400520 100644 --- a/libobs-d3d11/d3d11-samplerstate.cpp +++ b/libobs-d3d11/d3d11-samplerstate.cpp @@ -62,7 +62,7 @@ static inline D3D11_FILTER ConvertGSFilter( gs_sample_filter filter) gs_sampler_state::gs_sampler_state(gs_device_t *device, const gs_sampler_info *info) - : device (device), + : gs_obj (device, gs_type::gs_sampler_state), info (*info) { HRESULT hr; diff --git a/libobs-d3d11/d3d11-shader.cpp b/libobs-d3d11/d3d11-shader.cpp index 1aade440d..4e1d15365 100644 --- a/libobs-d3d11/d3d11-shader.cpp +++ b/libobs-d3d11/d3d11-shader.cpp @@ -40,7 +40,7 @@ void gs_vertex_shader::GetBuffersExpected( gs_vertex_shader::gs_vertex_shader(gs_device_t *device, const char *file, const char *shaderString) - : gs_shader (device, GS_SHADER_VERTEX), + : gs_shader (device, gs_type::gs_vertex_shader, GS_SHADER_VERTEX), hasNormals (false), hasColors (false), hasTangents (false), @@ -80,7 +80,7 @@ gs_vertex_shader::gs_vertex_shader(gs_device_t *device, const char *file, gs_pixel_shader::gs_pixel_shader(gs_device_t *device, const char *file, const char *shaderString) - : gs_shader(device, GS_SHADER_PIXEL) + : gs_shader(device, gs_type::gs_pixel_shader, GS_SHADER_PIXEL) { ShaderProcessor processor(device); ComPtr shaderBlob; diff --git a/libobs-d3d11/d3d11-stagesurf.cpp b/libobs-d3d11/d3d11-stagesurf.cpp index aae20a852..c08b4620f 100644 --- a/libobs-d3d11/d3d11-stagesurf.cpp +++ b/libobs-d3d11/d3d11-stagesurf.cpp @@ -19,7 +19,7 @@ gs_stage_surface::gs_stage_surface(gs_device_t *device, uint32_t width, uint32_t height, gs_color_format colorFormat) - : device (device), + : gs_obj (device, gs_type::gs_stage_surface), width (width), height (height), format (colorFormat), diff --git a/libobs-d3d11/d3d11-subsystem.cpp b/libobs-d3d11/d3d11-subsystem.cpp index c3821ae27..a665fcffc 100644 --- a/libobs-d3d11/d3d11-subsystem.cpp +++ b/libobs-d3d11/d3d11-subsystem.cpp @@ -46,6 +46,26 @@ static inline void LogD3D11ErrorDetails(HRError error, gs_device_t *device) static const IID dxgiFactory2 = {0x50c83a1c, 0xe072, 0x4c48, {0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0}}; + +gs_obj::gs_obj(gs_device_t *device_, gs_type type) : + device (device_), + obj_type (type) +{ + prev_next = &device->first_obj; + next = device->first_obj; + device->first_obj = this; + if (next) + next->prev_next = &next; +} + +gs_obj::~gs_obj() +{ + if (prev_next) + *prev_next = next; + if (next) + next->prev_next = prev_next; +} + static inline void make_swap_desc(DXGI_SWAP_CHAIN_DESC &desc, const gs_init_data *data) { @@ -133,7 +153,7 @@ void gs_swap_chain::Init() } gs_swap_chain::gs_swap_chain(gs_device *device, const gs_init_data *data) - : device (device), + : gs_obj (device, gs_type::gs_swap_chain), numBuffers (data->num_backbuffers), hwnd ((HWND)data->window.hwnd), initData (*data) diff --git a/libobs-d3d11/d3d11-subsystem.hpp b/libobs-d3d11/d3d11-subsystem.hpp index 48e34157b..5ec33285b 100644 --- a/libobs-d3d11/d3d11-subsystem.hpp +++ b/libobs-d3d11/d3d11-subsystem.hpp @@ -204,14 +204,42 @@ struct VBDataPtr { inline ~VBDataPtr() {gs_vbdata_destroy(data);} }; -struct gs_vertex_buffer { +enum class gs_type { + gs_vertex_buffer, + gs_index_buffer, + gs_texture_2d, + gs_zstencil_buffer, + gs_stage_surface, + gs_sampler_state, + gs_vertex_shader, + gs_pixel_shader, + gs_duplicator, + gs_swap_chain, +}; + +struct gs_obj { + gs_device_t *device; + gs_type obj_type; + gs_obj *next; + gs_obj **prev_next; + + inline gs_obj() : + device(nullptr), + next(nullptr), + prev_next(nullptr) + {} + + gs_obj(gs_device_t *device, gs_type type); + virtual ~gs_obj(); +}; + +struct gs_vertex_buffer : gs_obj { ComPtr vertexBuffer; ComPtr normalBuffer; ComPtr colorBuffer; ComPtr tangentBuffer; vector> uvBuffers; - gs_device_t *device; bool dynamic; VBDataPtr vbd; size_t numVerts; @@ -242,9 +270,8 @@ struct DataPtr { inline ~DataPtr() {bfree(data);} }; -struct gs_index_buffer { +struct gs_index_buffer : gs_obj { ComPtr indexBuffer; - gs_device_t *device; bool dynamic; gs_index_type type; size_t indexSize; @@ -260,27 +287,38 @@ struct gs_index_buffer { void *indices, size_t num, uint32_t flags); }; -struct gs_texture { +struct gs_texture : gs_obj { gs_texture_type type; - gs_device *device; uint32_t levels; gs_color_format format; ComPtr shaderRes; D3D11_SHADER_RESOURCE_VIEW_DESC resourceDesc = {}; - inline gs_texture() {} - - inline gs_texture(gs_device *device, gs_texture_type type, - uint32_t levels, gs_color_format format) + inline gs_texture(gs_texture_type type, uint32_t levels, + gs_color_format format) : type (type), - device (device), levels (levels), format (format) { } - virtual ~gs_texture() {} + inline gs_texture(gs_device *device, gs_type obj_type, + gs_texture_type type) + : gs_obj (device, obj_type), + type (type) + { + } + + inline gs_texture(gs_device *device, gs_type obj_type, + gs_texture_type type, + uint32_t levels, gs_color_format format) + : gs_obj (device, obj_type), + type (type), + levels (levels), + format (format) + { + } }; struct gs_texture_2d : gs_texture { @@ -308,7 +346,7 @@ struct gs_texture_2d : gs_texture { void BackupTexture(const uint8_t **data); inline gs_texture_2d() - : gs_texture (NULL, GS_TEXTURE_2D, 0, GS_UNKNOWN) + : gs_texture (GS_TEXTURE_2D, 0, GS_UNKNOWN) { } @@ -320,11 +358,10 @@ struct gs_texture_2d : gs_texture { gs_texture_2d(gs_device_t *device, uint32_t handle); }; -struct gs_zstencil_buffer { +struct gs_zstencil_buffer : gs_obj { ComPtr texture; ComPtr view; - gs_device *device; uint32_t width, height; gs_zstencil_format format; DXGI_FORMAT dxgiFormat; @@ -335,8 +372,7 @@ struct gs_zstencil_buffer { void InitBuffer(); inline gs_zstencil_buffer() - : device (NULL), - width (0), + : width (0), height (0), dxgiFormat (DXGI_FORMAT_UNKNOWN) { @@ -346,11 +382,10 @@ struct gs_zstencil_buffer { gs_zstencil_format format); }; -struct gs_stage_surface { +struct gs_stage_surface : gs_obj { ComPtr texture; D3D11_TEXTURE2D_DESC td = {}; - gs_device *device; uint32_t width, height; gs_color_format format; DXGI_FORMAT dxgiFormat; @@ -359,9 +394,8 @@ struct gs_stage_surface { gs_color_format colorFormat); }; -struct gs_sampler_state { +struct gs_sampler_state : gs_obj { ComPtr state; - gs_device_t *device; D3D11_SAMPLER_DESC sd = {}; gs_sampler_info info; @@ -397,8 +431,7 @@ struct ShaderError { } }; -struct gs_shader { - gs_device_t *device; +struct gs_shader : gs_obj { gs_shader_type type; vector params; ComPtr constants; @@ -415,8 +448,9 @@ struct gs_shader { void Compile(const char *shaderStr, const char *file, const char *target, ID3D10Blob **shader); - inline gs_shader(gs_device_t *device, gs_shader_type type) - : device (device), + inline gs_shader(gs_device_t *device, gs_type obj_type, + gs_shader_type type) + : gs_obj (device, obj_type), type (type), constantSize (0) { @@ -466,10 +500,9 @@ struct gs_vertex_shader : gs_shader { const char *shaderString); }; -struct gs_duplicator { +struct gs_duplicator : gs_obj { ComPtr duplicator; gs_texture_2d *texture; - gs_device_t *device; int idx; void Start(); @@ -495,8 +528,7 @@ struct gs_pixel_shader : gs_shader { const char *shaderString); }; -struct gs_swap_chain { - gs_device *device; +struct gs_swap_chain : gs_obj { uint32_t numBuffers; HWND hwnd; gs_init_data initData; @@ -511,13 +543,6 @@ struct gs_swap_chain { void Resize(uint32_t cx, uint32_t cy); void Init(); - inline gs_swap_chain() - : device (NULL), - numBuffers (0), - hwnd (NULL) - { - } - gs_swap_chain(gs_device *device, const gs_init_data *data); }; @@ -688,6 +713,8 @@ struct gs_device { matrix4 curViewMatrix; matrix4 curViewProjMatrix; + gs_obj *first_obj = nullptr; + void InitCompiler(); void InitFactory(uint32_t adapterIdx); void InitDevice(uint32_t adapterIdx); diff --git a/libobs-d3d11/d3d11-texture2d.cpp b/libobs-d3d11/d3d11-texture2d.cpp index b4c35613d..985546688 100644 --- a/libobs-d3d11/d3d11-texture2d.cpp +++ b/libobs-d3d11/d3d11-texture2d.cpp @@ -166,7 +166,8 @@ gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width, uint32_t height, gs_color_format colorFormat, uint32_t levels, const uint8_t **data, uint32_t flags, gs_texture_type type, bool gdiCompatible, bool shared) - : gs_texture (device, type, levels, colorFormat), + : gs_texture (device, gs_type::gs_texture_2d, type, levels, + colorFormat), width (width), height (height), dxgiFormat (ConvertGSTextureFormat(format)), @@ -184,7 +185,9 @@ gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t width, } gs_texture_2d::gs_texture_2d(gs_device_t *device, uint32_t handle) - : isShared (true), + : gs_texture (device, gs_type::gs_texture_2d, + GS_TEXTURE_2D), + isShared (true), sharedHandle (handle) { HRESULT hr; diff --git a/libobs-d3d11/d3d11-vertexbuffer.cpp b/libobs-d3d11/d3d11-vertexbuffer.cpp index dfdcac683..580aad64f 100644 --- a/libobs-d3d11/d3d11-vertexbuffer.cpp +++ b/libobs-d3d11/d3d11-vertexbuffer.cpp @@ -129,7 +129,7 @@ void gs_vertex_buffer::BuildBuffers() gs_vertex_buffer::gs_vertex_buffer(gs_device_t *device, struct gs_vb_data *data, uint32_t flags) - : device (device), + : gs_obj (device, gs_type::gs_vertex_buffer), dynamic ((flags & GS_DYNAMIC) != 0), vbd (data), numVerts (data->num) diff --git a/libobs-d3d11/d3d11-zstencilbuffer.cpp b/libobs-d3d11/d3d11-zstencilbuffer.cpp index 48599a414..8c077d167 100644 --- a/libobs-d3d11/d3d11-zstencilbuffer.cpp +++ b/libobs-d3d11/d3d11-zstencilbuffer.cpp @@ -48,7 +48,7 @@ void gs_zstencil_buffer::InitBuffer() gs_zstencil_buffer::gs_zstencil_buffer(gs_device_t *device, uint32_t width, uint32_t height, gs_zstencil_format format) - : device (device), + : gs_obj (device, gs_type::gs_zstencil_buffer), width (width), height (height), format (format),