Skip to content

Commit

Permalink
Implement requesting WebGPU adapter with GPURequestAdapterOptions
Browse files Browse the repository at this point in the history
This patch is the first one to support creating GPUDevice with extensions.
In this patch we implements requesting a WebGPU adapter with options.

Now we only support "powerPreference" as an option to request an adapter.
The option is treated as an enum and passed from Blink to the WebGPU
command buffer to create a Dawn adapter. The "high-performance" adapter
refers to the discrete GPU, and the "low-power" adapter refers to the
integrated GPU. The default adapter will be the one that refers to the
discrete GPU.

BUG=chromium:996713

Change-Id: Iffb98410cf2a2d6d4eb127bdd09a0886099db127
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1794033
Reviewed-by: Kenneth Russell <kbr@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
Cr-Commit-Position: refs/heads/master@{#699598}
  • Loading branch information
Jiawei-Shao authored and Commit Bot committed Sep 25, 2019
1 parent 10ae04f commit 5c5393b
Show file tree
Hide file tree
Showing 26 changed files with 304 additions and 53 deletions.
14 changes: 14 additions & 0 deletions gpu/command_buffer/build_webgpu_cmd_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#
# Options are documented in build_gles2_cmd_buffer.py/build_raster_cmd_buffer.py
_NAMED_TYPE_INFO = {
'PowerPreference': {
'type': 'PowerPreference',
'valid': [
'PowerPreference::kHighPerformance',
'PowerPreference::kLowPower',
],
'invalid': [
'PowerPreference::kNumPowerPreferences',
],
}
}

# A function info object specifies the type and other special data for the
Expand Down Expand Up @@ -50,6 +60,10 @@
'DissociateMailbox': {
'trace_level': 1,
},
'RequestAdapter': {
'impl_func': False,
'cmd_args': 'uint32_t power_preference'
},
}

def main(argv):
Expand Down
1 change: 1 addition & 0 deletions gpu/command_buffer/client/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ source_set("webgpu_interface") {
deps = [
":interface_base",
"//base",
"//gpu/command_buffer/common:webgpu",
]
}

Expand Down
7 changes: 7 additions & 0 deletions gpu/command_buffer/client/webgpu_cmd_helper_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ void DissociateMailbox(GLuint texture_id, GLuint texture_generation) {
}
}

void RequestAdapter(uint32_t power_preference) {
webgpu::cmds::RequestAdapter* c = GetCmdSpace<webgpu::cmds::RequestAdapter>();
if (c) {
c->Init(power_preference);
}
}

#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_CMD_HELPER_AUTOGEN_H_
7 changes: 7 additions & 0 deletions gpu/command_buffer/client/webgpu_implementation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,12 @@ ReservedTexture WebGPUImplementation::ReserveTexture(DawnDevice device) {
#endif
}

