Hash :
d1860ea1
Author :
Date :
2019-11-19T23:04:00
Metal: support OES_depth_texture Also added Depth32 & Depth16 texture data upload tests. Bug: angleproject:2634 Change-Id: I103f1cda1dc915f0dc8b04f7aaa2d8c0f9220cda Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1919281 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org>
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
//
// 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_command_buffer.h:
// Defines the wrapper classes for Metal's MTLCommandEncoder, MTLCommandQueue and
// MTLCommandBuffer.
//
#ifndef LIBANGLE_RENDERER_METAL_COMMANDENBUFFERMTL_H_
#define LIBANGLE_RENDERER_METAL_COMMANDENBUFFERMTL_H_
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
#include <deque>
#include <memory>
#include <mutex>
#include <thread>
#include <unordered_set>
#include <vector>
#include "common/FixedVector.h"
#include "common/angleutils.h"
#include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/mtl_resources.h"
#include "libANGLE/renderer/metal/mtl_state_cache.h"
namespace rx
{
namespace mtl
{
class CommandBuffer;
class CommandEncoder;
class RenderCommandEncoder;
class CommandQueue final : public WrappedObject<id<MTLCommandQueue>>, angle::NonCopyable
{
public:
void reset();
void set(id<MTLCommandQueue> metalQueue);
void finishAllCommands();
// This method will ensure that every GPU command buffer using this resource will finish before
// returning. Note: this doesn't include the "in-progress" command buffer, i.e. the one hasn't
// been commmitted yet. It's the responsibility of caller to make sure that command buffer is
// commited/flushed first before calling this method.
void ensureResourceReadyForCPU(const ResourceRef &resource);
void ensureResourceReadyForCPU(Resource *resource);
// Check whether the resource is being used by any command buffer still running on GPU.
// This must be called before attempting to read the content of resource on CPU side.
bool isResourceBeingUsedByGPU(const ResourceRef &resource) const
{
return isResourceBeingUsedByGPU(resource.get());
}
bool isResourceBeingUsedByGPU(const Resource *resource) const;
CommandQueue &operator=(id<MTLCommandQueue> metalQueue)
{
set(metalQueue);
return *this;
}
AutoObjCPtr<id<MTLCommandBuffer>> makeMetalCommandBuffer(uint64_t *queueSerialOut);
private:
void onCommandBufferCompleted(id<MTLCommandBuffer> buf, uint64_t serial);
using ParentClass = WrappedObject<id<MTLCommandQueue>>;
struct CmdBufferQueueEntry
{
AutoObjCPtr<id<MTLCommandBuffer>> buffer;
uint64_t serial;
};
std::deque<CmdBufferQueueEntry> mMetalCmdBuffers;
std::deque<CmdBufferQueueEntry> mMetalCmdBuffersTmp;
uint64_t mQueueSerialCounter = 1;
std::atomic<uint64_t> mCompletedBufferSerial{0};
mutable std::mutex mLock;
};
class CommandBuffer final : public WrappedObject<id<MTLCommandBuffer>>, angle::NonCopyable
{
public:
CommandBuffer(CommandQueue *cmdQueue);
~CommandBuffer();
void restart();
bool valid() const;
void commit();
void finish();
void present(id<CAMetalDrawable> presentationDrawable);
void setWriteDependency(const ResourceRef &resource);
void setReadDependency(const ResourceRef &resource);
CommandQueue &cmdQueue() { return mCmdQueue; }
void setActiveCommandEncoder(CommandEncoder *encoder);
void invalidateActiveCommandEncoder(CommandEncoder *encoder);
private:
void set(id<MTLCommandBuffer> metalBuffer);
void cleanup();
bool validImpl() const;
void commitImpl();
using ParentClass = WrappedObject<id<MTLCommandBuffer>>;
CommandQueue &mCmdQueue;
std::atomic<CommandEncoder *> mActiveCommandEncoder{nullptr};
uint64_t mQueueSerial = 0;
mutable std::mutex mLock;
bool mCommitted = false;
};
class CommandEncoder : public WrappedObject<id<MTLCommandEncoder>>, angle::NonCopyable
{
public:
enum Type
{
RENDER,
BLIT,
COMPUTE,
};
virtual ~CommandEncoder();
virtual void endEncoding();
void reset();
Type getType() const { return mType; }
CommandEncoder &markResourceBeingWrittenByGPU(const BufferRef &buffer);
CommandEncoder &markResourceBeingWrittenByGPU(const TextureRef &texture);
protected:
using ParentClass = WrappedObject<id<MTLCommandEncoder>>;
CommandEncoder(CommandBuffer *cmdBuffer, Type type);
CommandBuffer &cmdBuffer() { return mCmdBuffer; }
CommandQueue &cmdQueue() { return mCmdBuffer.cmdQueue(); }
void set(id<MTLCommandEncoder> metalCmdEncoder);
private:
const Type mType;
CommandBuffer &mCmdBuffer;
};
class RenderCommandEncoder final : public CommandEncoder
{
public:
RenderCommandEncoder(CommandBuffer *cmdBuffer);
~RenderCommandEncoder() override;
void endEncoding() override;
RenderCommandEncoder &restart(const RenderPassDesc &desc);
RenderCommandEncoder &setRenderPipelineState(id<MTLRenderPipelineState> state);
RenderCommandEncoder &setTriangleFillMode(MTLTriangleFillMode mode);
RenderCommandEncoder &setFrontFacingWinding(MTLWinding winding);
RenderCommandEncoder &setCullMode(MTLCullMode mode);
RenderCommandEncoder &setDepthStencilState(id<MTLDepthStencilState> state);
RenderCommandEncoder &setDepthBias(float depthBias, float slopeScale, float clamp);
RenderCommandEncoder &setStencilRefVals(uint32_t frontRef, uint32_t backRef);
RenderCommandEncoder &setStencilRefVal(uint32_t ref);
RenderCommandEncoder &setViewport(const MTLViewport &viewport);
RenderCommandEncoder &setScissorRect(const MTLScissorRect &rect);
RenderCommandEncoder &setBlendColor(float r, float g, float b, float a);
RenderCommandEncoder &setVertexBuffer(const BufferRef &buffer, uint32_t offset, uint32_t index);
RenderCommandEncoder &setVertexBytes(const uint8_t *bytes, size_t size, uint32_t index);
template <typename T>
RenderCommandEncoder &setVertexData(const T &data, uint32_t index)
{
return setVertexBytes(reinterpret_cast<const uint8_t *>(&data), sizeof(T), index);
}
RenderCommandEncoder &setVertexSamplerState(id<MTLSamplerState> state,
float lodMinClamp,
float lodMaxClamp,
uint32_t index);
RenderCommandEncoder &setVertexTexture(const TextureRef &texture, uint32_t index);
RenderCommandEncoder &setFragmentBuffer(const BufferRef &buffer,
uint32_t offset,
uint32_t index);
RenderCommandEncoder &setFragmentBytes(const uint8_t *bytes, size_t size, uint32_t index);
template <typename T>
RenderCommandEncoder &setFragmentData(const T &data, uint32_t index)
{
return setFragmentBytes(reinterpret_cast<const uint8_t *>(&data), sizeof(T), index);
}
RenderCommandEncoder &setFragmentSamplerState(id<MTLSamplerState> state,
float lodMinClamp,
float lodMaxClamp,
uint32_t index);
RenderCommandEncoder &setFragmentTexture(const TextureRef &texture, uint32_t index);
RenderCommandEncoder &draw(MTLPrimitiveType primitiveType,
uint32_t vertexStart,
uint32_t vertexCount);
RenderCommandEncoder &drawInstanced(MTLPrimitiveType primitiveType,
uint32_t vertexStart,
uint32_t vertexCount,
uint32_t instances);
RenderCommandEncoder &drawIndexed(MTLPrimitiveType primitiveType,
uint32_t indexCount,
MTLIndexType indexType,
const BufferRef &indexBuffer,
size_t bufferOffset);
RenderCommandEncoder &drawIndexedInstanced(MTLPrimitiveType primitiveType,
uint32_t indexCount,
MTLIndexType indexType,
const BufferRef &indexBuffer,
size_t bufferOffset,
uint32_t instances);
RenderCommandEncoder &drawIndexedInstancedBaseVertex(MTLPrimitiveType primitiveType,
uint32_t indexCount,
MTLIndexType indexType,
const BufferRef &indexBuffer,
size_t bufferOffset,
uint32_t instances,
uint32_t baseVertex);
RenderCommandEncoder &setColorStoreAction(MTLStoreAction action, uint32_t colorAttachmentIndex);
// Set store action for every color attachment.
RenderCommandEncoder &setColorStoreAction(MTLStoreAction action);
RenderCommandEncoder &setDepthStencilStoreAction(MTLStoreAction depthStoreAction,
MTLStoreAction stencilStoreAction);
RenderCommandEncoder &setDepthStoreAction(MTLStoreAction action);
RenderCommandEncoder &setStencilStoreAction(MTLStoreAction action);
const RenderPassDesc &renderPassDesc() const { return mRenderPassDesc; }
private:
id<MTLRenderCommandEncoder> get()
{
return static_cast<id<MTLRenderCommandEncoder>>(CommandEncoder::get());
}
inline void initWriteDependencyAndStoreAction(const TextureRef &texture,
MTLStoreAction *storeActionOut);
RenderPassDesc mRenderPassDesc;
MTLStoreAction mColorInitialStoreActions[kMaxRenderTargets];
MTLStoreAction mDepthInitialStoreAction;
MTLStoreAction mStencilInitialStoreAction;
};
class BlitCommandEncoder final : public CommandEncoder
{
public:
BlitCommandEncoder(CommandBuffer *cmdBuffer);
~BlitCommandEncoder() override;
BlitCommandEncoder &restart();
BlitCommandEncoder ©BufferToTexture(const BufferRef &src,
size_t srcOffset,
size_t srcBytesPerRow,
size_t srcBytesPerImage,
MTLSize srcSize,
const TextureRef &dst,
uint32_t dstSlice,
uint32_t dstLevel,
MTLOrigin dstOrigin,
MTLBlitOption blitOption);
BlitCommandEncoder ©Texture(const TextureRef &dst,
uint32_t dstSlice,
uint32_t dstLevel,
MTLOrigin dstOrigin,
MTLSize dstSize,
const TextureRef &src,
uint32_t srcSlice,
uint32_t srcLevel,
MTLOrigin srcOrigin);
BlitCommandEncoder &generateMipmapsForTexture(const TextureRef &texture);
BlitCommandEncoder &synchronizeResource(const TextureRef &texture);
private:
id<MTLBlitCommandEncoder> get()
{
return static_cast<id<MTLBlitCommandEncoder>>(CommandEncoder::get());
}
};
class ComputeCommandEncoder final : public CommandEncoder
{
public:
ComputeCommandEncoder(CommandBuffer *cmdBuffer);
~ComputeCommandEncoder() override;
ComputeCommandEncoder &restart();
ComputeCommandEncoder &setComputePipelineState(id<MTLComputePipelineState> state);
ComputeCommandEncoder &setBuffer(const BufferRef &buffer, uint32_t offset, uint32_t index);
ComputeCommandEncoder &setBytes(const uint8_t *bytes, size_t size, uint32_t index);
template <typename T>
ComputeCommandEncoder &setData(const T &data, uint32_t index)
{
return setBytes(reinterpret_cast<const uint8_t *>(&data), sizeof(T), index);
}
ComputeCommandEncoder &setSamplerState(id<MTLSamplerState> state,
float lodMinClamp,
float lodMaxClamp,
uint32_t index);
ComputeCommandEncoder &setTexture(const TextureRef &texture, uint32_t index);
ComputeCommandEncoder &dispatch(MTLSize threadGroupsPerGrid, MTLSize threadsPerGroup);
ComputeCommandEncoder &dispatchNonUniform(MTLSize threadsPerGrid, MTLSize threadsPerGroup);
private:
id<MTLComputeCommandEncoder> get()
{
return static_cast<id<MTLComputeCommandEncoder>>(CommandEncoder::get());
}
};
} // namespace mtl
} // namespace rx
#endif /* LIBANGLE_RENDERER_METAL_COMMANDENBUFFERMTL_H_ */