mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-07 21:23:32 +00:00
wg_engine: revision of device creation policy
make valid device and instance as mandatory paramter for webgpu renderer
This commit is contained in:
parent
77201546dd
commit
cf05128a96
3 changed files with 22 additions and 45 deletions
|
@ -367,6 +367,8 @@ struct WgWindow : Window
|
|||
{
|
||||
WGPUInstance instance;
|
||||
WGPUSurface surface;
|
||||
WGPUAdapter adapter;
|
||||
WGPUDevice device;
|
||||
|
||||
WgWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Wg, example, width, height, threadsCnt)
|
||||
{
|
||||
|
@ -412,11 +414,26 @@ struct WgWindow : Window
|
|||
};
|
||||
#endif
|
||||
|
||||
// create surface
|
||||
WGPUSurfaceDescriptor surfaceDesc{};
|
||||
surfaceDesc.nextInChain = (const WGPUChainedStruct*)&surfaceNativeDesc;
|
||||
surfaceDesc.label = "The surface";
|
||||
surface = wgpuInstanceCreateSurface(instance, &surfaceDesc);
|
||||
|
||||
// request adapter
|
||||
const WGPURequestAdapterOptions requestAdapterOptions { .compatibleSurface = surface, .powerPreference = WGPUPowerPreference_HighPerformance };
|
||||
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * pUserData) { *((WGPUAdapter*)pUserData) = adapter; };
|
||||
wgpuInstanceRequestAdapter(instance, &requestAdapterOptions, onAdapterRequestEnded, &adapter);
|
||||
|
||||
// get adapter and surface properties
|
||||
WGPUFeatureName featureNames[32]{};
|
||||
size_t featuresCount = wgpuAdapterEnumerateFeatures(this->adapter, featureNames);
|
||||
|
||||
// request device
|
||||
const WGPUDeviceDescriptor deviceDesc { .label = "The owned device", .requiredFeatureCount = featuresCount, .requiredFeatures = featureNames };
|
||||
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * pUserData) { *((WGPUDevice*)pUserData) = device; };
|
||||
wgpuAdapterRequestDevice(this->adapter, &deviceDesc, onDeviceRequestEnded, &device);
|
||||
|
||||
//Create a Canvas
|
||||
canvas = tvg::WgCanvas::gen();
|
||||
if (!canvas) {
|
||||
|
@ -433,6 +450,8 @@ struct WgWindow : Window
|
|||
delete(canvas);
|
||||
canvas = nullptr;
|
||||
|
||||
wgpuDeviceRelease(device);
|
||||
wgpuAdapterRelease(adapter);
|
||||
wgpuSurfaceRelease(surface);
|
||||
wgpuInstanceRelease(instance);
|
||||
}
|
||||
|
@ -440,7 +459,7 @@ struct WgWindow : Window
|
|||
void resize() override
|
||||
{
|
||||
//Set the canvas target and draw on it.
|
||||
verify(static_cast<tvg::WgCanvas*>(canvas)->target(nullptr, instance, surface, width, height, tvg::ColorSpace::ABGR8888S));
|
||||
verify(static_cast<tvg::WgCanvas*>(canvas)->target(device, instance, surface, width, height, tvg::ColorSpace::ABGR8888S));
|
||||
}
|
||||
|
||||
void refresh() override
|
||||
|
|
|
@ -65,7 +65,6 @@ void WgRenderer::release()
|
|||
|
||||
// release gpu handles
|
||||
clearTargets();
|
||||
releaseDevice();
|
||||
}
|
||||
|
||||
|
||||
|
@ -297,12 +296,7 @@ bool WgRenderer::target(WGPUDevice device, WGPUInstance instance, void* target,
|
|||
release();
|
||||
|
||||
// can not initialize renderer, give up
|
||||
if (!instance || !target || !width || !height) return false;
|
||||
|
||||
// store or regest gpu device
|
||||
this->device = device;
|
||||
if (!this->device)
|
||||
reguestDevice(instance, (WGPUSurface)target);
|
||||
if (!instance || !device || !target || !width || !height) return false;
|
||||
|
||||
// store target properties
|
||||
mTargetSurface.stride = width;
|
||||
|
@ -310,7 +304,7 @@ bool WgRenderer::target(WGPUDevice device, WGPUInstance instance, void* target,
|
|||
mTargetSurface.h = height;
|
||||
|
||||
// initialize rendering context
|
||||
mContext.initialize(instance, this->device);
|
||||
mContext.initialize(instance, device);
|
||||
|
||||
// initialize render tree instances
|
||||
mRenderStoragePool.initialize(mContext, width, height);
|
||||
|
@ -326,37 +320,6 @@ bool WgRenderer::target(WGPUDevice device, WGPUInstance instance, void* target,
|
|||
}
|
||||
|
||||
|
||||
void WgRenderer::reguestDevice(WGPUInstance instance, WGPUSurface surface)
|
||||
{
|
||||
// request adapter
|
||||
const WGPURequestAdapterOptions requestAdapterOptions { .compatibleSurface = surface, .powerPreference = WGPUPowerPreference_HighPerformance };
|
||||
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * pUserData) { *((WGPUAdapter*)pUserData) = adapter; };
|
||||
wgpuInstanceRequestAdapter(instance, &requestAdapterOptions, onAdapterRequestEnded, &this->adapter);
|
||||
|
||||
// get adapter and surface properties
|
||||
WGPUFeatureName featureNames[32]{};
|
||||
size_t featuresCount = wgpuAdapterEnumerateFeatures(this->adapter, featureNames);
|
||||
|
||||
// request device
|
||||
const WGPUDeviceDescriptor deviceDesc { .label = "The owned device", .requiredFeatureCount = featuresCount, .requiredFeatures = featureNames };
|
||||
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * pUserData) { *((WGPUDevice*)pUserData) = device; };
|
||||
wgpuAdapterRequestDevice(this->adapter, &deviceDesc, onDeviceRequestEnded, &this->device);
|
||||
gpuOwner = true;
|
||||
}
|
||||
|
||||
|
||||
void WgRenderer::releaseDevice()
|
||||
{
|
||||
if (!gpuOwner) return;
|
||||
wgpuDeviceRelease(device);
|
||||
wgpuDeviceDestroy(device);
|
||||
wgpuAdapterRelease(adapter);
|
||||
device = nullptr;
|
||||
adapter = nullptr;
|
||||
gpuOwner = false;
|
||||
}
|
||||
|
||||
|
||||
void WgRenderer::clearTargets() {
|
||||
releaseSurfaceTexture();
|
||||
targetTexture = nullptr;
|
||||
|
|
|
@ -66,8 +66,6 @@ private:
|
|||
void disposeObjects();
|
||||
void releaseSurfaceTexture();
|
||||
|
||||
void reguestDevice(WGPUInstance instance, WGPUSurface surface);
|
||||
void releaseDevice();
|
||||
void clearTargets();
|
||||
bool surfaceConfigure(WGPUSurface surface, WgContext& context, uint32_t width, uint32_t height);
|
||||
|
||||
|
@ -101,9 +99,6 @@ private:
|
|||
WGPUTexture targetTexture{}; // external handle
|
||||
WGPUSurfaceTexture surfaceTexture{};
|
||||
WGPUSurface surface{}; // external handle
|
||||
WGPUAdapter adapter{};
|
||||
WGPUDevice device{};
|
||||
bool gpuOwner{};
|
||||
};
|
||||
|
||||
#endif /* _TVG_WG_RENDERER_H_ */
|
||||
|
|
Loading…
Add table
Reference in a new issue