OpenGL: Make Device::gl private.

Make Device::gl private, and rename it to mGL. Change all accesses to
use Device::GetGL() accessor.

This is a precursor to adding native EGLContext support to
Dawn.

Bug: dawn:810
Change-Id: I3793d83644a70bafc6bea8b423c1a7c76beb248d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94560
Reviewed-by: Loko Kung <lokokung@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Stephen White 2022-06-24 14:12:39 +00:00 committed by Dawn LUCI CQ
parent e8d47d5263
commit c174692187
12 changed files with 67 additions and 52 deletions

View File

@ -39,21 +39,22 @@ ResultOrError<Ref<Buffer>> Buffer::CreateInternalBuffer(Device* device,
Buffer::Buffer(Device* device, const BufferDescriptor* descriptor)
: BufferBase(device, descriptor) {
const OpenGLFunctions& gl = device->GetGL();
// Allocate at least 4 bytes so clamped accesses are always in bounds.
mAllocatedSize = std::max(GetSize(), uint64_t(4u));
device->gl.GenBuffers(1, &mBuffer);
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
gl.GenBuffers(1, &mBuffer);
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
// The buffers with mappedAtCreation == true will be initialized in
// BufferBase::MapAtCreation().
if (device->IsToggleEnabled(Toggle::NonzeroClearResourcesOnCreationForTesting) &&
!descriptor->mappedAtCreation) {
std::vector<uint8_t> clearValues(mAllocatedSize, 1u);
device->gl.BufferData(GL_ARRAY_BUFFER, mAllocatedSize, clearValues.data(), GL_STATIC_DRAW);
gl.BufferData(GL_ARRAY_BUFFER, mAllocatedSize, clearValues.data(), GL_STATIC_DRAW);
} else {
// Buffers start zeroed if you pass nullptr to glBufferData.
device->gl.BufferData(GL_ARRAY_BUFFER, mAllocatedSize, nullptr, GL_STATIC_DRAW);
gl.BufferData(GL_ARRAY_BUFFER, mAllocatedSize, nullptr, GL_STATIC_DRAW);
}
}
@ -112,10 +113,11 @@ void Buffer::InitializeToZero() {
const uint64_t size = GetAllocatedSize();
Device* device = ToBackend(GetDevice());
const OpenGLFunctions& gl = device->GetGL();
const std::vector<uint8_t> clearValues(size, 0u);
device->gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
device->gl.BufferSubData(GL_ARRAY_BUFFER, 0, size, clearValues.data());
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
gl.BufferSubData(GL_ARRAY_BUFFER, 0, size, clearValues.data());
device->IncrementLazyClearCountForTesting();
SetIsDataInitialized();
@ -128,14 +130,14 @@ bool Buffer::IsCPUWritableAtCreation() const {
}
MaybeError Buffer::MapAtCreationImpl() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
mMappedData = gl.MapBufferRange(GL_ARRAY_BUFFER, 0, GetSize(), GL_MAP_WRITE_BIT);
return {};
}
MaybeError Buffer::MapAsyncImpl(wgpu::MapMode mode, size_t offset, size_t size) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
// It is an error to map an empty range in OpenGL. We always have at least a 4-byte buffer
// so we extend the range to be 4 bytes.
@ -171,7 +173,7 @@ void* Buffer::GetMappedPointerImpl() {
}
void Buffer::UnmapImpl() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.BindBuffer(GL_ARRAY_BUFFER, mBuffer);
gl.UnmapBuffer(GL_ARRAY_BUFFER);
@ -179,8 +181,10 @@ void Buffer::UnmapImpl() {
}
void Buffer::DestroyImpl() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
BufferBase::DestroyImpl();
ToBackend(GetDevice())->gl.DeleteBuffers(1, &mBuffer);
gl.DeleteBuffers(1, &mBuffer);
mBuffer = 0;
}

View File

