Hash :
613eefa3
Author :
Date :
2023-06-23T03:28:14
D3D11: fix invalidation of depth/stencil attachments This caused a crash when invalidating the depth/stencil attachments of the default framebuffer. But for non-default framebuffers, discarding depth/stencil just did nothing because "rtv.valid()" would be false. Bug: angleproject:8228 Change-Id: Ic22a29f521256af0ed4fc9c203cd6d750fcc00e6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4639494 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Kenneth Russell <kbr@chromium.org> Auto-Submit: Steven Noonan <steven@uplinklabs.net>
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
//
// Copyright 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Framebuffer11.cpp: Implements the Framebuffer11 class.
#include "libANGLE/renderer/d3d/d3d11/Framebuffer11.h"
#include "common/bitset_utils.h"
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/FramebufferAttachment.h"
#include "libANGLE/Texture.h"
#include "libANGLE/renderer/d3d/TextureD3D.h"
#include "libANGLE/renderer/d3d/d3d11/Buffer11.h"
#include "libANGLE/renderer/d3d/d3d11/Clear11.h"
#include "libANGLE/renderer/d3d/d3d11/Context11.h"
#include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include "libANGLE/renderer/d3d/d3d11/TextureStorage11.h"
#include "libANGLE/renderer/d3d/d3d11/formatutils11.h"
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
using namespace angle;
namespace rx
{
namespace
{
angle::Result MarkAttachmentsDirty(const gl::Context *context,
const gl::FramebufferAttachment *attachment)
{
if (attachment->type() == GL_TEXTURE)
{
gl::Texture *texture = attachment->getTexture();
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(context, &texStorage));
if (texStorage)
{
TextureStorage11 *texStorage11 = GetAs<TextureStorage11>(texStorage);
ASSERT(texStorage11);
texStorage11->markLevelDirty(attachment->mipLevel());
}
}
return angle::Result::Continue;
}
UINT GetAttachmentLayer(const gl::FramebufferAttachment *attachment)
{
if (attachment->type() == GL_TEXTURE &&
attachment->getTexture()->getType() == gl::TextureType::_3D)
{
return attachment->layer();
}
return 0;
}
} // anonymous namespace
Framebuffer11::Framebuffer11(const gl::FramebufferState &data, Renderer11 *renderer)
: FramebufferD3D(data, renderer), mRenderer(renderer)
{
ASSERT(mRenderer != nullptr);
}
Framebuffer11::~Framebuffer11() {}
angle::Result Framebuffer11::markAttachmentsDirty(const gl::Context *context) const
{
const auto &colorAttachments = mState.getColorAttachments();
for (size_t drawBuffer : mState.getEnabledDrawBuffers())
{
const gl::FramebufferAttachment &colorAttachment = colorAttachments[drawBuffer];
ASSERT(colorAttachment.isAttached());
ANGLE_TRY(MarkAttachmentsDirty(context, &colorAttachment));
}
const gl::FramebufferAttachment *dsAttachment = mState.getDepthOrStencilAttachment();
if (dsAttachment)
{
ANGLE_TRY(MarkAttachmentsDirty(context, dsAttachment));
}
return angle::Result::Continue;
}
angle::Result Framebuffer11::clearImpl(const gl::Context *context,
const ClearParameters &clearParams)
{
Clear11 *clearer = mRenderer->getClearer();
const gl::FramebufferAttachment *colorAttachment = mState.getFirstColorAttachment();
if (clearParams.scissorEnabled == true && colorAttachment != nullptr &&
UsePresentPathFast(mRenderer, colorAttachment))
{
// If the current framebuffer is using the default colorbuffer, and present path fast is
// active, and the scissor rect is enabled, then we should invert the scissor rect
// vertically
ClearParameters presentPathFastClearParams = clearParams;
gl::Extents framebufferSize = colorAttachment->getSize();
presentPathFastClearParams.scissor.y = framebufferSize.height -
presentPathFastClearParams.scissor.y -
presentPathFastClearParams.scissor.height;
ANGLE_TRY(clearer->clearFramebuffer(context, presentPathFastClearParams, mState));
}
else
{
ANGLE_TRY(clearer->clearFramebuffer(context, clearParams, mState));
}
ANGLE_TRY(markAttachmentsDirty(context));
return angle::Result::Continue;
}
angle::Result Framebuffer11::invalidate(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
return invalidateBase(context, count, attachments, false);
}
angle::Result Framebuffer11::discard(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
return invalidateBase(context, count, attachments, true);
}
angle::Result Framebuffer11::invalidateBase(const gl::Context *context,
size_t count,
const GLenum *attachments,
bool useEXTBehavior) const
{
ID3D11DeviceContext1 *deviceContext1 = mRenderer->getDeviceContext1IfSupported();
if (!deviceContext1)
{
// DiscardView() is only supported on ID3D11DeviceContext1
return angle::Result::Continue;
}
bool foundDepth = false;
bool foundStencil = false;
for (size_t i = 0; i < count; ++i)
{
switch (attachments[i])
{
// Handle depth and stencil attachments. Defer discarding until later.
case GL_DEPTH_STENCIL_ATTACHMENT:
foundDepth = true;
foundStencil = true;
break;
case GL_DEPTH_EXT:
case GL_DEPTH_ATTACHMENT:
foundDepth = true;
break;
case GL_STENCIL_EXT:
case GL_STENCIL_ATTACHMENT:
foundStencil = true;
break;
default:
{
// Handle color attachments
ASSERT((attachments[i] >= GL_COLOR_ATTACHMENT0 &&
attachments[i] <= GL_COLOR_ATTACHMENT15) ||
(attachments[i] == GL_COLOR));
size_t colorIndex =
(attachments[i] == GL_COLOR ? 0u : (attachments[i] - GL_COLOR_ATTACHMENT0));
const gl::FramebufferAttachment *colorAttachment =
mState.getColorAttachment(colorIndex);
if (colorAttachment)
{
ANGLE_TRY(invalidateAttachment(context, colorAttachment));
}
break;
}
}
}
bool discardDepth = false;
bool discardStencil = false;
// The D3D11 renderer uses the same view for depth and stencil buffers, so we must be careful.
if (useEXTBehavior)
{
// In the extension, if the app discards only one of the depth and stencil attachments, but
// those are backed by the same packed_depth_stencil buffer, then both images become
// undefined.
discardDepth = foundDepth;
// Don't bother discarding the stencil buffer if the depth buffer will already do it
discardStencil = foundStencil && (!discardDepth || mState.getDepthAttachment() == nullptr);
}
else
{
// In ES 3.0.4, if a specified attachment has base internal format DEPTH_STENCIL but the
// attachments list does not include DEPTH_STENCIL_ATTACHMENT or both DEPTH_ATTACHMENT and
// STENCIL_ATTACHMENT, then only the specified portion of every pixel in the subregion of
// pixels of the DEPTH_STENCIL buffer may be invalidated, and the other portion must be
// preserved.
discardDepth = (foundDepth && foundStencil) ||
(foundDepth && (mState.getStencilAttachment() == nullptr));
discardStencil = (foundStencil && (mState.getDepthAttachment() == nullptr));
}
if (discardDepth && mState.getDepthAttachment())
{
ANGLE_TRY(invalidateAttachment(context, mState.getDepthAttachment()));
}
if (discardStencil && mState.getStencilAttachment())
{
ANGLE_TRY(invalidateAttachment(context, mState.getStencilAttachment()));
}
return angle::Result::Continue;
}
angle::Result Framebuffer11::invalidateSub(const gl::Context *context,
size_t count,
const GLenum *attachments,
const gl::Rectangle &area)
{
// A no-op implementation conforms to the spec, so don't call UNIMPLEMENTED()
return angle::Result::Continue;
}
angle::Result Framebuffer11::invalidateAttachment(const gl::Context *context,
const gl::FramebufferAttachment *attachment) const
{
ID3D11DeviceContext1 *deviceContext1 = mRenderer->getDeviceContext1IfSupported();
ASSERT(deviceContext1);
ASSERT(attachment && attachment->isAttached());
RenderTarget11 *renderTarget = nullptr;
ANGLE_TRY(attachment->getRenderTarget(context, 0, &renderTarget));
if (attachment->getDepthSize() > 0 || attachment->getStencilSize() > 0)
{
const auto &dsv = renderTarget->getDepthStencilView();
if (dsv.valid())
{
deviceContext1->DiscardView(dsv.get());
}
return angle::Result::Continue;
}
else
{
const auto &rtv = renderTarget->getRenderTargetView();
if (rtv.valid())
{
deviceContext1->DiscardView(rtv.get());
}
return angle::Result::Continue;
}
}
angle::Result Framebuffer11::readPixelsImpl(const gl::Context *context,
const gl::Rectangle &area,
GLenum format,
GLenum type,
size_t outputPitch,
const gl::PixelPackState &pack,
gl::Buffer *packBuffer,
uint8_t *pixels)
{
const gl::FramebufferAttachment *readAttachment = mState.getReadPixelsAttachment(format);
ASSERT(readAttachment);
if (packBuffer != nullptr)
{
Buffer11 *packBufferStorage = GetImplAs<Buffer11>(packBuffer);
const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
PackPixelsParams packParams(area, angleFormat, static_cast<GLuint>(outputPitch),
pack.reverseRowOrder, packBuffer,
reinterpret_cast<ptrdiff_t>(pixels));
return packBufferStorage->packPixels(context, *readAttachment, packParams);
}
return mRenderer->readFromAttachment(context, *readAttachment, area, format, type,
static_cast<GLuint>(outputPitch), pack, pixels);
}
angle::Result Framebuffer11::blitImpl(const gl::Context *context,
const gl::Rectangle &sourceArea,
const gl::Rectangle &destArea,
const gl::Rectangle *scissor,
bool blitRenderTarget,
bool blitDepth,
bool blitStencil,
GLenum filter,
const gl::Framebuffer *sourceFramebuffer)
{
if (blitRenderTarget)
{
const gl::FramebufferAttachment *readBuffer = sourceFramebuffer->getReadColorAttachment();
ASSERT(readBuffer);
RenderTargetD3D *readRenderTarget = nullptr;
ANGLE_TRY(readBuffer->getRenderTarget(context, 0, &readRenderTarget));
ASSERT(readRenderTarget);
const auto &colorAttachments = mState.getColorAttachments();
const auto &drawBufferStates = mState.getDrawBufferStates();
UINT readLayer = GetAttachmentLayer(readBuffer);
for (size_t colorAttachment = 0; colorAttachment < colorAttachments.size();
colorAttachment++)
{
const gl::FramebufferAttachment &drawBuffer = colorAttachments[colorAttachment];
if (drawBuffer.isAttached() && drawBufferStates[colorAttachment] != GL_NONE)
{
RenderTargetD3D *drawRenderTarget = nullptr;
ANGLE_TRY(drawBuffer.getRenderTarget(
context, drawBuffer.getRenderToTextureSamples(), &drawRenderTarget));
ASSERT(drawRenderTarget);
const bool invertColorSource = UsePresentPathFast(mRenderer, readBuffer);
gl::Rectangle actualSourceArea = sourceArea;
if (invertColorSource)
{
RenderTarget11 *readRenderTarget11 = GetAs<RenderTarget11>(readRenderTarget);
actualSourceArea.y = readRenderTarget11->getHeight() - sourceArea.y;
actualSourceArea.height = -sourceArea.height;
}
const bool invertColorDest = UsePresentPathFast(mRenderer, &drawBuffer);
gl::Rectangle actualDestArea = destArea;
UINT drawLayer = GetAttachmentLayer(&drawBuffer);
const auto &surfaceTextureOffset = mState.getSurfaceTextureOffset();
actualDestArea.x = actualDestArea.x + surfaceTextureOffset.x;
actualDestArea.y = actualDestArea.y + surfaceTextureOffset.y;
if (invertColorDest)
{
RenderTarget11 *drawRenderTarget11 = GetAs<RenderTarget11>(drawRenderTarget);
actualDestArea.y = drawRenderTarget11->getHeight() - destArea.y;
actualDestArea.height = -destArea.height;
}
ANGLE_TRY(mRenderer->blitRenderbufferRect(context, actualSourceArea, actualDestArea,
readLayer, drawLayer, readRenderTarget,
drawRenderTarget, filter, scissor,
blitRenderTarget, false, false));
}
}
}
if (blitDepth || blitStencil)
{
const gl::FramebufferAttachment *readBuffer =
sourceFramebuffer->getDepthOrStencilAttachment();
ASSERT(readBuffer);
RenderTargetD3D *readRenderTarget = nullptr;
ANGLE_TRY(readBuffer->getRenderTarget(context, 0, &readRenderTarget));
ASSERT(readRenderTarget);
const bool invertSource = UsePresentPathFast(mRenderer, readBuffer);
gl::Rectangle actualSourceArea = sourceArea;
if (invertSource)
{
RenderTarget11 *readRenderTarget11 = GetAs<RenderTarget11>(readRenderTarget);
actualSourceArea.y = readRenderTarget11->getHeight() - sourceArea.y;
actualSourceArea.height = -sourceArea.height;
}
const gl::FramebufferAttachment *drawBuffer = mState.getDepthOrStencilAttachment();
ASSERT(drawBuffer);
RenderTargetD3D *drawRenderTarget = nullptr;
ANGLE_TRY(drawBuffer->getRenderTarget(context, drawBuffer->getRenderToTextureSamples(),
&drawRenderTarget));
ASSERT(drawRenderTarget);
bool invertDest = UsePresentPathFast(mRenderer, drawBuffer);
gl::Rectangle actualDestArea = destArea;
if (invertDest)
{
RenderTarget11 *drawRenderTarget11 = GetAs<RenderTarget11>(drawRenderTarget);
actualDestArea.y = drawRenderTarget11->getHeight() - destArea.y;
actualDestArea.height = -destArea.height;
}
ANGLE_TRY(mRenderer->blitRenderbufferRect(context, actualSourceArea, actualDestArea, 0, 0,
readRenderTarget, drawRenderTarget, filter,
scissor, false, blitDepth, blitStencil));
}
ANGLE_TRY(markAttachmentsDirty(context));
return angle::Result::Continue;
}
const gl::InternalFormat &Framebuffer11::getImplementationColorReadFormat(
const gl::Context *context) const
{
Context11 *context11 = GetImplAs<Context11>(context);
const Renderer11DeviceCaps &caps = context11->getRenderer()->getRenderer11DeviceCaps();
GLenum sizedFormat = mState.getReadAttachment()->getFormat().info->sizedInternalFormat;
const angle::Format &angleFormat = d3d11::Format::Get(sizedFormat, caps).format();
return gl::GetSizedInternalFormatInfo(angleFormat.fboImplementationInternalFormat);
}
angle::Result Framebuffer11::syncState(const gl::Context *context,
GLenum binding,
const gl::Framebuffer::DirtyBits &dirtyBits,
gl::Command command)
{
ANGLE_TRY(mRenderTargetCache.update(context, mState, dirtyBits));
ANGLE_TRY(FramebufferD3D::syncState(context, binding, dirtyBits, command));
// Call this last to allow the state manager to take advantage of the cached render targets.
mRenderer->getStateManager()->invalidateRenderTarget();
// Call this to syncViewport for framebuffer default parameters.
if (mState.getDefaultWidth() != 0 || mState.getDefaultHeight() != 0)
{
mRenderer->getStateManager()->invalidateViewport(context);
}
return angle::Result::Continue;
}
angle::Result Framebuffer11::getSamplePosition(const gl::Context *context,
size_t index,
GLfloat *xy) const
{
const gl::FramebufferAttachment *attachment = mState.getFirstNonNullAttachment();
ASSERT(attachment);
GLsizei sampleCount = attachment->getSamples();
rx::GetSamplePosition(sampleCount, index, xy);
return angle::Result::Continue;
}
RenderTarget11 *Framebuffer11::getFirstRenderTarget() const
{
for (auto *renderTarget : mRenderTargetCache.getColors())
{
if (renderTarget)
{
return renderTarget;
}
}
return mRenderTargetCache.getDepthStencil();
}
} // namespace rx