Title: [291387] trunk/Source/WebGPU
- Revision
- 291387
- Author
- [email protected]
- Date
- 2022-03-16 20:56:01 -0700 (Wed, 16 Mar 2022)
Log Message
[WebGPU] Implement sampler creation according to the spec
https://bugs.webkit.org/show_bug.cgi?id=237882
Reviewed by Kimmo Kinnunen.
Implement Device::createSampler() according to the algorithm in the spec. There
are a few things which the spec lists which we can't do yet (like making objects
invalid), so those things are left with FIXMEs. Every step is listed with links
to the spec where appropriate, and with quotes to the spec describing what is
being implemented.
* WebGPU/Buffer.mm:
(WebGPU::Buffer::mapAsync):
* WebGPU/ComputePipeline.mm:
(WebGPU::createConstantValues):
* WebGPU/Sampler.h:
(WebGPU::Sampler::create):
(WebGPU::Sampler::descriptor const):
(WebGPU::Sampler::isComparison const):
(WebGPU::Sampler::isFiltering const):
* WebGPU/Sampler.mm:
(WebGPU::validateCreateSampler):
(WebGPU::Device::createSampler):
(WebGPU::Sampler::Sampler):
Modified Paths
Diff
Modified: trunk/Source/WebGPU/ChangeLog (291386 => 291387)
--- trunk/Source/WebGPU/ChangeLog 2022-03-17 03:37:11 UTC (rev 291386)
+++ trunk/Source/WebGPU/ChangeLog 2022-03-17 03:56:01 UTC (rev 291387)
@@ -1,5 +1,32 @@
2022-03-16 Myles C. Maxfield <[email protected]>
+ [WebGPU] Implement sampler creation according to the spec
+ https://bugs.webkit.org/show_bug.cgi?id=237882
+
+ Reviewed by Kimmo Kinnunen.
+
+ Implement Device::createSampler() according to the algorithm in the spec. There
+ are a few things which the spec lists which we can't do yet (like making objects
+ invalid), so those things are left with FIXMEs. Every step is listed with links
+ to the spec where appropriate, and with quotes to the spec describing what is
+ being implemented.
+
+ * WebGPU/Buffer.mm:
+ (WebGPU::Buffer::mapAsync):
+ * WebGPU/ComputePipeline.mm:
+ (WebGPU::createConstantValues):
+ * WebGPU/Sampler.h:
+ (WebGPU::Sampler::create):
+ (WebGPU::Sampler::descriptor const):
+ (WebGPU::Sampler::isComparison const):
+ (WebGPU::Sampler::isFiltering const):
+ * WebGPU/Sampler.mm:
+ (WebGPU::validateCreateSampler):
+ (WebGPU::Device::createSampler):
+ (WebGPU::Sampler::Sampler):
+
+2022-03-16 Myles C. Maxfield <[email protected]>
+
[WebGPU] Implement first draft of debug commands according to the spec
https://bugs.webkit.org/show_bug.cgi?id=237879
Modified: trunk/Source/WebGPU/WebGPU/Buffer.mm (291386 => 291387)
--- trunk/Source/WebGPU/WebGPU/Buffer.mm 2022-03-17 03:37:11 UTC (rev 291386)
+++ trunk/Source/WebGPU/WebGPU/Buffer.mm 2022-03-17 03:56:01 UTC (rev 291387)
@@ -328,7 +328,6 @@
callback(WGPUBufferMapAsyncStatus_DeviceLost);
return;
default:
- ASSERT_NOT_REACHED();
callback(WGPUBufferMapAsyncStatus_Error);
return;
}
Modified: trunk/Source/WebGPU/WebGPU/ComputePipeline.mm (291386 => 291387)
--- trunk/Source/WebGPU/WebGPU/ComputePipeline.mm 2022-03-17 03:37:11 UTC (rev 291386)
+++ trunk/Source/WebGPU/WebGPU/ComputePipeline.mm 2022-03-17 03:56:01 UTC (rev 291387)
@@ -100,8 +100,6 @@
[constantValues setConstantValue:&value type:MTLDataTypeUInt withName:specializationConstant.mangledName];
break;
}
- default:
- return nullptr;
}
}
return constantValues;
Modified: trunk/Source/WebGPU/WebGPU/Sampler.h (291386 => 291387)
--- trunk/Source/WebGPU/WebGPU/Sampler.h 2022-03-17 03:37:11 UTC (rev 291386)
+++ trunk/Source/WebGPU/WebGPU/Sampler.h 2022-03-17 03:56:01 UTC (rev 291387)
@@ -34,9 +34,9 @@
class Sampler : public RefCounted<Sampler> {
WTF_MAKE_FAST_ALLOCATED;
public:
- static Ref<Sampler> create(id<MTLSamplerState> samplerState)
+ static Ref<Sampler> create(id<MTLSamplerState> samplerState, const WGPUSamplerDescriptor& descriptor)
{
- return adoptRef(*new Sampler(samplerState));
+ return adoptRef(*new Sampler(samplerState, descriptor));
}
~Sampler();
@@ -44,11 +44,20 @@
void setLabel(String&&);
id<MTLSamplerState> samplerState() const { return m_samplerState; }
+ const WGPUSamplerDescriptor& descriptor() const { return m_descriptor; }
+ // "Set s.[[isComparison]] to false if the compare attribute of s.[[descriptor]] is null or undefined. Otherwise, set it to true."
+ bool isComparison() const { return descriptor().compare != WGPUCompareFunction_Undefined; }
+ // "Set s.[[isFiltering]] to false if none of minFilter, magFilter, or mipmapFilter has the value of "linear". Otherwise, set it to true."
+ bool isFiltering() const { return descriptor().minFilter == WGPUFilterMode_Linear || descriptor().magFilter == WGPUFilterMode_Linear || descriptor().mipmapFilter == WGPUFilterMode_Linear; }
private:
- Sampler(id<MTLSamplerState>);
+ Sampler(id<MTLSamplerState>, const WGPUSamplerDescriptor& descriptor);
id<MTLSamplerState> m_samplerState { nil };
+
+ WGPUSamplerDescriptor m_descriptor { }; // "The GPUSamplerDescriptor with which the GPUSampler was created."
+ // "[[isComparison]] of type boolean." This is unnecessary; it's implemented in isComparison().
+ // "[[isFiltering]] of type boolean." This is unnecessary; it's implemented in isFiltering().
};
} // namespace WebGPU
Modified: trunk/Source/WebGPU/WebGPU/Sampler.mm (291386 => 291387)
--- trunk/Source/WebGPU/WebGPU/Sampler.mm 2022-03-17 03:37:11 UTC (rev 291386)
+++ trunk/Source/WebGPU/WebGPU/Sampler.mm 2022-03-17 03:56:01 UTC (rev 291387)
@@ -28,17 +28,185 @@
#import "APIConversions.h"
#import "Device.h"
+#import <cmath>
namespace WebGPU {
+static bool validateCreateSampler(Device&, const WGPUSamplerDescriptor& descriptor)
+{
+ // https://gpuweb.github.io/gpuweb/#abstract-opdef-validating-gpusamplerdescriptor
+
+ // FIXME: "device is valid."
+
+ // "descriptor.lodMinClamp is greater than or equal to 0."
+ if (std::isnan(descriptor.lodMinClamp) || descriptor.lodMinClamp < 0)
+ return false;
+
+ // "descriptor.lodMaxClamp is greater than or equal to descriptor.lodMinClamp."
+ if (std::isnan(descriptor.lodMaxClamp) || descriptor.lodMaxClamp < descriptor.lodMinClamp)
+ return false;
+
+ // "descriptor.maxAnisotropy is greater than or equal to 1."
+ if (descriptor.maxAnisotropy < 1)
+ return false;
+
+ // "descriptor.maxAnisotropy is less than or equal to 16."
+ if (descriptor.maxAnisotropy > 16)
+ return false;
+
+ // "When descriptor.maxAnisotropy is greater than 1"
+ if (descriptor.maxAnisotropy > 1) {
+ // "descriptor.magFilter, descriptor.minFilter, and descriptor.mipmapFilter must be equal to "linear"."
+ if (descriptor.magFilter != WGPUFilterMode_Linear
+ || descriptor.minFilter != WGPUFilterMode_Linear
+ || descriptor.mipmapFilter != WGPUFilterMode_Linear)
+ return false;
+ }
+
+ return true;
+}
+
RefPtr<Sampler> Device::createSampler(const WGPUSamplerDescriptor& descriptor)
{
- UNUSED_PARAM(descriptor);
- return Sampler::create(nil);
+ if (descriptor.nextInChain)
+ return nullptr;
+
+ // https://gpuweb.github.io/gpuweb/#dom-gpudevice-createsampler
+
+ // "If validating GPUSamplerDescriptor(this, descriptor) returns false:"
+ if (!validateCreateSampler(*this, descriptor)) {
+ // FIXME: "Generate a validation error."
+
+ // "Create a new invalid GPUSampler and return the result."
+ return nullptr;
+ }
+
+ MTLSamplerDescriptor *samplerDescriptor = [MTLSamplerDescriptor new];
+
+ switch (descriptor.addressModeU) {
+ case WGPUAddressMode_Repeat:
+ samplerDescriptor.rAddressMode = MTLSamplerAddressModeRepeat;
+ break;
+ case WGPUAddressMode_MirrorRepeat:
+ samplerDescriptor.rAddressMode = MTLSamplerAddressModeMirrorRepeat;
+ break;
+ case WGPUAddressMode_ClampToEdge:
+ samplerDescriptor.rAddressMode = MTLSamplerAddressModeClampToEdge;
+ break;
+ default:
+ return nullptr;
+ }
+
+ switch (descriptor.addressModeV) {
+ case WGPUAddressMode_Repeat:
+ samplerDescriptor.sAddressMode = MTLSamplerAddressModeRepeat;
+ break;
+ case WGPUAddressMode_MirrorRepeat:
+ samplerDescriptor.sAddressMode = MTLSamplerAddressModeMirrorRepeat;
+ break;
+ case WGPUAddressMode_ClampToEdge:
+ samplerDescriptor.sAddressMode = MTLSamplerAddressModeClampToEdge;
+ break;
+ default:
+ return nullptr;
+ }
+
+ switch (descriptor.addressModeW) {
+ case WGPUAddressMode_Repeat:
+ samplerDescriptor.tAddressMode = MTLSamplerAddressModeRepeat;
+ break;
+ case WGPUAddressMode_MirrorRepeat:
+ samplerDescriptor.tAddressMode = MTLSamplerAddressModeMirrorRepeat;
+ break;
+ case WGPUAddressMode_ClampToEdge:
+ samplerDescriptor.tAddressMode = MTLSamplerAddressModeClampToEdge;
+ break;
+ default:
+ return nullptr;
+ }
+
+ switch (descriptor.magFilter) {
+ case WGPUFilterMode_Nearest:
+ samplerDescriptor.magFilter = MTLSamplerMinMagFilterNearest;
+ break;
+ case WGPUFilterMode_Linear:
+ samplerDescriptor.magFilter = MTLSamplerMinMagFilterLinear;
+ break;
+ default:
+ return nullptr;
+ }
+
+ switch (descriptor.minFilter) {
+ case WGPUFilterMode_Nearest:
+ samplerDescriptor.minFilter = MTLSamplerMinMagFilterNearest;
+ break;
+ case WGPUFilterMode_Linear:
+ samplerDescriptor.minFilter = MTLSamplerMinMagFilterLinear;
+ break;
+ default:
+ return nullptr;
+ }
+
+ switch (descriptor.mipmapFilter) {
+ case WGPUFilterMode_Nearest:
+ samplerDescriptor.mipFilter = MTLSamplerMipFilterNearest;
+ break;
+ case WGPUFilterMode_Linear:
+ samplerDescriptor.mipFilter = MTLSamplerMipFilterLinear;
+ break;
+ default:
+ return nullptr;
+ }
+
+ samplerDescriptor.lodMinClamp = descriptor.lodMinClamp;
+
+ samplerDescriptor.lodMaxClamp = descriptor.lodMaxClamp;
+
+ switch (descriptor.compare) {
+ case WGPUCompareFunction_Undefined:
+ break;
+ case WGPUCompareFunction_Never:
+ samplerDescriptor.compareFunction = MTLCompareFunctionNever;
+ break;
+ case WGPUCompareFunction_Less:
+ samplerDescriptor.compareFunction = MTLCompareFunctionLess;
+ break;
+ case WGPUCompareFunction_LessEqual:
+ samplerDescriptor.compareFunction = MTLCompareFunctionLessEqual;
+ break;
+ case WGPUCompareFunction_Greater:
+ samplerDescriptor.compareFunction = MTLCompareFunctionGreater;
+ break;
+ case WGPUCompareFunction_GreaterEqual:
+ samplerDescriptor.compareFunction = MTLCompareFunctionGreaterEqual;
+ break;
+ case WGPUCompareFunction_Equal:
+ samplerDescriptor.compareFunction = MTLCompareFunctionEqual;
+ break;
+ case WGPUCompareFunction_NotEqual:
+ samplerDescriptor.compareFunction = MTLCompareFunctionNotEqual;
+ break;
+ case WGPUCompareFunction_Always:
+ samplerDescriptor.compareFunction = MTLCompareFunctionAlways;
+ break;
+ default:
+ return nullptr;
+ }
+
+ samplerDescriptor.maxAnisotropy = descriptor.maxAnisotropy;
+
+ samplerDescriptor.label = descriptor.label ? [NSString stringWithCString:descriptor.label encoding:NSUTF8StringEncoding] : nil;
+
+ id<MTLSamplerState> samplerState = [m_device newSamplerStateWithDescriptor:samplerDescriptor];
+ if (!samplerState)
+ return nullptr;
+
+ return Sampler::create(samplerState, descriptor);
}
-Sampler::Sampler(id<MTLSamplerState> samplerState)
+Sampler::Sampler(id<MTLSamplerState> samplerState, const WGPUSamplerDescriptor& descriptor)
: m_samplerState(samplerState)
+ , m_descriptor(descriptor)
{
}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes