1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MLGDevice.h"
#include "mozilla/layers/TextureHost.h"
#include "BufferCache.h"
#include "ClearRegionHelper.h"
#include "gfxConfig.h"
#include "mozilla/StaticPrefs_layers.h"
#include "gfxUtils.h"
#include "ShaderDefinitionsMLGPU.h"
#include "SharedBufferMLGPU.h"
#include "UtilityMLGPU.h"
namespace mozilla {
namespace layers {
using namespace gfx;
using namespace mlg;
MLGRenderTarget::MLGRenderTarget(MLGRenderTargetFlags aFlags)
: mFlags(aFlags), mLastDepthStart(-1) {}
MLGSwapChain::MLGSwapChain() : mIsDoubleBuffered(false) {}
bool MLGSwapChain::ApplyNewInvalidRegion(
nsIntRegion&& aRegion, const Maybe<gfx::IntRect>& aExtraRect) {
// We clamp the invalid region to the backbuffer size, otherwise the present
// can fail.
IntRect bounds(IntPoint(0, 0), GetSize());
nsIntRegion invalid = std::move(aRegion);
invalid.AndWith(bounds);
if (invalid.IsEmpty()) {
return false;
}
if (aExtraRect) {
IntRect rect = aExtraRect.value().Intersect(bounds);
if (!rect.IsEmpty()) {
invalid.OrWith(rect);
}
}
// This area is now invalid in the back and front buffers. Note that the front
// buffer is either totally valid or totally invalid, since either the last
// paint succeeded or was thrown out due to a buffer resize. Effectively, it
// will now contain the invalid region specific to this frame.
mBackBufferInvalid.OrWith(invalid);
AL_LOG("Backbuffer invalid region: %s\n",
Stringify(mBackBufferInvalid).c_str());
if (mIsDoubleBuffered) {
mFrontBufferInvalid.OrWith(invalid);
AL_LOG("Frontbuffer invalid region: %s\n",
Stringify(mFrontBufferInvalid).c_str());
}
return true;
}
MLGDevice::MLGDevice()
: mTopology(MLGPrimitiveTopology::Unknown),
mInitialized(false),
mIsValid(false),
mCanUseClearView(false),
mCanUseConstantBufferOffsetBinding(false),
mMaxConstantBufferBindSize(0) {}
MLGDevice::~MLGDevice() = default;
bool MLGDevice::Initialize() {
if (!mMaxConstantBufferBindSize) {
return Fail("FEATURE_FAILURE_NO_MAX_CB_BIND_SIZE",
"Failed to set a max constant buffer bind size");
}
if (mMaxConstantBufferBindSize < mlg::kMaxConstantBufferSize) {
// StagingBuffer depends on this value being accurate, so for now we just
// double-check it here.
return Fail("FEATURE_FAILURE_MIN_MAX_CB_BIND_SIZE",
"Minimum constant buffer bind size not met");
}
// We allow this to be pref'd off for testing. Switching it off enables
// Direct3D 11.0/Windows 7/OpenGL-style buffer code paths.
if (!StaticPrefs::layers_mlgpu_enable_buffer_sharing_AtStartup()) {
gfxConfig::EnableFallback(Fallback::NO_CONSTANT_BUFFER_OFFSETTING,
"Disabled by pref");
mCanUseConstantBufferOffsetBinding = false;
}
if (mCanUseConstantBufferOffsetBinding && !VerifyConstantBufferOffsetting()) {
gfxConfig::EnableFallback(Fallback::NO_CONSTANT_BUFFER_OFFSETTING,
"Constant buffer offset binding does not work");
mCanUseConstantBufferOffsetBinding = false;
}
// We allow this to be pref'd off for testing. Disabling it turns on
// ID3D11DeviceContext1::ClearView support, which is present on
// newer Windows 8+ drivers.
if (!StaticPrefs::layers_mlgpu_enable_clear_view_AtStartup()) {
mCanUseClearView = false;
}
// When compositing normal sized layer trees, we typically have small vertex
// buffers. Empirically the vertex and pixel constant buffer sizes are
// generally under 1KB and the vertex constant buffer size is under 8KB.
static const size_t kDefaultVertexBufferSize = 4096;
static const size_t kDefaultVSConstantBufferSize =
512 * kConstantBufferElementSize;
static const size_t kDefaultPSConstantBufferSize =
256 * kConstantBufferElementSize;
// Note: we create these after we've verified all the device-specific
// properties above.
mSharedVertexBuffer =
MakeUnique<SharedVertexBuffer>(this, kDefaultVertexBufferSize);
mSharedVSBuffer =
MakeUnique<SharedConstantBuffer>(this, kDefaultVSConstantBufferSize);
mSharedPSBuffer =
MakeUnique<SharedConstantBuffer>(this, kDefaultPSConstantBufferSize);
if (!mSharedVertexBuffer->Init() || !mSharedVSBuffer->Init() ||
!mSharedPSBuffer->Init()) {
return Fail("FEATURE_FAILURE_ALLOC_SHARED_BUFFER",
"Failed to allocate a shared shader buffer");
}
if (StaticPrefs::layers_mlgpu_enable_buffer_cache_AtStartup()) {
mConstantBufferCache = MakeUnique<BufferCache>(this);
}
mInitialized = true;
mIsValid = true;
return true;
}
void MLGDevice::BeginFrame() {
mSharedVertexBuffer->Reset();
mSharedPSBuffer->Reset();
mSharedVSBuffer->Reset();
}
void MLGDevice::EndFrame() {
if (mConstantBufferCache) {
mConstantBufferCache->EndFrame();
}
}
void MLGDevice::FinishSharedBufferUse() {
mSharedVertexBuffer->PrepareForUsage();
mSharedPSBuffer->PrepareForUsage();
mSharedVSBuffer->PrepareForUsage();
}
void MLGDevice::SetTopology(MLGPrimitiveTopology aTopology) {
if (mTopology == aTopology) {
return;
}
SetPrimitiveTopology(aTopology);
mTopology = aTopology;
}
void MLGDevice::SetVertexBuffer(uint32_t aSlot,
const VertexBufferSection* aSection) {
if (!aSection->IsValid()) {
return;
}
SetVertexBuffer(aSlot, aSection->GetBuffer(), aSection->Stride(),
aSection->Offset());
}
void MLGDevice::SetPSConstantBuffer(uint32_t aSlot,
const ConstantBufferSection* aSection) {
if (!aSection->IsValid()) {
return;
}
MLGBuffer* buffer = aSection->GetBuffer();
if (aSection->HasOffset()) {
uint32_t first = aSection->Offset();
uint32_t numConstants = aSection->NumConstants();
SetPSConstantBuffer(aSlot, buffer, first, numConstants);
} else {
SetPSConstantBuffer(aSlot, buffer);
}
}
void MLGDevice::SetVSConstantBuffer(uint32_t aSlot,
const ConstantBufferSection* aSection) {
if (!aSection->IsValid()) {
return;
}
MLGBuffer* buffer = aSection->GetBuffer();
if (aSection->HasOffset()) {
uint32_t first = aSection->Offset();
uint32_t numConstants = aSection->NumConstants();
SetVSConstantBuffer(aSlot, buffer, first, numConstants);
} else {
SetVSConstantBuffer(aSlot, buffer);
}
}
void MLGDevice::SetPSTexturesYUV(uint32_t aSlot, TextureSource* aTexture) {
// Note, we don't support tiled YCbCr textures.
const int Y = 0, Cb = 1, Cr = 2;
TextureSource* textures[3] = {aTexture->GetSubSource(Y),
aTexture->GetSubSource(Cb),
aTexture->GetSubSource(Cr)};
MOZ_ASSERT(textures[0]);
MOZ_ASSERT(textures[1]);
MOZ_ASSERT(textures[2]);
SetPSTextures(0, 3, textures);
}
void MLGDevice::SetPSTexture(uint32_t aSlot, TextureSource* aSource) {
SetPSTextures(aSlot, 1, &aSource);
}
void MLGDevice::SetSamplerMode(uint32_t aIndex, gfx::SamplingFilter aFilter) {
SetSamplerMode(aIndex, FilterToSamplerMode(aFilter));
}
bool MLGDevice::Fail(const nsCString& aFailureId, const nsCString* aMessage) {
const char* message =
aMessage ? aMessage->get() : "Failed initializing MLGDeviceD3D11";
gfxWarning() << "Failure initializing MLGDeviceD3D11: " << message;
mFailureId = aFailureId;
mFailureMessage = message;
return false;
}
void MLGDevice::UnmapSharedBuffers() {
mSharedVertexBuffer->Reset();
mSharedPSBuffer->Reset();
mSharedVSBuffer->Reset();
}
RefPtr<MLGBuffer> MLGDevice::GetBufferForColorSpace(YUVColorSpace aColorSpace) {
if (mColorSpaceBuffers[aColorSpace]) {
return mColorSpaceBuffers[aColorSpace];
}
YCbCrShaderConstants buffer;
memcpy(&buffer.yuvColorMatrix,
gfxUtils::YuvToRgbMatrix4x3RowMajor(aColorSpace),
sizeof(buffer.yuvColorMatrix));
RefPtr<MLGBuffer> resource = CreateBuffer(
MLGBufferType::Constant, sizeof(buffer), MLGUsage::Immutable, &buffer);
if (!resource) {
return nullptr;
}
mColorSpaceBuffers[aColorSpace] = resource;
return resource;
}
RefPtr<MLGBuffer> MLGDevice::GetBufferForColorDepthCoefficient(
ColorDepth aColorDepth) {
if (mColorDepthBuffers[aColorDepth]) {
return mColorDepthBuffers[aColorDepth];
}
YCbCrColorDepthConstants buffer;
buffer.coefficient = gfx::RescalingFactorForColorDepth(aColorDepth);
RefPtr<MLGBuffer> resource = CreateBuffer(
MLGBufferType::Constant, sizeof(buffer), MLGUsage::Immutable, &buffer);
if (!resource) {
return nullptr;
}
mColorDepthBuffers[aColorDepth] = resource;
return resource;
}
bool MLGDevice::Synchronize() { return true; }
void MLGDevice::PrepareClearRegion(ClearRegionHelper* aOut,
nsTArray<gfx::IntRect>&& aRects,
const Maybe<int32_t>& aSortIndex) {
if (CanUseClearView() && !aSortIndex) {
aOut->mRects = std::move(aRects);
return;
}
mSharedVertexBuffer->Allocate(&aOut->mInput, aRects.Length(), sizeof(IntRect),
aRects.Elements());
ClearConstants consts(aSortIndex ? aSortIndex.value() : 1);
mSharedVSBuffer->Allocate(&aOut->mVSBuffer, consts);
}
void MLGDevice::DrawClearRegion(const ClearRegionHelper& aHelper) {
// If we've set up vertices for a shader-based clear, execute that now.
if (aHelper.mInput.IsValid()) {
SetTopology(MLGPrimitiveTopology::UnitQuad);
SetVertexShader(VertexShaderID::Clear);
SetVertexBuffer(1, &aHelper.mInput);
SetVSConstantBuffer(kClearConstantBufferSlot, &aHelper.mVSBuffer);
SetBlendState(MLGBlendState::Copy);
SetPixelShader(PixelShaderID::Clear);
DrawInstanced(4, aHelper.mInput.NumVertices(), 0, 0);
return;
}
// Otherwise, if we have a normal rect list, we wanted to use the faster
// ClearView.
if (!aHelper.mRects.IsEmpty()) {
DeviceColor color(0.0, 0.0, 0.0, 0.0);
ClearView(mCurrentRT, color, aHelper.mRects.Elements(),
aHelper.mRects.Length());
}
}
void MLGDevice::WriteAsPNG(MLGTexture* aTexture, const char* aPath) {
MLGMappedResource map;
if (!Map(aTexture, MLGMapType::READ, &map)) {
return;
}
RefPtr<DataSourceSurface> surface = Factory::CreateWrappingDataSourceSurface(
map.mData, map.mStride, aTexture->GetSize(), SurfaceFormat::B8G8R8A8);
gfxUtils::WriteAsPNG(surface, aPath);
Unmap(aTexture);
}
RefPtr<MLGTexture> MLGDevice::CopyAndCreateReadbackTexture(
MLGTexture* aTexture) {
RefPtr<MLGTexture> copy =
CreateTexture(aTexture->GetSize(), SurfaceFormat::B8G8R8A8,
MLGUsage::Staging, MLGTextureFlags::None);
if (!copy) {
return nullptr;
}
CopyTexture(copy, IntPoint(0, 0), aTexture,
IntRect(IntPoint(0, 0), aTexture->GetSize()));
return copy;
}
} // namespace layers
} // namespace mozilla
|