mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +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;
|
WGPUInstance instance;
|
||||||
WGPUSurface surface;
|
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)
|
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
|
#endif
|
||||||
|
|
||||||
|
// create surface
|
||||||
WGPUSurfaceDescriptor surfaceDesc{};
|
WGPUSurfaceDescriptor surfaceDesc{};
|
||||||
surfaceDesc.nextInChain = (const WGPUChainedStruct*)&surfaceNativeDesc;
|
surfaceDesc.nextInChain = (const WGPUChainedStruct*)&surfaceNativeDesc;
|
||||||
surfaceDesc.label = "The surface";
|
surfaceDesc.label = "The surface";
|
||||||
surface = wgpuInstanceCreateSurface(instance, &surfaceDesc);
|
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
|
//Create a Canvas
|
||||||
canvas = tvg::WgCanvas::gen();
|
canvas = tvg::WgCanvas::gen();
|
||||||
if (!canvas) {
|
if (!canvas) {
|
||||||
|
@ -433,6 +450,8 @@ struct WgWindow : Window
|
||||||
delete(canvas);
|
delete(canvas);
|
||||||
canvas = nullptr;
|
canvas = nullptr;
|
||||||
|
|
||||||
|
wgpuDeviceRelease(device);
|
||||||
|
wgpuAdapterRelease(adapter);
|
||||||
wgpuSurfaceRelease(surface);
|
wgpuSurfaceRelease(surface);
|
||||||
wgpuInstanceRelease(instance);
|
wgpuInstanceRelease(instance);
|
||||||
}
|
}
|
||||||
|
@ -440,7 +459,7 @@ struct WgWindow : Window
|
||||||
void resize() override
|
void resize() override
|
||||||
{
|
{
|
||||||
//Set the canvas target and draw on it.
|
//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
|
void refresh() override
|
||||||
|
|
|
@ -65,7 +65,6 @@ void WgRenderer::release()
|
||||||
|
|
||||||
// release gpu handles
|
// release gpu handles
|
||||||
clearTargets();
|
clearTargets();
|
||||||
releaseDevice();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -297,12 +296,7 @@ bool WgRenderer::target(WGPUDevice device, WGPUInstance instance, void* target,
|
||||||
release();
|
release();
|
||||||
|
|
||||||
// can not initialize renderer, give up
|
// can not initialize renderer, give up
|
||||||
if (!instance || !target || !width || !height) return false;
|
if (!instance || !device || !target || !width || !height) return false;
|
||||||
|
|
||||||
// store or regest gpu device
|
|
||||||
this->device = device;
|
|
||||||
if (!this->device)
|
|
||||||
reguestDevice(instance, (WGPUSurface)target);
|
|
||||||
|
|
||||||
// store target properties
|
// store target properties
|
||||||
mTargetSurface.stride = width;
|
mTargetSurface.stride = width;
|
||||||
|
@ -310,7 +304,7 @@ bool WgRenderer::target(WGPUDevice device, WGPUInstance instance, void* target,
|
||||||
mTargetSurface.h = height;
|
mTargetSurface.h = height;
|
||||||
|
|
||||||
// initialize rendering context
|
// initialize rendering context
|
||||||
mContext.initialize(instance, this->device);
|
mContext.initialize(instance, device);
|
||||||
|
|
||||||
// initialize render tree instances
|
// initialize render tree instances
|
||||||
mRenderStoragePool.initialize(mContext, width, height);
|
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() {
|
void WgRenderer::clearTargets() {
|
||||||
releaseSurfaceTexture();
|
releaseSurfaceTexture();
|
||||||
targetTexture = nullptr;
|
targetTexture = nullptr;
|
||||||
|
|
|
@ -66,8 +66,6 @@ private:
|
||||||
void disposeObjects();
|
void disposeObjects();
|
||||||
void releaseSurfaceTexture();
|
void releaseSurfaceTexture();
|
||||||
|
|
||||||
void reguestDevice(WGPUInstance instance, WGPUSurface surface);
|
|
||||||
void releaseDevice();
|
|
||||||
void clearTargets();
|
void clearTargets();
|
||||||
bool surfaceConfigure(WGPUSurface surface, WgContext& context, uint32_t width, uint32_t height);
|
bool surfaceConfigure(WGPUSurface surface, WgContext& context, uint32_t width, uint32_t height);
|
||||||
|
|
||||||
|
@ -101,9 +99,6 @@ private:
|
||||||
WGPUTexture targetTexture{}; // external handle
|
WGPUTexture targetTexture{}; // external handle
|
||||||
WGPUSurfaceTexture surfaceTexture{};
|
WGPUSurfaceTexture surfaceTexture{};
|
||||||
WGPUSurface surface{}; // external handle
|
WGPUSurface surface{}; // external handle
|
||||||
WGPUAdapter adapter{};
|
|
||||||
WGPUDevice device{};
|
|
||||||
bool gpuOwner{};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* _TVG_WG_RENDERER_H_ */
|
#endif /* _TVG_WG_RENDERER_H_ */
|
||||||
|
|
Loading…
Add table
Reference in a new issue