void WebGPUImplementation::RequestAdapter(PowerPreference power_preference) {
GPU_CLIENT_SINGLE_THREAD_CHECK();
GPU_CLIENT_LOG("[" << GetLogPrefix() << "] wgRequestAdapter("
<< static_cast<uint32_t>(power_preference) << ")");
helper_->RequestAdapter(static_cast<uint32_t>(power_preference));
}

} // namespace webgpu
} // namespace gpu
3 changes: 3 additions & 0 deletions gpu/command_buffer/client/webgpu_implementation_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ void AssociateMailbox(GLuint device_id,

void DissociateMailbox(GLuint texture_id, GLuint texture_generation) override;

void RequestAdapter(PowerPreference power_preference =
PowerPreference::kHighPerformance) override;

#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_IMPLEMENTATION_AUTOGEN_H_
11 changes: 11 additions & 0 deletions gpu/command_buffer/client/webgpu_implementation_unittest_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ TEST_F(WebGPUImplementationTest, DissociateMailbox) {
gl_->DissociateMailbox(1, 2);
EXPECT_EQ(0, memcmp(&expected, commands_, sizeof(expected)));
}

TEST_F(WebGPUImplementationTest, RequestAdapter) {
struct Cmds {
cmds::RequestAdapter cmd;
};
Cmds expected;
expected.cmd.Init(1);

gl_->RequestAdapter(PowerPreference::kHighPerformance);
EXPECT_EQ(0, memcmp(&expected, commands_, sizeof(expected)));
}
#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_IMPLEMENTATION_UNITTEST_AUTOGEN_H_
1 change: 1 addition & 0 deletions gpu/command_buffer/client/webgpu_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <dawn/dawn.h>

#include "gpu/command_buffer/client/interface_base.h"
#include "gpu/command_buffer/common/webgpu_cmd_enums.h"

namespace gpu {
namespace webgpu {
Expand Down
2 changes: 2 additions & 0 deletions gpu/command_buffer/client/webgpu_interface_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ virtual void AssociateMailbox(GLuint device_id,
const GLbyte* mailbox) = 0;
virtual void DissociateMailbox(GLuint texture_id,
GLuint texture_generation) = 0;
virtual void RequestAdapter(
PowerPreference power_preference = PowerPreference::kHighPerformance) = 0;
#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_AUTOGEN_H_
1 change: 1 addition & 0 deletions gpu/command_buffer/client/webgpu_interface_stub_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ void AssociateMailbox(GLuint device_id,
GLuint usage,
const GLbyte* mailbox) override;
void DissociateMailbox(GLuint texture_id, GLuint texture_generation) override;
void RequestAdapter(PowerPreference power_preference) override;
#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_STUB_AUTOGEN_H_
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ void WebGPUInterfaceStub::AssociateMailbox(GLuint /* device_id */,
const GLbyte* /* mailbox */) {}
void WebGPUInterfaceStub::DissociateMailbox(GLuint /* texture_id */,
GLuint /* texture_generation */) {}
void WebGPUInterfaceStub::RequestAdapter(
PowerPreference /* power_preference */) {}
#endif // GPU_COMMAND_BUFFER_CLIENT_WEBGPU_INTERFACE_STUB_IMPL_AUTOGEN_H_
1 change: 1 addition & 0 deletions gpu/command_buffer/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ source_set("webgpu_sources") {

sources = [
"dawn_memory_transfer_handle.h",
"webgpu_cmd_enums.h",
"webgpu_cmd_format.cc",
"webgpu_cmd_format.h",
"webgpu_cmd_format_autogen.h",
Expand Down
25 changes: 25 additions & 0 deletions gpu/command_buffer/common/webgpu_cmd_enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_COMMON_WEBGPU_CMD_ENUMS_H_
#define GPU_COMMAND_BUFFER_COMMON_WEBGPU_CMD_ENUMS_H_

namespace gpu {
namespace webgpu {

enum class PowerPreference : uint32_t {
kLowPower,
kHighPerformance,
kNumPowerPreferences
};

// These numbers must not change
static_assert(static_cast<int>(PowerPreference::kLowPower) == 0,
"kLowPower should equal 0");
static_assert(static_cast<int>(PowerPreference::kHighPerformance) == 1,
"kHighPerformance should equal 1");

} // namespace webgpu
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_COMMON_WEBGPU_CMD_ENUMS_H_
33 changes: 33 additions & 0 deletions gpu/command_buffer/common/webgpu_cmd_format_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,37 @@ static_assert(offsetof(DissociateMailbox, texture_id) == 4,
static_assert(offsetof(DissociateMailbox, texture_generation) == 8,
"offset of DissociateMailbox texture_generation should be 8");

struct RequestAdapter {
typedef RequestAdapter ValueType;
static const CommandId kCmdId = kRequestAdapter;
static const cmd::ArgFlags kArgFlags = cmd::kFixed;
static const uint8_t cmd_flags = CMD_FLAG_SET_TRACE_LEVEL(3);

static uint32_t ComputeSize() {
return static_cast<uint32_t>(sizeof(ValueType)); // NOLINT
}

void SetHeader() { header.SetCmd<ValueType>(); }

void Init(uint32_t _power_preference) {
SetHeader();
power_preference = _power_preference;
}

void* Set(void* cmd, uint32_t _power_preference) {
static_cast<ValueType*>(cmd)->Init(_power_preference);
return NextCmdAddress<ValueType>(cmd);
}

gpu::CommandHeader header;
uint32_t power_preference;
};

static_assert(sizeof(RequestAdapter) == 8,
"size of RequestAdapter should be 8");
static_assert(offsetof(RequestAdapter, header) == 0,
"offset of RequestAdapter header should be 0");
static_assert(offsetof(RequestAdapter, power_preference) == 4,
"offset of RequestAdapter power_preference should be 4");

#endif // GPU_COMMAND_BUFFER_COMMON_WEBGPU_CMD_FORMAT_AUTOGEN_H_
10 changes: 10 additions & 0 deletions gpu/command_buffer/common/webgpu_cmd_format_test_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ TEST_F(WebGPUFormatTest, DissociateMailbox) {
CheckBytesWrittenMatchesExpectedSize(next_cmd, sizeof(cmd));
}

TEST_F(WebGPUFormatTest, RequestAdapter) {
cmds::RequestAdapter& cmd = *GetBufferAs<cmds::RequestAdapter>();
void* next_cmd = cmd.Set(&cmd, static_cast<uint32_t>(11));
EXPECT_EQ(static_cast<uint32_t>(cmds::RequestAdapter::kCmdId),
cmd.header.command);
EXPECT_EQ(sizeof(cmd), cmd.header.size * 4u);
EXPECT_EQ(static_cast<uint32_t>(11), cmd.power_preference);
CheckBytesWrittenMatchesExpectedSize(next_cmd, sizeof(cmd));
}

#endif // GPU_COMMAND_BUFFER_COMMON_WEBGPU_CMD_FORMAT_TEST_AUTOGEN_H_
3 changes: 2 additions & 1 deletion gpu/command_buffer/common/webgpu_cmd_ids_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#define WEBGPU_COMMAND_LIST(OP) \
OP(DawnCommands) /* 256 */ \
OP(AssociateMailboxImmediate) /* 257 */ \
OP(DissociateMailbox) /* 258 */
OP(DissociateMailbox) /* 258 */ \
OP(RequestAdapter) /* 259 */

enum CommandId {
kOneBeforeStartPoint =
Expand Down
47 changes: 47 additions & 0 deletions gpu/command_buffer/service/webgpu_cmd_validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,58 @@

#include <algorithm>
#include <vector>
#include "base/stl_util.h"
#include "gpu/command_buffer/common/webgpu_cmd_enums.h"
#include "gpu/command_buffer/common/webgpu_cmd_format.h"
#include "gpu/command_buffer/service/gles2_cmd_validation.h"

namespace gpu {
namespace webgpu {

// ValueValidator returns true if a value is valid.
template <typename T>
class ValueValidator {
public:
ValueValidator() = default;

ValueValidator(const T* valid_values, int num_values) {
AddValues(valid_values, num_values);
}

void AddValue(const T value) {
if (!IsValid(value)) {
valid_values_.push_back(value);
}
}

void AddValues(const T* valid_values, int num_values) {
for (int ii = 0; ii < num_values; ++ii) {
AddValue(valid_values[ii]);
}
}

void RemoveValues(const T* invalid_values, int num_values) {
for (int ii = 0; ii < num_values; ++ii) {
auto iter = std::find(valid_values_.begin(), valid_values_.end(),
invalid_values[ii]);
if (iter != valid_values_.end()) {
valid_values_.erase(iter);
DCHECK(!IsValid(invalid_values[ii]));
}
}
}

bool IsValid(const T value) const {
return std::find(valid_values_.begin(), valid_values_.end(), value) !=
valid_values_.end();
}

const std::vector<T>& GetValues() const { return valid_values_; }

private:
std::vector<T> valid_values_;
};

struct Validators {
Validators();

Expand Down
2 changes: 2 additions & 0 deletions gpu/command_buffer/service/webgpu_cmd_validation_autogen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
#ifndef GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_AUTOGEN_H_
#define GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_AUTOGEN_H_

ValueValidator<PowerPreference> power_preference;

#endif // GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_AUTOGEN_H_
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#ifndef GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_IMPLEMENTATION_AUTOGEN_H_
#define GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_IMPLEMENTATION_AUTOGEN_H_

Validators::Validators() {}
static const PowerPreference valid_power_preference_table[] = {
PowerPreference::kHighPerformance,
PowerPreference::kLowPower,
};

Validators::Validators()
: power_preference(valid_power_preference_table,
base::size(valid_power_preference_table)) {}

#endif // GPU_COMMAND_BUFFER_SERVICE_WEBGPU_CMD_VALIDATION_IMPLEMENTATION_AUTOGEN_H_
Loading

0 comments on commit 5c5393b

Please sign in to comment.