OpenGL: Implement the backend connection and adapter.

The OpenGL backend can't gather discover default adapters because it
needs getProc to do anything so we add DiscoverAdapters method to
Instance that takes backend-specific options.

dawn_native::opengl::CreateDevice is removed in favor of the adapter
path so OpenGLBinding is modified to create an instance locally. This is
only temporary until all backends support adapters, at which point a lot
of *Binding code will be factored.

Also contains a small fix for Result<T, E> with movable types.

BUG=dawn:29

Change-Id: I4eb3d4a14a871af73e1872132aff72b45e5fe566
Reviewed-on: https://dawn-review.googlesource.com/c/3663
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Corentin Wallez
2019-01-07 09:48:03 +00:00
committed by Commit Bot service account
parent ab8eb2d6ed
commit 90e594ee8b
14 changed files with 221 additions and 27 deletions

View File

@@ -0,0 +1,90 @@
// Copyright 2019 The Dawn Authors
//
// 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 "dawn_native/opengl/BackendGL.h"
#include "dawn_native/OpenGLBackend.h"
#include "dawn_native/opengl/DeviceGL.h"
namespace dawn_native { namespace opengl {
// Implementation of OpenGLBackend.h
AdapterDiscoveryOptions::AdapterDiscoveryOptions()
: AdapterDiscoveryOptionsBase(BackendType::OpenGL) {
}
// The OpenGL backend's Adapter.
class Adapter : public AdapterBase {
public:
Adapter(InstanceBase* instance, const AdapterDiscoveryOptions* options)
: AdapterBase(instance, BackendType::OpenGL) {
// Use getProc to populate GLAD.
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(options->getProc));
// Set state that never changes between devices.
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
}
virtual ~Adapter() = default;
private:
ResultOrError<DeviceBase*> CreateDeviceImpl() override {
// There is no limit on the number of devices created from this adapter because they can
// all share the same backing OpenGL context.
return {new Device};
}
};
// Implementation of the OpenGL backend's BackendConnection
Backend::Backend(InstanceBase* instance) : BackendConnection(instance, BackendType::OpenGL) {
}
std::vector<std::unique_ptr<AdapterBase>> Backend::DiscoverDefaultAdapters() {
// The OpenGL backend needs at least "getProcAddress" to discover an adapter.
return {};
}
ResultOrError<std::vector<std::unique_ptr<AdapterBase>>> Backend::DiscoverAdapters(
const AdapterDiscoveryOptionsBase* optionsBase) {
// TODO(cwallez@chromium.org): For now we can only create a single adapter because glad uses
// static variables to store the pointers to OpenGL procs. Also, if we could create
// multiple adapters, we would need to figure out what to do about MakeCurrent.
if (mCreatedAdapter) {
return DAWN_VALIDATION_ERROR("The OpenGL backend can only create a single adapter");
}
ASSERT(optionsBase->backendType == BackendType::OpenGL);
const AdapterDiscoveryOptions* options =
reinterpret_cast<const AdapterDiscoveryOptions*>(optionsBase);
if (options->getProc == nullptr) {
return DAWN_VALIDATION_ERROR("AdapterDiscoveryOptions::getProc must be set");
}
std::vector<std::unique_ptr<AdapterBase>> adapters;
adapters.push_back(std::make_unique<Adapter>(GetInstance(), options));
mCreatedAdapter = true;
return std::move(adapters);
}
BackendConnection* Connect(InstanceBase* instance) {
return new Backend(instance);
}
}} // namespace dawn_native::opengl

View File

@@ -0,0 +1,31 @@
// Copyright 2019 The Dawn Authors
//
// 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 "dawn_native/BackendConnection.h"
namespace dawn_native { namespace opengl {
class Backend : public BackendConnection {
public:
Backend(InstanceBase* instance);
std::vector<std::unique_ptr<AdapterBase>> DiscoverDefaultAdapters() override;
ResultOrError<std::vector<std::unique_ptr<AdapterBase>>> DiscoverAdapters(
const AdapterDiscoveryOptionsBase* options) override;
private:
bool mCreatedAdapter = false;
};
}} // namespace dawn_native::opengl

View File

@@ -33,22 +33,6 @@
namespace dawn_native { namespace opengl {
dawnDevice CreateDevice(void* (*getProc)(const char*)) {
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(getProc));
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
return reinterpret_cast<dawnDevice>(new Device);
}
BackendConnection* Connect(InstanceBase* instance) {
return nullptr;
}
// Device
Device::Device() {
CollectPCIInfo();
}