blob: 0884f27594ba754d4b779705cad03cd29f04a61a [file] [log] [blame]
/*
* Copyright (C) 2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "config.h"
#import "GPUComputePipeline.h"
#if ENABLE(WEBGPU)
#import "GPUComputePipelineDescriptor.h"
#import "GPUDevice.h"
#import "GPUPipelineMetalConvertLayout.h"
#import "WHLSLPrepare.h"
#import <Metal/Metal.h>
#import <wtf/BlockObjCExceptions.h>
#import <wtf/text/StringConcatenate.h>
namespace WebCore {
static bool trySetMetalFunctions(const char* const functionName, MTLLibrary *computeMetalLibrary, MTLComputePipelineDescriptor *mtlDescriptor, const String& computeEntryPointName, GPUErrorScopes& errorScopes)
{
BEGIN_BLOCK_OBJC_EXCEPTIONS;
if (!computeMetalLibrary) {
errorScopes.generateError(makeString(functionName, ": MTLLibrary for compute stage does not exist!"));
return false;
}
auto function = adoptNS([computeMetalLibrary newFunctionWithName:computeEntryPointName]);
if (!function) {
errorScopes.generateError(makeString(functionName, ": Cannot create compute MTLFunction \"", computeEntryPointName, "\"!"));
return false;
}
[mtlDescriptor setComputeFunction:function.get()];
END_BLOCK_OBJC_EXCEPTIONS;
return true;
}
static Optional<WHLSL::ComputeDimensions> trySetFunctions(const char* const functionName, const GPUPipelineStageDescriptor& computeStage, const GPUDevice& device, MTLComputePipelineDescriptor* mtlDescriptor, Optional<WHLSL::ComputePipelineDescriptor>& whlslDescriptor, GPUErrorScopes& errorScopes)
{
RetainPtr<MTLLibrary> computeLibrary;
String computeEntryPoint;
WHLSL::ComputeDimensions computeDimensions { 1, 1, 1 };
if (whlslDescriptor) {
// WHLSL functions are compiled to MSL first.
String whlslSource = computeStage.module->whlslSource();
ASSERT(!whlslSource.isNull());
whlslDescriptor->entryPointName = computeStage.entryPoint;
auto whlslCompileResult = WHLSL::prepare(whlslSource, *whlslDescriptor);
if (!whlslCompileResult) {
errorScopes.generateError(makeString(functionName, ": WHLSL compilation failed!"));
return WTF::nullopt;
}
computeDimensions = whlslCompileResult->computeDimensions;
NSError *error = nil;
BEGIN_BLOCK_OBJC_EXCEPTIONS;
computeLibrary = adoptNS([device.platformDevice() newLibraryWithSource:whlslCompileResult->metalSource options:nil error:&error]);
END_BLOCK_OBJC_EXCEPTIONS;
#ifndef NDEBUG
if (!computeLibrary)
NSLog(@"%@", error);
#endif
ASSERT(computeLibrary);
// FIXME: https://bugs.webkit.org/show_bug.cgi?id=195771 Once we zero-fill variables, there should be no warnings, so we should be able to ASSERT(!error) here.
computeEntryPoint = whlslCompileResult->mangledEntryPointName;
} else {
computeLibrary = computeStage.module->platformShaderModule();
computeEntryPoint = computeStage.entryPoint;
}
if (trySetMetalFunctions(functionName, computeLibrary.get(), mtlDescriptor, computeEntryPoint, errorScopes))
return computeDimensions;
return WTF::nullopt;
}
struct ConvertResult {
RetainPtr<MTLComputePipelineDescriptor> pipelineDescriptor;
WHLSL::ComputeDimensions computeDimensions;
};
static Optional<ConvertResult> convertComputePipelineDescriptor(const char* const functionName, const GPUComputePipelineDescriptor& descriptor, const GPUDevice& device, GPUErrorScopes& errorScopes)
{
RetainPtr<MTLComputePipelineDescriptor> mtlDescriptor;
BEGIN_BLOCK_OBJC_EXCEPTIONS;
mtlDescriptor = adoptNS([MTLComputePipelineDescriptor new]);
END_BLOCK_OBJC_EXCEPTIONS;
if (!mtlDescriptor) {
errorScopes.generateError(makeString(functionName, ": Error creating MTLDescriptor!"));
return WTF::nullopt;
}
const auto& computeStage = descriptor.computeStage;
bool isWhlsl = !computeStage.module->whlslSource().isNull();
Optional<WHLSL::ComputePipelineDescriptor> whlslDescriptor;
if (isWhlsl)
whlslDescriptor = WHLSL::ComputePipelineDescriptor();
if (descriptor.layout && whlslDescriptor) {
if (auto layout = convertLayout(*descriptor.layout))
whlslDescriptor->layout = WTFMove(*layout);
else {
errorScopes.generateError(makeString(functionName, ": Error converting GPUPipelineLayout!"));
return WTF::nullopt;
}
}
if (auto computeDimensions = trySetFunctions(functionName, computeStage, device, mtlDescriptor.get(), whlslDescriptor, errorScopes))
return {{ mtlDescriptor, *computeDimensions }};
return WTF::nullopt;
}
struct CreateResult {
RetainPtr<MTLComputePipelineState> pipelineState;
WHLSL::ComputeDimensions computeDimensions;
};
static Optional<CreateResult> tryCreateMTLComputePipelineState(const char* const functionName, const GPUDevice& device, const GPUComputePipelineDescriptor& descriptor, GPUErrorScopes& errorScopes)
{
if (!device.platformDevice()) {
errorScopes.generateError(makeString(functionName, ": Invalid GPUDevice!"));
return WTF::nullopt;
}
auto convertResult = convertComputePipelineDescriptor(functionName, descriptor, device, errorScopes);
if (!convertResult)
return WTF::nullopt;
ASSERT(convertResult->pipelineDescriptor);
auto mtlDescriptor = convertResult->pipelineDescriptor;
RetainPtr<MTLComputePipelineState> pipeline;
BEGIN_BLOCK_OBJC_EXCEPTIONS;
NSError *error = nil;
pipeline = adoptNS([device.platformDevice() newComputePipelineStateWithDescriptor:mtlDescriptor.get() options:MTLPipelineOptionNone reflection:nil error:&error]);
if (!pipeline) {
errorScopes.generateError(makeString(functionName, ": ", (error ? error.localizedDescription.UTF8String : "Unable to create MTLComputePipelineState!")));
return WTF::nullopt;
}
END_BLOCK_OBJC_EXCEPTIONS;
return {{ pipeline, convertResult->computeDimensions }};
}
RefPtr<GPUComputePipeline> GPUComputePipeline::tryCreate(const GPUDevice& device, const GPUComputePipelineDescriptor& descriptor, Ref<GPUErrorScopes>&& errorScopes)
{
const char* const functionName = "GPUComputePipeline::tryCreate()";
auto createResult = tryCreateMTLComputePipelineState(functionName, device, descriptor, errorScopes);
if (!createResult)
return nullptr;
return adoptRef(new GPUComputePipeline(WTFMove(createResult->pipelineState), createResult->computeDimensions, WTFMove(errorScopes)));
}
GPUComputePipeline::GPUComputePipeline(RetainPtr<MTLComputePipelineState>&& pipeline, WHLSL::ComputeDimensions computeDimensions, Ref<GPUErrorScopes>&& errorScopes)
: GPUObjectBase(WTFMove(errorScopes))
, m_platformComputePipeline(WTFMove(pipeline))
, m_computeDimensions(computeDimensions)
{
}
} // namespace WebCore
#endif // ENABLE(WEBGPU)