Hash :
405d4cf3
Author :
Date :
2019-11-09T02:28:49
Metal: multiple bug fixes - ContextMtl: triangle fan draws should call setupDraw() with original parameters. Not the modified parameters. - SurfaceMtl: should initialize metal layer's drawableSize after layer's creation. - TextureMtl & FrameBufferMtl: Fix texture copySubImage CPU path incorrectly copied unflipped area. - mtl_render_utils: Fix wrong variable name used for trifan compute pipeline cache table. - mtl_resources: Fix texture & buffer memory leaks due to missing ANGLE_MTL_AUTORELEASE. - mtl_utils: Fix viewport flipping error due to arithmetic between unsigned & signed values. These bugs were discovered during dEQP tests running. Bug: angleproject:2634 Change-Id: Ie01380910ab68a2b876718d9dac0b5b4c41b607c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1906608 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jonah Ryan-Davis <jonahr@google.com>
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
//
// Copyright 2019 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.
//
// mtl_utils.mm:
// Implements utilities functions that create Metal shaders, convert from angle enums
// to Metal enums and so on.
//
#include "libANGLE/renderer/metal/mtl_utils.h"
#include <TargetConditionals.h>
#include "common/MemoryBuffer.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
namespace rx
{
namespace mtl
{
angle::Result InitializeTextureContents(const gl::Context *context,
const TextureRef &texture,
const Format &textureObjFormat,
const gl::ImageIndex &index)
{
ASSERT(texture && texture->valid());
ASSERT(texture->textureType() == MTLTextureType2D ||
texture->textureType() == MTLTextureTypeCube);
ContextMtl *contextMtl = mtl::GetImpl(context);
const gl::InternalFormat &intendedInternalFormat = textureObjFormat.intendedInternalFormat();
// This function is called in many places to initialize the content of a texture.
// So it's better we do the sanity check here instead of let the callers do it themselves:
if (!textureObjFormat.valid() || intendedInternalFormat.compressed ||
intendedInternalFormat.depthBits > 0 || intendedInternalFormat.stencilBits > 0)
{
return angle::Result::Continue;
}
gl::Extents size = texture->size(index);
// Intialize the content to black
const angle::Format &srcFormat =
angle::Format::Get(intendedInternalFormat.alphaBits > 0 ? angle::FormatID::R8G8B8A8_UNORM
: angle::FormatID::R8G8B8_UNORM);
const size_t srcRowPitch = srcFormat.pixelBytes * size.width;
angle::MemoryBuffer srcRow;
ANGLE_CHECK_GL_ALLOC(contextMtl, srcRow.resize(srcRowPitch));
memset(srcRow.data(), 0, srcRowPitch);
const angle::Format &dstFormat = angle::Format::Get(textureObjFormat.actualFormatId);
const size_t dstRowPitch = dstFormat.pixelBytes * size.width;
angle::MemoryBuffer conversionRow;
ANGLE_CHECK_GL_ALLOC(contextMtl, conversionRow.resize(dstRowPitch));
CopyImageCHROMIUM(srcRow.data(), srcRowPitch, srcFormat.pixelBytes, 0,
srcFormat.pixelReadFunction, conversionRow.data(), dstRowPitch,
dstFormat.pixelBytes, 0, dstFormat.pixelWriteFunction,
intendedInternalFormat.format, dstFormat.componentType, size.width, 1, 1,
false, false, false);
auto mtlRowRegion = MTLRegionMake2D(0, 0, size.width, 1);
for (NSUInteger r = 0; r < static_cast<NSUInteger>(size.height); ++r)
{
mtlRowRegion.origin.y = r;
// Upload to texture
texture->replaceRegion(contextMtl, mtlRowRegion, index.getLevelIndex(),
index.hasLayer() ? index.cubeMapFaceIndex() : 0,
conversionRow.data(), dstRowPitch);
}
return angle::Result::Continue;
}
MTLViewport GetViewport(const gl::Rectangle &rect, double znear, double zfar)
{
MTLViewport re;
re.originX = rect.x;
re.originY = rect.y;
re.width = rect.width;
re.height = rect.height;
re.znear = znear;
re.zfar = zfar;
return re;
}
MTLViewport GetViewportFlipY(const gl::Rectangle &rect,
NSUInteger screenHeight,
double znear,
double zfar)
{
MTLViewport re;
re.originX = rect.x;
re.originY = static_cast<double>(screenHeight) - rect.y1();
re.width = rect.width;
re.height = rect.height;
re.znear = znear;
re.zfar = zfar;
return re;
}
MTLViewport GetViewport(const gl::Rectangle &rect,
NSUInteger screenHeight,
bool flipY,
double znear,
double zfar)
{
if (flipY)
{
return GetViewportFlipY(rect, screenHeight, znear, zfar);
}
return GetViewport(rect, znear, zfar);
}
MTLScissorRect GetScissorRect(const gl::Rectangle &rect, NSUInteger screenHeight, bool flipY)
{
MTLScissorRect re;
re.x = rect.x;
re.y = flipY ? (screenHeight - rect.y1()) : rect.y;
re.width = rect.width;
re.height = rect.height;
return re;
}
AutoObjCPtr<id<MTLLibrary>> CreateShaderLibrary(id<MTLDevice> metalDevice,
const std::string &source,
AutoObjCPtr<NSError *> *error)
{
return CreateShaderLibrary(metalDevice, source.c_str(), source.size(), error);
}
AutoObjCPtr<id<MTLLibrary>> CreateShaderLibrary(id<MTLDevice> metalDevice,
const char *source,
size_t sourceLen,
AutoObjCPtr<NSError *> *errorOut)
{
ANGLE_MTL_OBJC_SCOPE
{
NSError *nsError = nil;
auto nsSource = [[NSString alloc] initWithBytesNoCopy:const_cast<char *>(source)
length:sourceLen
encoding:NSUTF8StringEncoding
freeWhenDone:NO];
auto options = [[[MTLCompileOptions alloc] init] ANGLE_MTL_AUTORELEASE];
auto library = [metalDevice newLibraryWithSource:nsSource options:options error:&nsError];
[nsSource ANGLE_MTL_AUTORELEASE];
*errorOut = std::move(nsError);
return [library ANGLE_MTL_AUTORELEASE];
}
}
AutoObjCPtr<id<MTLLibrary>> CreateShaderLibraryFromBinary(id<MTLDevice> metalDevice,
const uint8_t *binarySource,
size_t binarySourceLen,
AutoObjCPtr<NSError *> *errorOut)
{
ANGLE_MTL_OBJC_SCOPE
{
NSError *nsError = nil;
auto shaderSourceData =
dispatch_data_create(binarySource, binarySourceLen, dispatch_get_main_queue(),
^{
});
auto library = [metalDevice newLibraryWithData:shaderSourceData error:&nsError];
[shaderSourceData ANGLE_MTL_AUTORELEASE];
*errorOut = std::move(nsError);
return [library ANGLE_MTL_AUTORELEASE];
}
}
MTLTextureType GetTextureType(gl::TextureType glType)
{
switch (glType)
{
case gl::TextureType::_2D:
return MTLTextureType2D;
case gl::TextureType::CubeMap:
return MTLTextureTypeCube;
default:
return MTLTextureTypeInvalid;
}
}
MTLSamplerMinMagFilter GetFilter(GLenum filter)
{
switch (filter)
{
case GL_LINEAR_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_LINEAR:
return MTLSamplerMinMagFilterLinear;
case GL_NEAREST_MIPMAP_LINEAR:
case GL_NEAREST_MIPMAP_NEAREST:
case GL_NEAREST:
return MTLSamplerMinMagFilterNearest;
default:
UNIMPLEMENTED();
return MTLSamplerMinMagFilterNearest;
}
}
MTLSamplerMipFilter GetMipmapFilter(GLenum filter)
{
switch (filter)
{
case GL_LINEAR:
case GL_NEAREST:
return MTLSamplerMipFilterNotMipmapped;
case GL_LINEAR_MIPMAP_LINEAR:
case GL_NEAREST_MIPMAP_LINEAR:
return MTLSamplerMipFilterLinear;
case GL_NEAREST_MIPMAP_NEAREST:
case GL_LINEAR_MIPMAP_NEAREST:
return MTLSamplerMipFilterNearest;
default:
UNIMPLEMENTED();
return MTLSamplerMipFilterNotMipmapped;
}
}
MTLSamplerAddressMode GetSamplerAddressMode(GLenum wrap)
{
switch (wrap)
{
case GL_REPEAT:
return MTLSamplerAddressModeRepeat;
case GL_MIRRORED_REPEAT:
return MTLSamplerAddressModeMirrorRepeat;
case GL_CLAMP_TO_BORDER:
// ES doesn't have border support
return MTLSamplerAddressModeClampToEdge;
case GL_CLAMP_TO_EDGE:
return MTLSamplerAddressModeClampToEdge;
default:
UNIMPLEMENTED();
return MTLSamplerAddressModeClampToEdge;
}
}
MTLBlendFactor GetBlendFactor(GLenum factor)
{
switch (factor)
{
case GL_ZERO:
return MTLBlendFactorZero;
case GL_ONE:
return MTLBlendFactorOne;
case GL_SRC_COLOR:
return MTLBlendFactorSourceColor;
case GL_DST_COLOR:
return MTLBlendFactorDestinationColor;
case GL_ONE_MINUS_SRC_COLOR:
return MTLBlendFactorOneMinusSourceColor;
case GL_SRC_ALPHA:
return MTLBlendFactorSourceAlpha;
case GL_ONE_MINUS_SRC_ALPHA:
return MTLBlendFactorOneMinusSourceAlpha;
case GL_DST_ALPHA:
return MTLBlendFactorDestinationAlpha;
case GL_ONE_MINUS_DST_ALPHA:
return MTLBlendFactorOneMinusDestinationAlpha;
case GL_ONE_MINUS_DST_COLOR:
return MTLBlendFactorOneMinusDestinationColor;
case GL_SRC_ALPHA_SATURATE:
return MTLBlendFactorSourceAlphaSaturated;
case GL_CONSTANT_COLOR:
return MTLBlendFactorBlendColor;
case GL_CONSTANT_ALPHA:
return MTLBlendFactorBlendAlpha;
case GL_ONE_MINUS_CONSTANT_COLOR:
return MTLBlendFactorOneMinusBlendColor;
case GL_ONE_MINUS_CONSTANT_ALPHA:
return MTLBlendFactorOneMinusBlendAlpha;
default:
UNREACHABLE();
return MTLBlendFactorZero;
}
}
MTLBlendOperation GetBlendOp(GLenum op)
{
switch (op)
{
case GL_FUNC_ADD:
return MTLBlendOperationAdd;
case GL_FUNC_SUBTRACT:
return MTLBlendOperationSubtract;
case GL_FUNC_REVERSE_SUBTRACT:
return MTLBlendOperationReverseSubtract;
case GL_MIN:
return MTLBlendOperationMin;
case GL_MAX:
return MTLBlendOperationMax;
default:
UNREACHABLE();
return MTLBlendOperationAdd;
}
}
MTLCompareFunction GetCompareFunc(GLenum func)
{
switch (func)
{
case GL_NEVER:
return MTLCompareFunctionNever;
case GL_ALWAYS:
return MTLCompareFunctionAlways;
case GL_LESS:
return MTLCompareFunctionLess;
case GL_LEQUAL:
return MTLCompareFunctionLessEqual;
case GL_EQUAL:
return MTLCompareFunctionEqual;
case GL_GREATER:
return MTLCompareFunctionGreater;
case GL_GEQUAL:
return MTLCompareFunctionGreaterEqual;
case GL_NOTEQUAL:
return MTLCompareFunctionNotEqual;
default:
UNREACHABLE();
return MTLCompareFunctionAlways;
}
}
MTLStencilOperation GetStencilOp(GLenum op)
{
switch (op)
{
case GL_KEEP:
return MTLStencilOperationKeep;
case GL_ZERO:
return MTLStencilOperationZero;
case GL_REPLACE:
return MTLStencilOperationReplace;
case GL_INCR:
return MTLStencilOperationIncrementClamp;
case GL_DECR:
return MTLStencilOperationDecrementClamp;
case GL_INCR_WRAP:
return MTLStencilOperationIncrementWrap;
case GL_DECR_WRAP:
return MTLStencilOperationDecrementWrap;
case GL_INVERT:
return MTLStencilOperationInvert;
default:
UNREACHABLE();
return MTLStencilOperationKeep;
}
}
MTLWinding GetFontfaceWinding(GLenum frontFaceMode, bool invert)
{
switch (frontFaceMode)
{
case GL_CW:
return invert ? MTLWindingCounterClockwise : MTLWindingClockwise;
case GL_CCW:
return invert ? MTLWindingClockwise : MTLWindingCounterClockwise;
default:
UNREACHABLE();
return MTLWindingClockwise;
}
}
#if ANGLE_MTL_PRIMITIVE_TOPOLOGY_CLASS_AVAILABLE
PrimitiveTopologyClass GetPrimitiveTopologyClass(gl::PrimitiveMode mode)
{
// NOTE(hqle): Support layered renderring in future.
// In non-layered rendering mode, unspecified is enough.
return MTLPrimitiveTopologyClassUnspecified;
}
#else // ANGLE_MTL_PRIMITIVE_TOPOLOGY_CLASS_AVAILABLE
PrimitiveTopologyClass GetPrimitiveTopologyClass(gl::PrimitiveMode mode)
{
return kPrimitiveTopologyClassTriangle;
}
#endif
MTLPrimitiveType GetPrimitiveType(gl::PrimitiveMode mode)
{
switch (mode)
{
case gl::PrimitiveMode::Triangles:
return MTLPrimitiveTypeTriangle;
case gl::PrimitiveMode::Points:
return MTLPrimitiveTypePoint;
case gl::PrimitiveMode::Lines:
return MTLPrimitiveTypeLine;
case gl::PrimitiveMode::LineStrip:
case gl::PrimitiveMode::LineLoop:
return MTLPrimitiveTypeLineStrip;
case gl::PrimitiveMode::TriangleStrip:
return MTLPrimitiveTypeTriangleStrip;
case gl::PrimitiveMode::TriangleFan:
// NOTE(hqle): Emulate triangle fan.
default:
return MTLPrimitiveTypeInvalid;
}
}
MTLIndexType GetIndexType(gl::DrawElementsType type)
{
switch (type)
{
case gl::DrawElementsType::UnsignedShort:
return MTLIndexTypeUInt16;
case gl::DrawElementsType::UnsignedInt:
return MTLIndexTypeUInt32;
case gl::DrawElementsType::UnsignedByte:
// NOTE(hqle): Convert to supported type
default:
return MTLIndexTypeInvalid;
}
}
MTLClearColor EmulatedAlphaClearColor(MTLClearColor color, MTLColorWriteMask colorMask)
{
MTLClearColor re = color;
if (!(colorMask & MTLColorWriteMaskAlpha))
{
re.alpha = kEmulatedAlphaValue;
}
return re;
}
} // namespace mtl
} // namespace rx