@ -448,7 +448,7 @@ CommandBuffer::CommandBuffer(CommandEncoder* encoder, const CommandBufferDescrip
: CommandBufferBase(encoder, descriptor) {}
MaybeError CommandBuffer::Execute() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
auto LazyClearSyncScope = [](const SyncScopeResourceUsage& scope) {
for (size_t i = 0; i < scope.textures.size(); i++) {
@ -767,7 +767,7 @@ MaybeError CommandBuffer::Execute() {
}
MaybeError CommandBuffer::ExecuteComputePass() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
ComputePipeline* lastPipeline = nullptr;
BindGroupTracker bindGroupTracker = {};
@ -844,7 +844,7 @@ MaybeError CommandBuffer::ExecuteComputePass() {
}
MaybeError CommandBuffer::ExecuteRenderPass(BeginRenderPassCmd* renderPass) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
GLuint fbo = 0;
// Create the framebuffer used for this render pass and calls the correct glDrawBuffers

View File

@ -29,16 +29,17 @@ ComputePipeline::~ComputePipeline() = default;
void ComputePipeline::DestroyImpl() {
ComputePipelineBase::DestroyImpl();
DeleteProgram(ToBackend(GetDevice())->gl);
DeleteProgram(ToBackend(GetDevice())->GetGL());
}
MaybeError ComputePipeline::Initialize() {
DAWN_TRY(InitializeBase(ToBackend(GetDevice())->gl, ToBackend(GetLayout()), GetAllStages()));
DAWN_TRY(
InitializeBase(ToBackend(GetDevice())->GetGL(), ToBackend(GetLayout()), GetAllStages()));
return {};
}
void ComputePipeline::ApplyNow() {
PipelineGL::ApplyNow(ToBackend(GetDevice())->gl);
PipelineGL::ApplyNow(ToBackend(GetDevice())->GetGL());
}
} // namespace dawn::native::opengl

View File

@ -46,7 +46,7 @@ ResultOrError<Ref<Device>> Device::Create(AdapterBase* adapter,
Device::Device(AdapterBase* adapter,
const DeviceDescriptor* descriptor,
const OpenGLFunctions& functions)
: DeviceBase(adapter, descriptor), gl(functions) {}
: DeviceBase(adapter, descriptor), mGL(functions) {}
Device::~Device() {
Destroy();
@ -60,6 +60,7 @@ MaybeError Device::Initialize(const DeviceDescriptor* descriptor) {
}
void Device::InitTogglesFromDriver() {
const OpenGLFunctions& gl = GetGL();
bool supportsBaseVertex = gl.IsAtLeastGLES(3, 2) || gl.IsAtLeastGL(3, 2);
bool supportsBaseInstance = gl.IsAtLeastGLES(3, 2) || gl.IsAtLeastGL(4, 2);
@ -124,6 +125,7 @@ const GLFormat& Device::GetGLFormat(const Format& format) {
}
GLenum Device::GetBGRAInternalFormat() const {
const OpenGLFunctions& gl = GetGL();
if (gl.IsGLExtensionSupported("GL_EXT_texture_format_BGRA8888") ||
gl.IsGLExtensionSupported("GL_APPLE_texture_format_BGRA8888")) {
return GL_BGRA8_EXT;
@ -195,6 +197,7 @@ ResultOrError<Ref<TextureViewBase>> Device::CreateTextureViewImpl(
}
void Device::SubmitFenceSync() {
const OpenGLFunctions& gl = GetGL();
GLsync sync = gl.FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
IncrementLastSubmittedCommandSerial();
mFencesInFlight.emplace(sync, GetLastSubmittedCommandSerial());
@ -224,6 +227,7 @@ MaybeError Device::ValidateEGLImageCanBeWrapped(const TextureDescriptor* descrip
}
TextureBase* Device::CreateTextureWrappingEGLImage(const ExternalImageDescriptor* descriptor,
::EGLImage image) {
const OpenGLFunctions& gl = GetGL();
const TextureDescriptor* textureDescriptor = FromAPI(descriptor->cTextureDescriptor);
if (ConsumedError(ValidateTextureDescriptor(this, textureDescriptor))) {
@ -264,6 +268,7 @@ MaybeError Device::TickImpl() {
ResultOrError<ExecutionSerial> Device::CheckAndUpdateCompletedSerials() {
ExecutionSerial fenceSerial{0};
const OpenGLFunctions& gl = GetGL();
while (!mFencesInFlight.empty()) {
auto [sync, tentativeSerial] = mFencesInFlight.front();
@ -314,6 +319,7 @@ void Device::DestroyImpl() {
}
MaybeError Device::WaitForIdleForDestruction() {
const OpenGLFunctions& gl = GetGL();
gl.Finish();
DAWN_TRY(CheckPassedSerials());
ASSERT(mFencesInFlight.empty());
@ -333,4 +339,8 @@ float Device::GetTimestampPeriodInNS() const {
return 1.0f;
}
const OpenGLFunctions& Device::GetGL() const {
return mGL;
}
} // namespace dawn::native::opengl

View File

@ -46,8 +46,8 @@ class Device final : public DeviceBase {
MaybeError Initialize(const DeviceDescriptor* descriptor);
// Contains all the OpenGL entry points, glDoFoo is called via device->gl.DoFoo.
const OpenGLFunctions gl;
// Contains all the OpenGL entry points, glDoFoo is called via gl.DoFoo.
const OpenGLFunctions& GetGL() const;
const GLFormat& GetGLFormat(const Format& format);
@ -121,6 +121,8 @@ class Device final : public DeviceBase {
void DestroyImpl() override;
MaybeError WaitForIdleForDestruction() override;
const OpenGLFunctions mGL;
std::queue<std::pair<GLsync, ExecutionSerial>> mFencesInFlight;
GLFormatTable mFormatTable;

View File

@ -24,13 +24,13 @@ NativeSwapChainImpl::NativeSwapChainImpl(Device* device,
: mPresentCallback(present), mPresentUserdata(presentUserdata), mDevice(device) {}
NativeSwapChainImpl::~NativeSwapChainImpl() {
const OpenGLFunctions& gl = mDevice->gl;
const OpenGLFunctions& gl = mDevice->GetGL();
gl.DeleteTextures(1, &mBackTexture);
gl.DeleteFramebuffers(1, &mBackFBO);
}
void NativeSwapChainImpl::Init(DawnWSIContextGL* /*context*/) {
const OpenGLFunctions& gl = mDevice->gl;
const OpenGLFunctions& gl = mDevice->GetGL();
gl.GenTextures(1, &mBackTexture);
gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
@ -53,7 +53,7 @@ DawnSwapChainError NativeSwapChainImpl::Configure(WGPUTextureFormat format,
mWidth = width;
mHeight = height;
const OpenGLFunctions& gl = mDevice->gl;
const OpenGLFunctions& gl = mDevice->GetGL();
gl.BindTexture(GL_TEXTURE_2D, mBackTexture);
// Reallocate the texture
gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
@ -67,7 +67,7 @@ DawnSwapChainError NativeSwapChainImpl::GetNextTexture(DawnSwapChainNextTexture*
}
DawnSwapChainError NativeSwapChainImpl::Present() {
const OpenGLFunctions& gl = mDevice->gl;
const OpenGLFunctions& gl = mDevice->GetGL();
gl.BindFramebuffer(GL_READ_FRAMEBUFFER, mBackFBO);
gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
gl.Scissor(0, 0, mWidth, mHeight);

View File

@ -42,7 +42,7 @@ MaybeError Queue::WriteBufferImpl(BufferBase* buffer,
uint64_t bufferOffset,
const void* data,
size_t size) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
ToBackend(buffer)->EnsureDataInitializedAsDestination(bufferOffset, size);
@ -70,7 +70,7 @@ MaybeError Queue::WriteTextureImpl(const ImageCopyTexture& destination,
} else {
ToBackend(destination.texture)->EnsureSubresourceContentInitialized(range);
}
DoTexSubImage(ToBackend(GetDevice())->gl, textureCopy, data, dataLayout, writeSizePixel);
DoTexSubImage(ToBackend(GetDevice())->GetGL(), textureCopy, data, dataLayout, writeSizePixel);
ToBackend(destination.texture)->Touch();
return {};
}

View File

@ -226,7 +226,8 @@ RenderPipeline::RenderPipeline(Device* device, const RenderPipelineDescriptor* d
mGlPrimitiveTopology(GLPrimitiveTopology(GetPrimitiveTopology())) {}
MaybeError RenderPipeline::Initialize() {
DAWN_TRY(InitializeBase(ToBackend(GetDevice())->gl, ToBackend(GetLayout()), GetAllStages()));
DAWN_TRY(
InitializeBase(ToBackend(GetDevice())->GetGL(), ToBackend(GetLayout()), GetAllStages()));
CreateVAOForVertexState();
return {};
}
@ -235,7 +236,7 @@ RenderPipeline::~RenderPipeline() = default;
void RenderPipeline::DestroyImpl() {
RenderPipelineBase::DestroyImpl();
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.DeleteVertexArrays(1, &mVertexArrayObject);
gl.BindVertexArray(0);
DeleteProgram(gl);
@ -252,7 +253,7 @@ RenderPipeline::GetAttributesUsingVertexBuffer(VertexBufferSlot slot) const {
}
void RenderPipeline::CreateVAOForVertexState() {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.GenVertexArrays(1, &mVertexArrayObject);
gl.BindVertexArray(mVertexArrayObject);
@ -284,7 +285,7 @@ void RenderPipeline::CreateVAOForVertexState() {
}
void RenderPipeline::ApplyNow(PersistentPipelineState& persistentPipelineState) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
PipelineGL::ApplyNow(gl);
ASSERT(mVertexArrayObject);

View File

@ -67,7 +67,7 @@ GLenum WrapMode(wgpu::AddressMode mode) {
Sampler::Sampler(Device* device, const SamplerDescriptor* descriptor)
: SamplerBase(device, descriptor) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.GenSamplers(1, &mFilteringHandle);
SetupGLSampler(mFilteringHandle, descriptor, false);
@ -80,7 +80,7 @@ Sampler::~Sampler() = default;
void Sampler::DestroyImpl() {
SamplerBase::DestroyImpl();
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.DeleteSamplers(1, &mFilteringHandle);
gl.DeleteSamplers(1, &mNonFilteringHandle);
}
@ -89,7 +89,7 @@ void Sampler::SetupGLSampler(GLuint sampler,
const SamplerDescriptor* descriptor,
bool forceNearest) {
Device* device = ToBackend(GetDevice());
const OpenGLFunctions& gl = device->gl;
const OpenGLFunctions& gl = device->GetGL();
if (forceNearest) {
gl.SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

View File

@ -95,7 +95,7 @@ ResultOrError<std::string> ShaderModule::TranslateToGLSL(const char* entryPointN
tint::Program program;
DAWN_TRY_ASSIGN(program, RunTransforms(&transformManager, GetTintProgram(), transformInputs,
nullptr, nullptr));
const OpenGLVersion& version = ToBackend(GetDevice())->gl.GetVersion();
const OpenGLVersion& version = ToBackend(GetDevice())->GetGL().GetVersion();
tint::writer::glsl::Options tintOptions;
using Version = tint::writer::glsl::Version;

View File

@ -83,12 +83,6 @@ GLenum TargetForTextureViewDimension(wgpu::TextureViewDimension dimension,
UNREACHABLE();
}
GLuint GenTexture(const OpenGLFunctions& gl) {
GLuint handle = 0;
gl.GenTextures(1, &handle);
return handle;
}
bool RequiresCreatingNewTextureView(const TextureBase* texture,
const TextureViewDescriptor* textureViewDescriptor) {
constexpr wgpu::TextureUsage kShaderUsageNeedsView =
@ -179,9 +173,10 @@ void AllocateTexture(const OpenGLFunctions& gl,
// Texture
Texture::Texture(Device* device, const TextureDescriptor* descriptor)
: Texture(device, descriptor, GenTexture(device->gl), TextureState::OwnedInternal) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
: Texture(device, descriptor, 0, TextureState::OwnedInternal) {
const OpenGLFunctions& gl = device->GetGL();
gl.GenTextures(1, &mHandle);
uint32_t levels = GetNumMipLevels();
const GLFormat& glFormat = GetGLFormat();
@ -221,7 +216,8 @@ Texture::~Texture() {}
void Texture::DestroyImpl() {
TextureBase::DestroyImpl();
if (GetTextureState() == TextureState::OwnedInternal) {
ToBackend(GetDevice())->gl.DeleteTextures(1, &mHandle);
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.DeleteTextures(1, &mHandle);
mHandle = 0;
}
}
@ -246,7 +242,7 @@ MaybeError Texture::ClearTexture(const SubresourceRange& range,
}
Device* device = ToBackend(GetDevice());
const OpenGLFunctions& gl = device->gl;
const OpenGLFunctions& gl = device->GetGL();
uint8_t clearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0 : 1;
float fClearColor = (clearValue == TextureBase::ClearValue::Zero) ? 0.f : 1.f;
@ -534,7 +530,7 @@ MaybeError Texture::ClearTexture(const SubresourceRange& range,
}
textureCopy.origin.z = layer;
DoTexSubImage(ToBackend(GetDevice())->gl, textureCopy, 0, dataLayout, mipSize);
DoTexSubImage(gl, textureCopy, 0, dataLayout, mipSize);
}
}
gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
@ -571,9 +567,9 @@ TextureView::TextureView(TextureBase* texture, const TextureViewDescriptor* desc
if (!RequiresCreatingNewTextureView(texture, descriptor)) {
mHandle = ToBackend(texture)->GetHandle();
} else {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
if (gl.IsAtLeastGL(4, 3)) {
mHandle = GenTexture(gl);
gl.GenTextures(1, &mHandle);
const Texture* textureGL = ToBackend(texture);
gl.TextureView(mHandle, mTarget, textureGL->GetHandle(), GetInternalFormat(),
descriptor->baseMipLevel, descriptor->mipLevelCount,
@ -592,7 +588,8 @@ TextureView::~TextureView() {}
void TextureView::DestroyImpl() {
TextureViewBase::DestroyImpl();
if (mOwnsHandle) {
ToBackend(GetDevice())->gl.DeleteTextures(1, &mHandle);
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
gl.DeleteTextures(1, &mHandle);
}
}
@ -606,7 +603,7 @@ GLenum TextureView::GetGLTarget() const {
}
void TextureView::BindToFramebuffer(GLenum target, GLenum attachment) {
const OpenGLFunctions& gl = ToBackend(GetDevice())->gl;
const OpenGLFunctions& gl = ToBackend(GetDevice())->GetGL();
// Use the base texture where possible to minimize the amount of copying required on GLES.
bool useOwnView = GetFormat().format != GetTexture()->GetFormat().format &&
@ -648,7 +645,7 @@ void TextureView::CopyIfNeeded() {
}
Device* device = ToBackend(GetDevice());
const OpenGLFunctions& gl = device->gl;
const OpenGLFunctions& gl = device->GetGL();
uint32_t srcLevel = GetBaseMipLevel();
uint32_t numLevels = GetLevelCount();
@ -657,7 +654,7 @@ void TextureView::CopyIfNeeded() {
Extent3D size{width, height, GetLayerCount()};
if (mHandle == 0) {
mHandle = GenTexture(gl);
gl.GenTextures(1, &mHandle);
gl.BindTexture(mTarget, mHandle);
AllocateTexture(gl, mTarget, texture->GetSampleCount(), numLevels, GetInternalFormat(),
size);

View File

@ -128,7 +128,7 @@ class EGLImageTestBase : public DawnTest {
size_t size) {
dawn::native::opengl::Device* openglDevice =
dawn::native::opengl::ToBackend(dawn::native::FromAPI(device.Get()));
const dawn::native::opengl::OpenGLFunctions& gl = openglDevice->gl;
const dawn::native::opengl::OpenGLFunctions& gl = openglDevice->GetGL();
GLuint tex;
gl.GenTextures(1, &tex);
gl.BindTexture(GL_TEXTURE_2D, tex);
@ -300,7 +300,7 @@ class EGLImageUsageTests : public EGLImageTestBase {
size_t dataSize) {
dawn::native::opengl::Device* openglDevice =
dawn::native::opengl::ToBackend(dawn::native::FromAPI(device.Get()));
const dawn::native::opengl::OpenGLFunctions& gl = openglDevice->gl;
const dawn::native::opengl::OpenGLFunctions& gl = openglDevice->GetGL();
// Get a texture view for the eglImage
wgpu::TextureDescriptor textureDescriptor;