blob: 4bf201495a3c30365b61e90e49e281d539af5b41 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallez0ba55502017-06-14 15:46:59 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Corentin Wallez30965a72018-07-24 16:42:33 +020015#ifndef DAWNNATIVE_METAL_SHADERMODULEMTL_H_
16#define DAWNNATIVE_METAL_SHADERMODULEMTL_H_
Corentin Wallez0ba55502017-06-14 15:46:59 -040017
Corentin Wallezd37523f2018-07-24 13:53:51 +020018#include "dawn_native/ShaderModule.h"
Corentin Wallez0ba55502017-06-14 15:46:59 -040019
Corentin Wallez0055d952020-11-16 23:07:56 +000020#include "common/NSRef.h"
Ryan Harrison58dbfca2019-11-14 16:10:45 +000021#include "dawn_native/Error.h"
22
Corentin Wallez0055d952020-11-16 23:07:56 +000023#import <Metal/Metal.h>
24
Corentin Wallezec9cf2a2022-01-12 09:17:35 +000025namespace dawn::native::metal {
Corentin Wallez0ba55502017-06-14 15:46:59 -040026
Corentin Wallezdf671032018-08-20 17:01:20 +020027 class Device;
Corentin Wallez30beb652017-07-26 12:35:35 -040028 class PipelineLayout;
Idan Raiterd3150222020-08-13 23:53:59 +000029 class RenderPipeline;
Corentin Wallez30beb652017-07-26 12:35:35 -040030
Rafael Cintronc64242d2020-04-06 18:20:02 +000031 class ShaderModule final : public ShaderModuleBase {
Corentin Wallezf58d84d2017-11-24 14:12:44 -050032 public:
Corentin Wallez50f99582021-03-31 18:36:32 +000033 static ResultOrError<Ref<ShaderModule>> Create(Device* device,
34 const ShaderModuleDescriptor* descriptor,
35 ShaderModuleParseResult* parseResult);
Corentin Wallez0ba55502017-06-14 15:46:59 -040036
Corentin Wallezf58d84d2017-11-24 14:12:44 -050037 struct MetalFunctionData {
Corentin Wallez0055d952020-11-16 23:07:56 +000038 NSPRef<id<MTLFunction>> function;
Corentin Wallezbfc9cee2019-08-02 18:15:08 +000039 bool needsStorageBufferLength;
Austin Eng5528d0e2021-09-15 18:16:50 +000040 std::vector<uint32_t> workgroupAllocations;
Corentin Wallezf58d84d2017-11-24 14:12:44 -050041 };
Shrek Shaob0a5ed42021-11-04 18:43:10 +000042
43 // MTLFunctionConstantValues needs @available tag to compile
44 // Use id (like void*) in function signature as workaround and do static cast inside
Corentin Wallez676e8f32020-09-09 22:47:07 +000045 MaybeError CreateFunction(const char* entryPointName,
46 SingleShaderStage stage,
47 const PipelineLayout* layout,
48 MetalFunctionData* out,
Shrek Shaob0a5ed42021-11-04 18:43:10 +000049 id constantValues = nil,
Corentin Wallez676e8f32020-09-09 22:47:07 +000050 uint32_t sampleMask = 0xFFFFFFFF,
Jiawei Shao24cad6e2021-09-15 01:28:23 +000051 const RenderPipeline* renderPipeline = nullptr);
Corentin Wallez0ba55502017-06-14 15:46:59 -040052
Corentin Wallezf58d84d2017-11-24 14:12:44 -050053 private:
Stephen Whitec0f20fb2021-09-02 16:50:58 +000054 ResultOrError<std::string> TranslateToMSL(const char* entryPointName,
55 SingleShaderStage stage,
56 const PipelineLayout* layout,
57 uint32_t sampleMask,
58 const RenderPipeline* renderPipeline,
Stephen Whitec0f20fb2021-09-02 16:50:58 +000059 std::string* remappedEntryPointName,
60 bool* needsStorageBufferLength,
Austin Eng5528d0e2021-09-15 18:16:50 +000061 bool* hasInvariantAttribute,
62 std::vector<uint32_t>* workgroupAllocations);
Ryan Harrison58dbfca2019-11-14 16:10:45 +000063 ShaderModule(Device* device, const ShaderModuleDescriptor* descriptor);
Rafael Cintronc64242d2020-04-06 18:20:02 +000064 ~ShaderModule() override = default;
Austin Eng0d948f72020-12-07 18:12:13 +000065 MaybeError Initialize(ShaderModuleParseResult* parseResult);
Corentin Wallez0ba55502017-06-14 15:46:59 -040066 };
67
Corentin Wallezec9cf2a2022-01-12 09:17:35 +000068} // namespace dawn::native::metal
Corentin Wallez0ba55502017-06-14 15:46:59 -040069
Corentin Wallez30965a72018-07-24 16:42:33 +020070#endif // DAWNNATIVE_METAL_SHADERMODULEMTL_H_