2017-11-17 11:14:29 -05:00
|
|
|
/*
|
|
|
|
* SPIRVToMSLConverter.cpp
|
|
|
|
*
|
2020-01-07 16:47:29 -05:00
|
|
|
* Copyright (c) 2015-2020 The Brenwill Workshop Ltd. (http://www.brenwill.com)
|
2017-11-17 11:14:29 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SPIRVToMSLConverter.h"
|
|
|
|
#include "MVKCommonEnvironment.h"
|
|
|
|
#include "MVKStrings.h"
|
2018-01-08 21:44:46 -05:00
|
|
|
#include "FileSupport.h"
|
2019-02-18 22:19:37 +00:00
|
|
|
#include "SPIRVSupport.h"
|
2019-03-28 11:58:05 -04:00
|
|
|
#include <fstream>
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
using namespace mvk;
|
|
|
|
using namespace std;
|
2019-06-15 01:47:13 -04:00
|
|
|
using namespace SPIRV_CROSS_NAMESPACE;
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
2019-06-29 18:01:07 -04:00
|
|
|
#pragma mark SPIRVToMSLConversionConfiguration
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
// Returns whether the vector contains the value (using a matches(T&) comparison member function). */
|
|
|
|
template<class T>
|
2019-06-29 18:01:07 -04:00
|
|
|
bool containsMatching(const vector<T>& vec, const T& val) {
|
2018-03-19 10:58:46 -04:00
|
|
|
for (const T& vecVal : vec) { if (vecVal.matches(val)) { return true; } }
|
2017-11-17 11:14:29 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionOptions::matches(const SPIRVToMSLConversionOptions& other) const {
|
2018-03-19 10:58:46 -04:00
|
|
|
if (entryPointStage != other.entryPointStage) { return false; }
|
2019-06-15 01:47:13 -04:00
|
|
|
if (entryPointName != other.entryPointName) { return false; }
|
Add support for tessellation.
At long last, tessellation comes to MoltenVK! With this change, clients
will now be able to specify tessellation shaders when creating
pipelines, and then draw tessellated patches with them.
Unfortunately, there seem to be a few gotchas with tessellation in
Metal. For one thing, tessellation pipelines in Metal are structured
very differently from Vulkan. There is no tessellation control or even
vertex stage. Instead, the tessellation evaluation shader takes the
place of the vertex function as a "post-tessellation vertex function."
The tessellation levels are supplied in a buffer to the tessellator,
which you are expected to populate. The most common way to do this is by
running a compute shader. MoltenVK thus runs the vertex shader and
tessellation control shader by themselves; a single `VkPipeline` object
then requires at least *three* `MTLPipelineState` objects.
But wait, there's more! The tessellation-control-as-compute stage uses
Metal's support for vertex-style stage input to a compute shader. But,
this support requires one to declare indexing *ahead of time*, when the
pipeline state is created. So a single `VkPipeline` object could have as
many as *five* `MTLPipelineState` objects.
Further, if there are more output than input control points for the
tessellation control stage, then later invocations may end up fetching
the wrong attributes! To get around this, this change uses index buffers
to ensure that all tessellation control shaders see the correct input.
Unfortunately, in the indexed draw case, this means that the incoming
index buffer needs to be munged.
Instancing is another pain point here. In Vulkan, as in OpenGL and
Direct3D, instancing is done in the vertex shader; but in Metal, it is
done at the tessellation evaluation stage. For this reason, only the
vertex stage of a tessellated draw supports instancing. Additional
memory is required to hold data for the extra vertices generated by
instancing. This also requires still more munging of index buffers for
indexed draws.
Indirect draws are even more painful. Because the number of vertices and
instances is unknown, storage for the maximum possible number of
vertices must be allocated. This change imposes a totally arbitrary
limit of 131072 vertices from a single draw, including all vertices
generated by instancing. On a Mac, this requires about 194-256 MB of
VRAM for all the temporary buffers.
There are some possible optimizations here. If we could prove that the
vertex shader's output doesn't depend on the instance ID, either
directly or through a per-instance attribute, then we could avoid
running the vertex and tess. control stages per instance, and take
advantage of Metal's support for tess. eval instancing. If we could
also prove that the vertex shader simply passes instance attributes
through (similarly with the tess. control shader), we could do this for
many more instanced draws as well. It should also be possible to cache
the output from the tess. control stage; if the draw comes up again, we
can then skip the vertex and tess. control stages entirely!
Fixes #56 and #501.
2019-02-18 20:56:42 -06:00
|
|
|
if (tessPatchKind != other.tessPatchKind) { return false; }
|
|
|
|
if (numTessControlPoints != other.numTessControlPoints) { return false; }
|
2020-07-27 15:02:56 -04:00
|
|
|
if (shouldFlipVertexY != other.shouldFlipVertexY) { return false; }
|
|
|
|
|
|
|
|
if (memcmp(&mslOptions, &other.mslOptions, sizeof(mslOptions)) != 0) { return false; }
|
2019-06-15 01:47:13 -04:00
|
|
|
|
|
|
|
return true;
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL std::string SPIRVToMSLConversionOptions::printMSLVersion(uint32_t mslVersion, bool includePatch) {
|
2018-12-11 18:27:01 -05:00
|
|
|
string verStr;
|
|
|
|
|
|
|
|
uint32_t major = mslVersion / 10000;
|
|
|
|
verStr += to_string(major);
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
uint32_t minor = (mslVersion - CompilerMSL::Options::make_msl_version(major)) / 100;
|
2018-12-11 18:27:01 -05:00
|
|
|
verStr += ".";
|
|
|
|
verStr += to_string(minor);
|
|
|
|
|
|
|
|
if (includePatch) {
|
2019-06-15 01:47:13 -04:00
|
|
|
uint32_t patch = mslVersion - CompilerMSL::Options::make_msl_version(major, minor);
|
2018-12-11 18:27:01 -05:00
|
|
|
verStr += ".";
|
|
|
|
verStr += to_string(patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return verStr;
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL SPIRVToMSLConversionOptions::SPIRVToMSLConversionOptions() {
|
2020-07-27 15:02:56 -04:00
|
|
|
// Explicitly set mslOptions to defaults over cleared memory to ensure all instances
|
|
|
|
// have exactly the same memory layout when using memory comparison in matches().
|
|
|
|
memset(&mslOptions, 0, sizeof(mslOptions));
|
|
|
|
mslOptions = CompilerMSL::Options();
|
2019-06-29 18:01:07 -04:00
|
|
|
|
2019-04-17 16:09:07 -04:00
|
|
|
#if MVK_MACOS
|
2019-06-15 01:47:13 -04:00
|
|
|
mslOptions.platform = CompilerMSL::Options::macOS;
|
2019-04-17 16:09:07 -04:00
|
|
|
#endif
|
|
|
|
#if MVK_IOS
|
2019-06-15 01:47:13 -04:00
|
|
|
mslOptions.platform = CompilerMSL::Options::iOS;
|
2019-04-17 16:09:07 -04:00
|
|
|
#endif
|
2020-06-15 16:21:13 -07:00
|
|
|
#if MVK_TVOS
|
|
|
|
mslOptions.platform = CompilerMSL::Options::iOS;
|
|
|
|
#endif
|
2020-07-27 15:02:56 -04:00
|
|
|
|
|
|
|
mslOptions.pad_fragment_output_components = true;
|
2019-04-17 16:09:07 -04:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
MVK_PUBLIC_SYMBOL bool mvk::MSLShaderInput::matches(const mvk::MSLShaderInput& other) const {
|
|
|
|
if (shaderInput.location != other.shaderInput.location) { return false; }
|
|
|
|
if (shaderInput.format != other.shaderInput.format) { return false; }
|
|
|
|
if (shaderInput.builtin != other.shaderInput.builtin) { return false; }
|
|
|
|
if (shaderInput.vecsize != other.shaderInput.vecsize) { return false; }
|
2020-05-30 16:07:47 -04:00
|
|
|
if (binding != other.binding) { return false; }
|
2019-06-15 01:47:13 -04:00
|
|
|
return true;
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
MVK_PUBLIC_SYMBOL bool mvk::MSLResourceBinding::matches(const MSLResourceBinding& other) const {
|
|
|
|
if (resourceBinding.stage != other.resourceBinding.stage) { return false; }
|
|
|
|
if (resourceBinding.desc_set != other.resourceBinding.desc_set) { return false; }
|
|
|
|
if (resourceBinding.binding != other.resourceBinding.binding) { return false; }
|
|
|
|
if (resourceBinding.msl_buffer != other.resourceBinding.msl_buffer) { return false; }
|
|
|
|
if (resourceBinding.msl_texture != other.resourceBinding.msl_texture) { return false; }
|
|
|
|
if (resourceBinding.msl_sampler != other.resourceBinding.msl_sampler) { return false; }
|
|
|
|
|
|
|
|
if (requiresConstExprSampler != other.requiresConstExprSampler) { return false; }
|
|
|
|
|
|
|
|
// If requiresConstExprSampler is false, constExprSampler can be ignored
|
|
|
|
if (requiresConstExprSampler) {
|
|
|
|
if (constExprSampler.coord != other.constExprSampler.coord) { return false; }
|
|
|
|
if (constExprSampler.min_filter != other.constExprSampler.min_filter) { return false; }
|
|
|
|
if (constExprSampler.mag_filter != other.constExprSampler.mag_filter) { return false; }
|
|
|
|
if (constExprSampler.mip_filter != other.constExprSampler.mip_filter) { return false; }
|
|
|
|
if (constExprSampler.s_address != other.constExprSampler.s_address) { return false; }
|
|
|
|
if (constExprSampler.t_address != other.constExprSampler.t_address) { return false; }
|
|
|
|
if (constExprSampler.r_address != other.constExprSampler.r_address) { return false; }
|
|
|
|
if (constExprSampler.compare_func != other.constExprSampler.compare_func) { return false; }
|
|
|
|
if (constExprSampler.border_color != other.constExprSampler.border_color) { return false; }
|
|
|
|
if (constExprSampler.lod_clamp_min != other.constExprSampler.lod_clamp_min) { return false; }
|
|
|
|
if (constExprSampler.lod_clamp_max != other.constExprSampler.lod_clamp_max) { return false; }
|
|
|
|
if (constExprSampler.max_anisotropy != other.constExprSampler.max_anisotropy) { return false; }
|
2020-05-07 16:02:10 +02:00
|
|
|
|
|
|
|
if (constExprSampler.planes != other.constExprSampler.planes) { return false; }
|
|
|
|
if (constExprSampler.resolution != other.constExprSampler.resolution) { return false; }
|
|
|
|
if (constExprSampler.chroma_filter != other.constExprSampler.chroma_filter) { return false; }
|
|
|
|
if (constExprSampler.x_chroma_offset != other.constExprSampler.x_chroma_offset) { return false; }
|
|
|
|
if (constExprSampler.y_chroma_offset != other.constExprSampler.y_chroma_offset) { return false; }
|
|
|
|
for(uint32_t i = 0; i < 4; ++i)
|
|
|
|
if (constExprSampler.swizzle[i] != other.constExprSampler.swizzle[i]) { return false; }
|
|
|
|
if (constExprSampler.ycbcr_model != other.constExprSampler.ycbcr_model) { return false; }
|
|
|
|
if (constExprSampler.ycbcr_range != other.constExprSampler.ycbcr_range) { return false; }
|
|
|
|
if (constExprSampler.bpc != other.constExprSampler.bpc) { return false; }
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
if (constExprSampler.compare_enable != other.constExprSampler.compare_enable) { return false; }
|
|
|
|
if (constExprSampler.lod_clamp_enable != other.constExprSampler.lod_clamp_enable) { return false; }
|
|
|
|
if (constExprSampler.anisotropy_enable != other.constExprSampler.anisotropy_enable) { return false; }
|
2020-05-07 16:02:10 +02:00
|
|
|
if (constExprSampler.ycbcr_conversion_enable != other.constExprSampler.ycbcr_conversion_enable) { return false; }
|
2019-06-15 01:47:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionConfiguration::stageSupportsVertexAttributes() const {
|
2019-03-15 20:25:55 -04:00
|
|
|
return (options.entryPointStage == spv::ExecutionModelVertex ||
|
|
|
|
options.entryPointStage == spv::ExecutionModelTessellationControl ||
|
|
|
|
options.entryPointStage == spv::ExecutionModelTessellationEvaluation);
|
|
|
|
}
|
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
// Check them all in case inactive VA's duplicate locations used by active VA's.
|
2020-06-17 21:36:27 -05:00
|
|
|
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionConfiguration::isShaderInputLocationUsed(uint32_t location) const {
|
|
|
|
for (auto& si : shaderInputs) {
|
|
|
|
if ((si.shaderInput.location == location) && si.isUsedByShader) { return true; }
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
MVK_PUBLIC_SYMBOL uint32_t SPIRVToMSLConversionConfiguration::countShaderInputsAt(uint32_t binding) const {
|
|
|
|
uint32_t siCnt = 0;
|
|
|
|
for (auto& si : shaderInputs) {
|
|
|
|
if ((si.binding == binding) && si.isUsedByShader) { siCnt++; }
|
2020-05-31 14:16:12 -04:00
|
|
|
}
|
2020-06-17 21:36:27 -05:00
|
|
|
return siCnt;
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::markAllInputsAndResourcesUsed() {
|
|
|
|
for (auto& si : shaderInputs) { si.isUsedByShader = true; }
|
2019-01-21 11:32:15 -05:00
|
|
|
for (auto& rb : resourceBindings) { rb.isUsedByShader = true; }
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConversionConfiguration::matches(const SPIRVToMSLConversionConfiguration& other) const {
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
if ( !options.matches(other.options) ) { return false; }
|
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
for (const auto& si : shaderInputs) {
|
|
|
|
if (si.isUsedByShader && !containsMatching(other.shaderInputs, si)) { return false; }
|
2018-06-06 21:14:09 -04:00
|
|
|
}
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2018-03-19 10:58:46 -04:00
|
|
|
for (const auto& rb : resourceBindings) {
|
2019-06-29 18:01:07 -04:00
|
|
|
if (rb.isUsedByShader && !containsMatching(other.resourceBindings, rb)) { return false; }
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
2018-06-06 21:14:09 -04:00
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-29 15:50:51 -04:00
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::alignWith(const SPIRVToMSLConversionConfiguration& srcContext) {
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
for (auto& si : shaderInputs) {
|
|
|
|
si.isUsedByShader = false;
|
|
|
|
for (auto& srcSI : srcContext.shaderInputs) {
|
|
|
|
if (si.matches(srcSI)) { si.isUsedByShader = srcSI.isUsedByShader; }
|
2018-06-06 21:14:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
for (auto& rb : resourceBindings) {
|
|
|
|
rb.isUsedByShader = false;
|
|
|
|
for (auto& srcRB : srcContext.resourceBindings) {
|
|
|
|
if (rb.matches(srcRB)) { rb.isUsedByShader = srcRB.isUsedByShader; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark SPIRVToMSLConverter
|
|
|
|
|
|
|
|
MVK_PUBLIC_SYMBOL void SPIRVToMSLConverter::setSPIRV(const uint32_t* spirvCode, size_t length) {
|
|
|
|
_spirv.clear(); // Clear for reuse
|
|
|
|
_spirv.reserve(length);
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
_spirv.push_back(spirvCode[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
MVK_PUBLIC_SYMBOL bool SPIRVToMSLConverter::convert(SPIRVToMSLConversionConfiguration& context,
|
2017-11-17 11:14:29 -05:00
|
|
|
bool shouldLogSPIRV,
|
|
|
|
bool shouldLogMSL,
|
|
|
|
bool shouldLogGLSL) {
|
2019-03-28 11:58:05 -04:00
|
|
|
|
|
|
|
// Uncomment to write SPIR-V to file as a debugging aid
|
|
|
|
// ofstream spvFile("spirv.spv", ios::binary);
|
|
|
|
// spvFile.write((char*)_spirv.data(), _spirv.size() << 2);
|
|
|
|
// spvFile.close();
|
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
_wasConverted = true;
|
|
|
|
_resultLog.clear();
|
|
|
|
_msl.clear();
|
2019-06-29 18:01:07 -04:00
|
|
|
_shaderConversionResults.reset();
|
2017-11-17 11:14:29 -05:00
|
|
|
|
|
|
|
if (shouldLogSPIRV) { logSPIRV("Converting"); }
|
|
|
|
|
2019-03-29 11:21:19 +01:00
|
|
|
SPIRV_CROSS_NAMESPACE::CompilerMSL* pMSLCompiler = nullptr;
|
2018-05-04 12:11:19 -04:00
|
|
|
|
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
|
|
|
try {
|
|
|
|
#endif
|
2019-03-29 11:21:19 +01:00
|
|
|
pMSLCompiler = new SPIRV_CROSS_NAMESPACE::CompilerMSL(_spirv);
|
2018-05-04 12:11:19 -04:00
|
|
|
|
|
|
|
if (context.options.hasEntryPoint()) {
|
|
|
|
pMSLCompiler->set_entry_point(context.options.entryPointName, context.options.entryPointStage);
|
|
|
|
}
|
2018-03-19 10:58:46 -04:00
|
|
|
|
Add support for tessellation.
At long last, tessellation comes to MoltenVK! With this change, clients
will now be able to specify tessellation shaders when creating
pipelines, and then draw tessellated patches with them.
Unfortunately, there seem to be a few gotchas with tessellation in
Metal. For one thing, tessellation pipelines in Metal are structured
very differently from Vulkan. There is no tessellation control or even
vertex stage. Instead, the tessellation evaluation shader takes the
place of the vertex function as a "post-tessellation vertex function."
The tessellation levels are supplied in a buffer to the tessellator,
which you are expected to populate. The most common way to do this is by
running a compute shader. MoltenVK thus runs the vertex shader and
tessellation control shader by themselves; a single `VkPipeline` object
then requires at least *three* `MTLPipelineState` objects.
But wait, there's more! The tessellation-control-as-compute stage uses
Metal's support for vertex-style stage input to a compute shader. But,
this support requires one to declare indexing *ahead of time*, when the
pipeline state is created. So a single `VkPipeline` object could have as
many as *five* `MTLPipelineState` objects.
Further, if there are more output than input control points for the
tessellation control stage, then later invocations may end up fetching
the wrong attributes! To get around this, this change uses index buffers
to ensure that all tessellation control shaders see the correct input.
Unfortunately, in the indexed draw case, this means that the incoming
index buffer needs to be munged.
Instancing is another pain point here. In Vulkan, as in OpenGL and
Direct3D, instancing is done in the vertex shader; but in Metal, it is
done at the tessellation evaluation stage. For this reason, only the
vertex stage of a tessellated draw supports instancing. Additional
memory is required to hold data for the extra vertices generated by
instancing. This also requires still more munging of index buffers for
indexed draws.
Indirect draws are even more painful. Because the number of vertices and
instances is unknown, storage for the maximum possible number of
vertices must be allocated. This change imposes a totally arbitrary
limit of 131072 vertices from a single draw, including all vertices
generated by instancing. On a Mac, this requires about 194-256 MB of
VRAM for all the temporary buffers.
There are some possible optimizations here. If we could prove that the
vertex shader's output doesn't depend on the instance ID, either
directly or through a per-instance attribute, then we could avoid
running the vertex and tess. control stages per instance, and take
advantage of Metal's support for tess. eval instancing. If we could
also prove that the vertex shader simply passes instance attributes
through (similarly with the tess. control shader), we could do this for
many more instanced draws as well. It should also be possible to cache
the output from the tess. control stage; if the draw comes up again, we
can then skip the vertex and tess. control stages entirely!
Fixes #56 and #501.
2019-02-18 20:56:42 -06:00
|
|
|
// Set up tessellation parameters if needed.
|
|
|
|
if (context.options.entryPointStage == spv::ExecutionModelTessellationControl ||
|
|
|
|
context.options.entryPointStage == spv::ExecutionModelTessellationEvaluation) {
|
|
|
|
if (context.options.tessPatchKind != spv::ExecutionModeMax) {
|
|
|
|
pMSLCompiler->set_execution_mode(context.options.tessPatchKind);
|
|
|
|
}
|
|
|
|
if (context.options.numTessControlPoints != 0) {
|
|
|
|
pMSLCompiler->set_execution_mode(spv::ExecutionModeOutputVertices, context.options.numTessControlPoints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 12:11:19 -04:00
|
|
|
// Establish the MSL options for the compiler
|
|
|
|
// This needs to be done in two steps...for CompilerMSL and its superclass.
|
2019-06-15 01:47:13 -04:00
|
|
|
pMSLCompiler->set_msl_options(context.options.mslOptions);
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2018-05-04 12:11:19 -04:00
|
|
|
auto scOpts = pMSLCompiler->get_common_options();
|
|
|
|
scOpts.vertex.flip_vert_y = context.options.shouldFlipVertexY;
|
|
|
|
pMSLCompiler->set_common_options(scOpts);
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
// Add shader inputs
|
|
|
|
for (auto& si : context.shaderInputs) {
|
|
|
|
pMSLCompiler->add_msl_shader_input(si.shaderInput);
|
2019-03-15 20:25:55 -04:00
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Add resource bindings and hardcoded constexpr samplers
|
|
|
|
for (auto& rb : context.resourceBindings) {
|
|
|
|
auto& rbb = rb.resourceBinding;
|
|
|
|
pMSLCompiler->add_msl_resource_binding(rbb);
|
|
|
|
|
|
|
|
if (rb.requiresConstExprSampler) {
|
|
|
|
pMSLCompiler->remap_constexpr_sampler_by_binding(rbb.desc_set, rbb.binding, rb.constExprSampler);
|
|
|
|
}
|
2019-03-15 20:25:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_msl = pMSLCompiler->compile();
|
2018-07-29 15:50:51 -04:00
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
if (shouldLogMSL) { logSource(_msl, "MSL", "Converted"); }
|
2018-07-29 15:50:51 -04:00
|
|
|
|
2018-03-09 11:00:17 -06:00
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2019-03-29 11:21:19 +01:00
|
|
|
} catch (SPIRV_CROSS_NAMESPACE::CompilerError& ex) {
|
2017-11-17 11:14:29 -05:00
|
|
|
string errMsg("MSL conversion error: ");
|
|
|
|
errMsg += ex.what();
|
|
|
|
logError(errMsg.data());
|
2018-05-04 12:11:19 -04:00
|
|
|
if (shouldLogMSL && pMSLCompiler) {
|
|
|
|
_msl = pMSLCompiler->get_partial_source();
|
2017-11-17 11:14:29 -05:00
|
|
|
logSource(_msl, "MSL", "Partially converted");
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 11:00:17 -06:00
|
|
|
#endif
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
// Populate the shader conversion results with info from the compilation run,
|
|
|
|
// and mark which vertex attributes and resource bindings are used by the shader
|
|
|
|
populateEntryPoint(pMSLCompiler, context.options);
|
|
|
|
_shaderConversionResults.isRasterizationDisabled = pMSLCompiler && pMSLCompiler->get_is_rasterization_disabled();
|
|
|
|
_shaderConversionResults.needsSwizzleBuffer = pMSLCompiler && pMSLCompiler->needs_swizzle_buffer();
|
|
|
|
_shaderConversionResults.needsOutputBuffer = pMSLCompiler && pMSLCompiler->needs_output_buffer();
|
|
|
|
_shaderConversionResults.needsPatchOutputBuffer = pMSLCompiler && pMSLCompiler->needs_patch_output_buffer();
|
|
|
|
_shaderConversionResults.needsBufferSizeBuffer = pMSLCompiler && pMSLCompiler->needs_buffer_size_buffer();
|
|
|
|
_shaderConversionResults.needsInputThreadgroupMem = pMSLCompiler && pMSLCompiler->needs_input_threadgroup_mem();
|
2019-07-23 23:52:34 -05:00
|
|
|
_shaderConversionResults.needsDispatchBaseBuffer = pMSLCompiler && pMSLCompiler->needs_dispatch_base_buffer();
|
2018-07-29 15:50:51 -04:00
|
|
|
|
2020-06-17 21:36:27 -05:00
|
|
|
for (auto& ctxSI : context.shaderInputs) {
|
|
|
|
ctxSI.isUsedByShader = pMSLCompiler->is_msl_shader_input_used(ctxSI.shaderInput.location);
|
2018-07-29 15:50:51 -04:00
|
|
|
}
|
2019-03-15 20:25:55 -04:00
|
|
|
for (auto& ctxRB : context.resourceBindings) {
|
2019-06-15 01:47:13 -04:00
|
|
|
ctxRB.isUsedByShader = pMSLCompiler->is_msl_resource_binding_used(ctxRB.resourceBinding.stage,
|
|
|
|
ctxRB.resourceBinding.desc_set,
|
|
|
|
ctxRB.resourceBinding.binding);
|
2018-07-29 15:50:51 -04:00
|
|
|
}
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2019-03-15 20:25:55 -04:00
|
|
|
delete pMSLCompiler;
|
|
|
|
|
2017-11-17 11:14:29 -05:00
|
|
|
// To check GLSL conversion
|
|
|
|
if (shouldLogGLSL) {
|
2019-03-29 11:21:19 +01:00
|
|
|
SPIRV_CROSS_NAMESPACE::CompilerGLSL* pGLSLCompiler = nullptr;
|
2018-05-04 12:11:19 -04:00
|
|
|
|
2018-03-09 11:00:17 -06:00
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2018-05-04 12:11:19 -04:00
|
|
|
try {
|
2018-03-09 11:00:17 -06:00
|
|
|
#endif
|
2019-03-29 11:21:19 +01:00
|
|
|
pGLSLCompiler = new SPIRV_CROSS_NAMESPACE::CompilerGLSL(_spirv);
|
2018-12-17 12:06:02 -06:00
|
|
|
auto options = pGLSLCompiler->get_common_options();
|
|
|
|
options.vulkan_semantics = true;
|
|
|
|
options.separate_shader_objects = true;
|
|
|
|
pGLSLCompiler->set_common_options(options);
|
2018-05-04 12:11:19 -04:00
|
|
|
string glsl = pGLSLCompiler->compile();
|
2017-11-17 11:14:29 -05:00
|
|
|
logSource(glsl, "GLSL", "Estimated original");
|
2018-03-09 11:00:17 -06:00
|
|
|
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
|
2019-03-29 11:21:19 +01:00
|
|
|
} catch (SPIRV_CROSS_NAMESPACE::CompilerError& ex) {
|
2017-11-17 11:14:29 -05:00
|
|
|
string errMsg("Original GLSL extraction error: ");
|
|
|
|
errMsg += ex.what();
|
|
|
|
logMsg(errMsg.data());
|
2018-05-04 12:11:19 -04:00
|
|
|
if (pGLSLCompiler) {
|
|
|
|
string glsl = pGLSLCompiler->get_partial_source();
|
|
|
|
logSource(glsl, "GLSL", "Partially converted");
|
|
|
|
}
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
2018-03-09 11:00:17 -06:00
|
|
|
#endif
|
2018-09-14 14:01:22 -04:00
|
|
|
delete pGLSLCompiler;
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return _wasConverted;
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Appends the message text to the result log.
|
2017-11-17 11:14:29 -05:00
|
|
|
void SPIRVToMSLConverter::logMsg(const char* logMsg) {
|
|
|
|
string trimMsg = trim(logMsg);
|
|
|
|
if ( !trimMsg.empty() ) {
|
|
|
|
_resultLog += trimMsg;
|
|
|
|
_resultLog += "\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Appends the error text to the result log, sets the wasConverted property to false, and returns it.
|
2017-11-17 11:14:29 -05:00
|
|
|
bool SPIRVToMSLConverter::logError(const char* errMsg) {
|
|
|
|
logMsg(errMsg);
|
|
|
|
_wasConverted = false;
|
|
|
|
return _wasConverted;
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Appends the SPIR-V to the result log, indicating whether it is being converted or was converted.
|
2017-11-17 11:14:29 -05:00
|
|
|
void SPIRVToMSLConverter::logSPIRV(const char* opDesc) {
|
|
|
|
|
|
|
|
string spvLog;
|
|
|
|
mvk::logSPIRV(_spirv, spvLog);
|
|
|
|
|
|
|
|
_resultLog += opDesc;
|
|
|
|
_resultLog += " SPIR-V:\n";
|
|
|
|
_resultLog += spvLog;
|
|
|
|
_resultLog += "\nEnd SPIR-V\n\n";
|
2018-01-08 21:44:46 -05:00
|
|
|
|
|
|
|
// Uncomment one or both of the following lines to get additional debugging and tracability capabilities.
|
|
|
|
// The SPIR-V can be written in binary form to a file, and/or logged in human readable form to the console.
|
|
|
|
// These can be helpful if errors occur during conversion of SPIR-V to MSL.
|
|
|
|
// writeSPIRVToFile("spvout.spv");
|
|
|
|
// printf("\n%s\n", getResultLog().c_str());
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Writes the SPIR-V code to a file. This can be useful for debugging
|
|
|
|
// when the SPRIR-V did not originally come from a known file
|
2018-01-08 21:44:46 -05:00
|
|
|
void SPIRVToMSLConverter::writeSPIRVToFile(string spvFilepath) {
|
|
|
|
vector<char> fileContents;
|
|
|
|
spirvToBytes(_spirv, fileContents);
|
|
|
|
string errMsg;
|
|
|
|
if (writeFile(spvFilepath, fileContents, errMsg)) {
|
|
|
|
_resultLog += "Saved SPIR-V to file: " + absolutePath(spvFilepath) + "\n\n";
|
|
|
|
} else {
|
|
|
|
_resultLog += "Could not write SPIR-V file. " + errMsg + "\n\n";
|
|
|
|
}
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Validates that the SPIR-V code will disassemble during logging.
|
2017-11-17 11:14:29 -05:00
|
|
|
bool SPIRVToMSLConverter::validateSPIRV() {
|
|
|
|
if (_spirv.size() < 5) { return false; }
|
|
|
|
if (_spirv[0] != spv::MagicNumber) { return false; }
|
|
|
|
if (_spirv[4] != 0) { return false; }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:47:13 -04:00
|
|
|
// Appends the source to the result log, prepending with the operation.
|
2017-11-17 11:14:29 -05:00
|
|
|
void SPIRVToMSLConverter::logSource(string& src, const char* srcLang, const char* opDesc) {
|
|
|
|
_resultLog += opDesc;
|
|
|
|
_resultLog += " ";
|
|
|
|
_resultLog += srcLang;
|
|
|
|
_resultLog += ":\n";
|
|
|
|
_resultLog += src;
|
|
|
|
_resultLog += "\nEnd ";
|
|
|
|
_resultLog += srcLang;
|
|
|
|
_resultLog += "\n\n";
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
void SPIRVToMSLConverter::populateWorkgroupDimension(SPIRVWorkgroupSizeDimension& wgDim,
|
|
|
|
uint32_t size,
|
|
|
|
SPIRV_CROSS_NAMESPACE::SpecializationConstant& spvSpecConst) {
|
2018-07-03 13:57:53 -04:00
|
|
|
wgDim.size = max(size, 1u);
|
2019-09-06 12:27:40 -05:00
|
|
|
wgDim.isSpecialized = (uint32_t(spvSpecConst.id) != 0);
|
2018-07-03 13:57:53 -04:00
|
|
|
wgDim.specializationID = spvSpecConst.constant_id;
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
// Populates the entry point with info extracted from the SPRI-V compiler.
|
|
|
|
void SPIRVToMSLConverter::populateEntryPoint(SPIRV_CROSS_NAMESPACE::Compiler* pCompiler,
|
|
|
|
SPIRVToMSLConversionOptions& options) {
|
2018-05-04 12:11:19 -04:00
|
|
|
|
|
|
|
if ( !pCompiler ) { return; }
|
2017-11-17 11:14:29 -05:00
|
|
|
|
2019-03-29 11:21:19 +01:00
|
|
|
SPIRV_CROSS_NAMESPACE::SPIREntryPoint spvEP;
|
2018-03-19 10:58:46 -04:00
|
|
|
if (options.hasEntryPoint()) {
|
2018-05-04 12:11:19 -04:00
|
|
|
spvEP = pCompiler->get_entry_point(options.entryPointName, options.entryPointStage);
|
2018-03-19 10:58:46 -04:00
|
|
|
} else {
|
2018-05-04 12:11:19 -04:00
|
|
|
const auto& entryPoints = pCompiler->get_entry_points_and_stages();
|
2018-03-19 10:58:46 -04:00
|
|
|
if ( !entryPoints.empty() ) {
|
|
|
|
auto& ep = entryPoints[0];
|
2018-05-04 12:11:19 -04:00
|
|
|
spvEP = pCompiler->get_entry_point(ep.name, ep.execution_model);
|
2018-03-19 10:58:46 -04:00
|
|
|
}
|
|
|
|
}
|
2017-12-26 22:20:20 -05:00
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
auto& ep = _shaderConversionResults.entryPoint;
|
|
|
|
ep.mtlFunctionName = spvEP.name;
|
|
|
|
|
2019-03-29 11:21:19 +01:00
|
|
|
SPIRV_CROSS_NAMESPACE::SpecializationConstant widthSC, heightSC, depthSC;
|
2018-07-03 13:57:53 -04:00
|
|
|
pCompiler->get_work_group_size_specialization_constants(widthSC, heightSC, depthSC);
|
2018-02-14 13:08:27 +01:00
|
|
|
|
2019-06-29 18:01:07 -04:00
|
|
|
auto& wgSize = ep.workgroupSize;
|
|
|
|
populateWorkgroupDimension(wgSize.width, spvEP.workgroup_size.x, widthSC);
|
|
|
|
populateWorkgroupDimension(wgSize.height, spvEP.workgroup_size.y, heightSC);
|
|
|
|
populateWorkgroupDimension(wgSize.depth, spvEP.workgroup_size.z, depthSC);
|
2017-11-17 11:14:29 -05:00
|
|
|
}
|