Skip to content

Commit

Permalink
gpu instance
Browse files Browse the repository at this point in the history
  • Loading branch information
recp committed Oct 2, 2023
1 parent 44e1194 commit 1a78f72
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 21 deletions.
3 changes: 2 additions & 1 deletion include/gpu/api/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ extern "C" {
#include "../gpu.h"

struct GPUApi;
struct GPUInstance;

typedef struct GPUApiDevice {
GPUDevice* (*createSystemDefaultDevice)(struct GPUApi*);
GPUDevice* (*createSystemDefaultDevice)(struct GPUApi*, GPUInstance * __restrict inst);
} GPUApiDevice;

#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions include/gpu/api/gpudef.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ extern "C" {
#include "frame.h"
#include "descriptor.h"
#include "sampler.h"
#include "instance.h"

typedef struct GPUApi {
GPUBackend backend;
Expand All @@ -54,6 +55,7 @@ typedef struct GPUApi {
GPUApiFrame frame;
GPUApiDescriptor descriptor;
GPUApiSampler sampler;
GPUApiInstance instance;
void *reserved;
} GPUApi;

Expand Down
35 changes: 35 additions & 0 deletions include/gpu/api/instance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 Recep Aslantas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef gpu_gpudef_instance_h
#define gpu_gpudef_instance_h
#ifdef __cplusplus
extern "C" {
#endif

#include "../common.h"
#include "../gpu.h"

struct GPUApi;

typedef struct GPUApiInstance {
GPUInstance *(*createInstance)(struct GPUApi *__restrict api, void * __restrict unused);
} GPUApiInstance;

#ifdef __cplusplus
}
#endif
#endif /* gpu_gpudef_instance_h */
3 changes: 2 additions & 1 deletion include/gpu/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ extern "C" {
#endif

#include "common.h"
#include "instance.h"

typedef struct GPUDevice {
void *priv;
} GPUDevice;

GPU_EXPORT
GPUDevice*
GPUCreateSystemDefaultDevice(void);
GPUCreateSystemDefaultDevice(GPUInstance *inst);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions include/gpu/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern "C" {
#include "feature.h"
#include "descriptor.h"
#include "sampler.h"
#include "instance.h"

typedef enum GPUBackend {
GPU_BACKEND_NULL = 0,
Expand Down
36 changes: 36 additions & 0 deletions include/gpu/instance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 Recep Aslantas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef gpu_instance_h
#define gpu_instance_h
#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"

typedef struct GPUInstance {
void *_priv;
} GPUInstance;

GPU_EXPORT
GPUInstance*
GPUCreateInstance(void * __restrict unused);

#ifdef __cplusplus
}
#endif
#endif /* gpu_instance_h */
4 changes: 2 additions & 2 deletions src/api/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

GPU_EXPORT
GPUDevice*
GPUCreateSystemDefaultDevice(void) {
GPUCreateSystemDefaultDevice(GPUInstance *inst) {
GPUApi *api;

if (!(api = gpuActiveGPUApi()))
return NULL;

return api->device.createSystemDefaultDevice(api);
return api->device.createSystemDefaultDevice(api, inst);
}
28 changes: 28 additions & 0 deletions src/api/instance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2020 Recep Aslantas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "../common.h"

GPU_EXPORT
GPUInstance*
GPUCreateInstance(void * __restrict unused) {
GPUApi *api;

if (!(api = gpuActiveGPUApi()))
return NULL;

return api->instance.createInstance(api, unused);
}
1 change: 1 addition & 0 deletions src/backend/dx12/impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ GPU_HIDE void dx12_initSwapChain(GPUApiSwapChain* apiSwapChain);
GPU_HIDE void dx12_initFrame(GPUApiFrame *apiFrame);
GPU_HIDE void dx12_initDescriptor(GPUApiDescriptor *apiDescriptor);
GPU_HIDE void dx12_initSampler(GPUApiSampler *apiSampler);
GPU_HIDE void dx12_initInstance(GPUApiInstance *apiInstance);

#endif /* dx12_apis_h */
39 changes: 25 additions & 14 deletions src/backend/dx12/impl/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
GPU_HIDE
void
dx12__getHardwareAdapter(IDXGIFactory4* dxgiFactory, IDXGIAdapter1** ppAdapter) {
IDXGIAdapter1* adapter = NULL;
*ppAdapter = NULL;
IDXGIAdapter1 *adapter;
UINT adapterIndex;

adapter = NULL;
*ppAdapter = NULL;
adapterIndex = 0;

UINT adapterIndex = 0;
while (dxgiFactory->lpVtbl->EnumAdapters1(dxgiFactory, adapterIndex, &adapter) != DXGI_ERROR_NOT_FOUND) {
DXGI_ADAPTER_DESC1 desc;
adapter->lpVtbl->GetDesc1(adapter, &desc);
Expand All @@ -37,8 +40,8 @@ dx12__getHardwareAdapter(IDXGIFactory4* dxgiFactory, IDXGIAdapter1** ppAdapter)
/* Check to see if the adapter supports Direct3D 12, but don't create the
actual device yet.*/
if (SUCCEEDED(D3D12CreateDevice((IUnknown*)adapter,
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device, NULL))) {
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device, NULL))) {
*ppAdapter = adapter; /* Transfer ownership to caller */
return;
}
Expand All @@ -54,30 +57,38 @@ dx12__createDevice(ID3D12Device** p_d3dDevice, IDXGIFactory4** p_dxgiFactory, ID
ID3D12Device *d3dDevice;
IDXGIFactory4 *dxgiFactory;
IDXGIAdapter1 *adapter;
UINT dxgiFactoryFlags;
HRESULT hr;

dxgiFactoryFlags = 0;

#if defined(_DEBUG)
/* If the project is in a debug build, enable debugging via SDK Layers.*/
/* Enable the debug layer (requires the Graphics Tools "optional feature").
NOTE: Enabling the debug layer after device creation will invalidate the active device.
*/
{
ID3D12Debug* debugController;
if (SUCCEEDED(D3D12GetDebugInterface(&IID_ID3D12Debug,
(void**)&debugController))) {
debugController->lpVtbl->EnableDebugLayer(debugController);
debugController->lpVtbl->Release(debugController);

/* Enable additional debug layers. */
dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
}
}
#endif

hr = CreateDXGIFactory1(&IID_IDXGIFactory1, (void**)&dxgiFactory);
hr = CreateDXGIFactory2(dxgiFactoryFlags, &IID_IDXGIFactory1, (void**)&dxgiFactory);
dxThrowIfFailed(hr);

dx12__getHardwareAdapter(dxgiFactory, &adapter);

/* Create the Direct3D 12 API device object */
hr = D3D12CreateDevice((IUnknown*)adapter,
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device,
(void**)&d3dDevice);
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device,
(void**)&d3dDevice);

if (adapter) {
adapter->lpVtbl->Release(adapter);
Expand All @@ -91,9 +102,9 @@ dx12__createDevice(ID3D12Device** p_d3dDevice, IDXGIFactory4** p_dxgiFactory, ID
dxThrowIfFailed(hr);

hr = D3D12CreateDevice((IUnknown*)warpAdapter,
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device,
(void**)&d3dDevice);
D3D_FEATURE_LEVEL_11_0,
&IID_ID3D12Device,
(void**)&d3dDevice);

if (warpAdapter) {
warpAdapter->lpVtbl->Release(warpAdapter);
Expand All @@ -112,7 +123,7 @@ dx12__createDevice(ID3D12Device** p_d3dDevice, IDXGIFactory4** p_dxgiFactory, ID

GPU_HIDE
GPUDevice*
dx12_createSystemDefaultDevice(GPUApi *api) {
dx12_createSystemDefaultDevice(GPUApi *api, GPUInstance * __restrict inst) {
GPUDevice *device;
ID3D12Device *d3dDevice;
IDXGIFactory4 *dxgiFactory;
Expand Down
31 changes: 31 additions & 0 deletions src/backend/dx12/impl/instance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 Recep Aslantas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "../common.h"

GPU_HIDE
GPUInstance *
dx12_createInstance(struct GPUApi *__restrict api, void *__restrict unused) {
GPUInstance *inst;
inst = calloc(1, sizeof(*inst));
return inst;
}

GPU_HIDE
void
dx12_initInstance(GPUApiInstance *apiInstance) {
apiInstance->createInstance = dx12_createInstance;
}
1 change: 1 addition & 0 deletions src/backend/dx12/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ backend_dx12(void) {
dx12_initSwapChain(&dx12.swapchain);
dx12_initFrame(&dx12.frame);
dx12_initDescriptor(&dx12.descriptor);
dx12_initInstance(&dx12.instance);

dx12.initialized = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/mt/impl/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

GPU_HIDE
GPUDevice*
mt_createSystemDefaultDevice(GPUApi *api) {
mt_createSystemDefaultDevice(GPUApi *api, GPUInstance * __restrict inst) {
GPUDevice *device;
MtDevice *mtDevice;

Expand Down
8 changes: 8 additions & 0 deletions win/gpu/gpu.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@
<ClInclude Include="..\..\include\gpu\api\device.h" />
<ClInclude Include="..\..\include\gpu\api\frame.h" />
<ClInclude Include="..\..\include\gpu\api\gpudef.h" />
<ClInclude Include="..\..\include\gpu\api\instance.h" />
<ClInclude Include="..\..\include\gpu\api\library.h" />
<ClInclude Include="..\..\include\gpu\api\pass.h" />
<ClInclude Include="..\..\include\gpu\api\rce.h" />
<ClInclude Include="..\..\include\gpu\api\render-pipeline.h" />
<ClInclude Include="..\..\include\gpu\api\sampler.h" />
<ClInclude Include="..\..\include\gpu\api\swapchain.h" />
<ClInclude Include="..\..\include\gpu\api\vertex.h" />
<ClInclude Include="..\..\include\gpu\buffer.h" />
Expand All @@ -228,12 +230,14 @@
<ClInclude Include="..\..\include\gpu\feature.h" />
<ClInclude Include="..\..\include\gpu\frame.h" />
<ClInclude Include="..\..\include\gpu\gpu.h" />
<ClInclude Include="..\..\include\gpu\instance.h" />
<ClInclude Include="..\..\include\gpu\library.h" />
<ClInclude Include="..\..\include\gpu\metalkit.h" />
<ClInclude Include="..\..\include\gpu\pass.h" />
<ClInclude Include="..\..\include\gpu\pipeline.h" />
<ClInclude Include="..\..\include\gpu\pixelformat.h" />
<ClInclude Include="..\..\include\gpu\resource.h" />
<ClInclude Include="..\..\include\gpu\sampler.h" />
<ClInclude Include="..\..\include\gpu\stage-io.h" />
<ClInclude Include="..\..\include\gpu\swapchain.h" />
<ClInclude Include="..\..\include\gpu\texture.h" />
Expand All @@ -252,18 +256,22 @@
<ClCompile Include="..\..\src\api\descr\descriptor.c" />
<ClCompile Include="..\..\src\api\device.c" />
<ClCompile Include="..\..\src\api\frame.c" />
<ClCompile Include="..\..\src\api\instance.c" />
<ClCompile Include="..\..\src\api\library.c" />
<ClCompile Include="..\..\src\api\pass\renderpass.c" />
<ClCompile Include="..\..\src\api\render\pipeline.c" />
<ClCompile Include="..\..\src\api\render\rce.c" />
<ClCompile Include="..\..\src\api\sampler.c" />
<ClCompile Include="..\..\src\api\swapchain.c" />
<ClCompile Include="..\..\src\api\vertex.c" />
<ClCompile Include="..\..\src\backend\backend.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\cmdque.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\descr\descpool.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\device.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\frame.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\instance.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\renderpass.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\sampler.c" />
<ClCompile Include="..\..\src\backend\dx12\impl\swapchain.c" />
<ClCompile Include="..\..\src\backend\dx12\init.c" />
<ClCompile Include="..\..\src\init.c" />
Expand Down
Loading

0 comments on commit 1a78f72

Please sign in to comment.