Skip to content

Commit 7c37b2e

Browse files
committed
default pipeline
1 parent 9554fc9 commit 7c37b2e

File tree

4 files changed

+203
-137
lines changed

4 files changed

+203
-137
lines changed

include/irr/builtin/shaders/loaders/mtl/fragment_impl.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ vec3 irr_computeLighting(out irr_glsl_IsotropicViewSurfaceInteraction out_intera
209209

210210
void main()
211211
{
212+
#ifndef _NO_UV
212213
mat2 dUV = mat2(dFdx(UV), dFdy(UV));
213-
214+
#else
215+
mat2 dUV = mat2(vec2(0,0),vec2(0,0));
216+
#endif
214217
irr_glsl_IsotropicViewSurfaceInteraction interaction;
215218
vec3 color = irr_computeLighting(interaction, dUV);
216219

src/irr/asset/CGraphicsPipelineLoaderMTL.cpp

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "irr/asset/CGraphicsPipelineLoaderMTL.h"
1111
#include "irr/asset/IGLSLEmbeddedIncludeLoader.h"
12+
#include "irr/builtin/MTLdefaults.h"
1213

1314

1415
namespace
@@ -116,13 +117,33 @@ void main()
116117
using namespace irr;
117118
using namespace asset;
118119

120+
static void insertPipelineIntoCache(core::smart_refctd_ptr<ICPURenderpassIndependentPipeline>&& asset, const char* path, IAssetManager* _assetMgr)
121+
{
122+
asset::SAssetBundle bundle({ std::move(asset) });
123+
_assetMgr->changeAssetKey(bundle, path);
124+
_assetMgr->insertAssetIntoCache(bundle);
125+
}
119126
static void insertShaderIntoCache(core::smart_refctd_ptr<ICPUSpecializedShader>& asset, const char* path, IAssetManager* _assetMgr)
120127
{
121128
asset::SAssetBundle bundle({ asset });
122129
_assetMgr->changeAssetKey(bundle, path);
123130
_assetMgr->insertAssetIntoCache(bundle);
124131
}
132+
template<typename AssetType, IAsset::E_TYPE assetType>
133+
static core::smart_refctd_ptr<AssetType> getDefaultAsset(const char* _key, IAssetManager* _assetMgr)
134+
{
135+
size_t storageSz = 1ull;
136+
asset::SAssetBundle bundle;
137+
const IAsset::E_TYPE types[]{ assetType, static_cast<IAsset::E_TYPE>(0u) };
138+
139+
_assetMgr->findAssets(storageSz, &bundle, _key, types);
140+
if (bundle.isEmpty())
141+
return nullptr;
142+
auto assets = bundle.getContents();
143+
//assert(assets.first != assets.second);
125144

145+
return core::smart_refctd_ptr_static_cast<AssetType>(assets.begin()[0]);
146+
}
126147
#define VERT_SHADER_NO_UV_CACHE_KEY "irr/builtin/shaders/loaders/mtl/vertex_no_uv.vert"
127148
#define VERT_SHADER_UV_CACHE_KEY "irr/builtin/shaders/loaders/mtl/vertex_uv.vert"
128149
#define FRAG_SHADER_NO_UV_CACHE_KEY "irr/builtin/shaders/loaders/mtl/fragment_no_uv.frag"
@@ -148,6 +169,55 @@ CGraphicsPipelineLoaderMTL::CGraphicsPipelineLoaderMTL(IAssetManager* _am) : m_a
148169
registerShader(IRR_CORE_UNIQUE_STRING_LITERAL_TYPE(VERT_SHADER_UV_CACHE_KEY) {}, ICPUSpecializedShader::ESS_VERTEX);
149170
registerShader(IRR_CORE_UNIQUE_STRING_LITERAL_TYPE(FRAG_SHADER_NO_UV_CACHE_KEY){},ICPUSpecializedShader::ESS_FRAGMENT);
150171
registerShader(IRR_CORE_UNIQUE_STRING_LITERAL_TYPE(FRAG_SHADER_UV_CACHE_KEY){},ICPUSpecializedShader::ESS_FRAGMENT);
172+
173+
174+
constexpr const char* MISSING_MTL_PIPELINE_CACHE_KEY = "irr/builtin/graphics_pipeline/loaders/mtl/missing_material_pipeline";
175+
176+
/*
177+
if possible, it would be great to use CImageLoaderPNG here
178+
179+
auto imgDataBuf = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(MISSING_CHECKERBOARD_TEXTURE_CONTENT));
180+
void* dst = reinterpret_cast<uint8_t*>(imgDataBuf->getPointer());
181+
const void* src = &MISSING_CHECKERBOARD_TEXTURE_CONTENT;
182+
memcpy(dst, src, sizeof(MISSING_CHECKERBOARD_TEXTURE_CONTENT));
183+
ICPUImage::SCreationParams imageParams;
184+
imageParams.type =
185+
ICPUImage img();
186+
187+
188+
189+
190+
*/
191+
auto texture_file = m_assetMgr->getFileSystem()->createMemoryReadFile(MISSING_CHECKERBOARD_TEXTURE_CONTENT, sizeof(MISSING_CHECKERBOARD_TEXTURE_CONTENT), "checkerboard.png");
192+
193+
SAssetLoadParams params;
194+
auto imageBundle = m_assetMgr->getAsset(texture_file, std::string("checkerboard.png"), params);
195+
auto image = core::smart_refctd_ptr_dynamic_cast<ICPUImage>(imageBundle.getContents().begin()[0]);
196+
ICPUImageView::SCreationParams viewParams;
197+
viewParams.flags = static_cast<ICPUImageView::E_CREATE_FLAGS>(0u);
198+
viewParams.format = EF_UNKNOWN;
199+
viewParams.viewType = IImageView<ICPUImage>::ET_2D;
200+
viewParams.subresourceRange.baseArrayLayer = 0u;
201+
viewParams.subresourceRange.layerCount = 1u;
202+
viewParams.subresourceRange.baseMipLevel = 0u;
203+
viewParams.subresourceRange.levelCount = 1u;
204+
viewParams.image = std::move(image);
205+
206+
image_views_set_t views;
207+
views[0] = ICPUImageView::create(std::move(viewParams));
208+
209+
auto file = m_assetMgr->getFileSystem()->createMemoryReadFile(DUMMY_MTL_CONTENT, strlen(DUMMY_MTL_CONTENT), "default IrrlichtBAW material");
210+
auto bundle = loadAsset(file, params);
211+
auto pipeline = static_cast< ICPURenderpassIndependentPipeline*>( bundle.getContents().begin()->get());
212+
file->drop();
213+
auto p = core::smart_refctd_ptr<ICPURenderpassIndependentPipeline>(pipeline);
214+
auto ds3 = makeDescSet(std::move(views), p->getLayout()->getDescriptorSetLayout(3u));
215+
216+
SAssetBundle ds3Bundle{ ds3 };
217+
m_assetMgr->changeAssetKey(ds3Bundle, MISSING_CHECKERBOARD_TEXTURE_CACHE_KEY);
218+
m_assetMgr->insertAssetIntoCache(ds3Bundle);
219+
220+
insertPipelineIntoCache(std::move(p), MISSING_MTL_PIPELINE_CACHE_KEY, m_assetMgr);
151221
}
152222

153223
bool CGraphicsPipelineLoaderMTL::isALoadableFileFormat(io::IReadFile* _file) const
@@ -167,21 +237,6 @@ bool CGraphicsPipelineLoaderMTL::isALoadableFileFormat(io::IReadFile* _file) con
167237
return mtl.find("newmtl") != std::string::npos;
168238
}
169239

170-
template<typename AssetType, IAsset::E_TYPE assetType>
171-
static core::smart_refctd_ptr<AssetType> getDefaultAsset(const char* _key, IAssetManager* _assetMgr)
172-
{
173-
size_t storageSz = 1ull;
174-
asset::SAssetBundle bundle;
175-
const IAsset::E_TYPE types[]{ assetType, static_cast<IAsset::E_TYPE>(0u) };
176-
177-
_assetMgr->findAssets(storageSz, &bundle, _key, types);
178-
if (bundle.isEmpty())
179-
return nullptr;
180-
auto assets = bundle.getContents();
181-
//assert(assets.first != assets.second);
182-
183-
return core::smart_refctd_ptr_static_cast<AssetType>(assets.begin()[0]);
184-
}
185240

186241
core::smart_refctd_ptr<ICPUPipelineLayout> CGraphicsPipelineLoaderMTL::makePipelineLayoutFromMtl(const SMtl& _mtl, bool _noDS3)
187242
{
@@ -324,19 +379,30 @@ SAssetBundle CGraphicsPipelineLoaderMTL::loadAsset(io::IReadFile* _file, const I
324379

325380
core::smart_refctd_ptr<ICPUDescriptorSet> ds3;
326381
{
327-
const std::string dsCacheKey = std::string(fullName.c_str())+"?"+materials[i].name+"?_ds";
328-
const asset::IAsset::E_TYPE types[]{ asset::IAsset::ET_DESCRIPTOR_SET, (asset::IAsset::E_TYPE)0u };
329-
auto ds_bundle = _override->findCachedAsset(dsCacheKey, types, ctx.inner, _hierarchyLevel + ICPUMesh::DESC_SET_HIERARCHYLEVELS_BELOW);
330-
if (!ds_bundle.isEmpty())
331-
ds3 = core::smart_refctd_ptr_static_cast<ICPUDescriptorSet>(ds_bundle.getContents().begin()[0]);
332-
else
382+
const std::string dsCacheKey = std::string(fullName.c_str()) + "?" + materials[i].name + "?_ds";
383+
if (_override)
333384
{
334-
auto views = loadImages(relPath.c_str(), materials[i], ctx);
335-
ds3 = makeDescSet(std::move(views), layout->getDescriptorSetLayout(3u));
336-
if (ds3)
385+
const asset::IAsset::E_TYPE types[]{ asset::IAsset::ET_DESCRIPTOR_SET, (asset::IAsset::E_TYPE)0u };
386+
auto ds_bundle = _override->findCachedAsset(dsCacheKey, types, ctx.inner, _hierarchyLevel + ICPUMesh::DESC_SET_HIERARCHYLEVELS_BELOW);
387+
if (!ds_bundle.isEmpty())
388+
ds3 = core::smart_refctd_ptr_static_cast<ICPUDescriptorSet>(ds_bundle.getContents().begin()[0]);
389+
else
337390
{
338-
SAssetBundle bundle{ds3};
339-
_override->insertAssetIntoCache(bundle, dsCacheKey, ctx.inner, _hierarchyLevel + ICPURenderpassIndependentPipeline::DESC_SET_HIERARCHYLEVELS_BELOW);
391+
auto views = loadImages(relPath.c_str(), materials[i], ctx);
392+
ds3 = makeDescSet(std::move(views), layout->getDescriptorSetLayout(3u));
393+
if (ds3)
394+
{
395+
SAssetBundle bundle{ ds3 };
396+
_override->insertAssetIntoCache(bundle, dsCacheKey, ctx.inner, _hierarchyLevel + ICPURenderpassIndependentPipeline::DESC_SET_HIERARCHYLEVELS_BELOW);
397+
}
398+
}
399+
}
400+
else {
401+
const asset::IAsset::E_TYPE types[]{ asset::IAsset::ET_DESCRIPTOR_SET, (asset::IAsset::E_TYPE)0u };
402+
auto b = m_assetMgr->findAssets(dsCacheKey, types);
403+
if (b)
404+
{
405+
ds3 = core::smart_refctd_ptr_dynamic_cast<ICPUDescriptorSet>(b->begin()->getContents().begin()[0]);
340406
}
341407
}
342408
}
@@ -662,7 +728,6 @@ auto CGraphicsPipelineLoaderMTL::loadImages(const char* _relDir, const SMtl& _mt
662728
ICPUImage::SCreationParams cubemapParams = images[CMTLPipelineMetadata::EMP_REFL_POSX]->getCreationParameters();
663729
cubemapParams.arrayLayers = 6u;
664730
cubemapParams.type = IImage::ET_2D;
665-
666731
auto cubemap = ICPUImage::create(std::move(cubemapParams));
667732
auto regions = core::make_refctd_dynamic_array<core::smart_refctd_dynamic_array<ICPUImage::SBufferCopy>>(regions_);
668733
cubemap->setBufferAndRegions(std::move(imgDataBuf), regions);

src/irr/asset/COBJMeshFileLoader.cpp

Lines changed: 2 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "COBJMeshFileLoader.h"
1818

1919

20-
2120
namespace irr
2221
{
2322
namespace asset
@@ -51,8 +50,7 @@ static core::smart_refctd_ptr<AssetType> getDefaultAsset(const char* _key, IAsse
5150

5251
static const uint32_t WORD_BUFFER_LENGTH = 512;
5352

54-
constexpr const char* DEFAULT_MTL_PIPELINE_UV_CACHE_KEY = "irr/builtin/graphics_pipeline/loaders/mtl/default_uv";
55-
constexpr const char* DEFAULT_MTL_PIPELINE_NO_UV_CACHE_KEY = "irr/builtin/graphics_pipeline/loaders/mtl/default_no_uv";
53+
constexpr const char* MISSING_MTL_PIPELINE_CACHE_KEY = "irr/builtin/graphics_pipeline/loaders/mtl/missing_material_pipeline";
5654

5755
constexpr uint32_t POSITION = 0u;
5856
constexpr uint32_t UV = 2u;
@@ -62,111 +60,6 @@ constexpr uint32_t BND_NUM = 0u;
6260
//! Constructor
6361
COBJMeshFileLoader::COBJMeshFileLoader(IAssetManager* _manager) : AssetManager(_manager), FileSystem(_manager->getFileSystem())
6462
{
65-
SVertexInputParams vtxParams;
66-
SBlendParams blendParams;
67-
SPrimitiveAssemblyParams primParams;
68-
SRasterizationParams rasterParams;
69-
70-
blendParams.blendParams[0].blendEnable = true;
71-
blendParams.blendParams[0].srcColorFactor = EBF_ONE;
72-
blendParams.blendParams[0].srcAlphaFactor = EBF_ONE;
73-
blendParams.blendParams[0].dstColorFactor = EBF_ONE_MINUS_SRC_ALPHA;
74-
blendParams.blendParams[0].dstAlphaFactor = EBF_ONE_MINUS_SRC_ALPHA;
75-
76-
vtxParams.enabledAttribFlags = (1u << POSITION) | (1u << NORMAL);
77-
vtxParams.enabledBindingFlags = 1u << BND_NUM;
78-
vtxParams.bindings[BND_NUM].stride = sizeof(SObjVertex);
79-
vtxParams.bindings[BND_NUM].inputRate = EVIR_PER_VERTEX;
80-
//position
81-
vtxParams.attributes[POSITION].binding = BND_NUM;
82-
vtxParams.attributes[POSITION].format = EF_R32G32B32_SFLOAT;
83-
vtxParams.attributes[POSITION].relativeOffset = offsetof(SObjVertex, pos);
84-
//normal
85-
vtxParams.attributes[NORMAL].binding = BND_NUM;
86-
vtxParams.attributes[NORMAL].format = EF_A2B10G10R10_SNORM_PACK32;
87-
vtxParams.attributes[NORMAL].relativeOffset = offsetof(SObjVertex, normal32bit);
88-
89-
90-
91-
core::smart_refctd_ptr<ICPUPipelineLayout> layout;
92-
{
93-
auto bindings = core::make_refctd_dynamic_array<core::smart_refctd_dynamic_array<ICPUDescriptorSetLayout::SBinding>>(static_cast<size_t>(CMTLPipelineMetadata::EMP_REFL_POSX) + 1ull);
94-
95-
ICPUDescriptorSetLayout::SBinding bnd;
96-
bnd.count = 1u;
97-
bnd.stageFlags = ICPUSpecializedShader::ESS_FRAGMENT;
98-
bnd.type = EDT_COMBINED_IMAGE_SAMPLER;
99-
bnd.binding = 0u;
100-
std::fill(bindings->begin(), bindings->end(), bnd);
101-
102-
103-
core::smart_refctd_ptr<ICPUSampler> samplers[2];
104-
samplers[0] = getDefaultAsset<ICPUSampler, IAsset::ET_SAMPLER>("irr/builtin/samplers/default", AssetManager);
105-
samplers[1] = getDefaultAsset<ICPUSampler, IAsset::ET_SAMPLER>("irr/builtin/samplers/default_clamp_to_border", AssetManager);
106-
for (uint32_t i = 0u; i <= CMTLPipelineMetadata::EMP_REFL_POSX; ++i)
107-
{
108-
(*bindings)[i].binding = i;
109-
(*bindings)[i].samplers = samplers;
110-
}
111-
112-
113-
auto ds1layout = getDefaultAsset<ICPUDescriptorSetLayout, IAsset::ET_DESCRIPTOR_SET_LAYOUT>("irr/builtin/descriptor_set_layout/basic_view_parameters", AssetManager);
114-
core::smart_refctd_ptr<ICPUDescriptorSetLayout> ds3Layout = core::make_smart_refctd_ptr<ICPUDescriptorSetLayout>(bindings->begin(), bindings->end());
115-
SPushConstantRange pcRng;
116-
pcRng.stageFlags = ICPUSpecializedShader::ESS_FRAGMENT;
117-
pcRng.offset = 0u;
118-
pcRng.size = sizeof(208u);
119-
layout = core::make_smart_refctd_ptr<ICPUPipelineLayout>(&pcRng, &pcRng + 1, nullptr, std::move(ds1layout), nullptr, std::move(ds3Layout));
120-
121-
}
122-
std::pair<core::smart_refctd_ptr<ICPUSpecializedShader>, core::smart_refctd_ptr<ICPUSpecializedShader>> shaders[2];
123-
shaders[0] = {
124-
std::move(getDefaultAsset<ICPUSpecializedShader, IAsset::ET_SPECIALIZED_SHADER>("irr/builtin/shaders/loaders/mtl/vertex_no_uv.vert", AssetManager)),
125-
std::move(getDefaultAsset<ICPUSpecializedShader, IAsset::ET_SPECIALIZED_SHADER>("irr/builtin/shaders/loaders/mtl/fragment_no_uv.frag", AssetManager))
126-
};
127-
shaders[1] = {
128-
std::move(getDefaultAsset<ICPUSpecializedShader, IAsset::ET_SPECIALIZED_SHADER>("irr/builtin/shaders/loaders/mtl/vertex_uv.vert", AssetManager)),
129-
std::move(getDefaultAsset<ICPUSpecializedShader, IAsset::ET_SPECIALIZED_SHADER>("irr/builtin/shaders/loaders/mtl/fragment_uv.frag", AssetManager))
130-
};
131-
132-
constexpr size_t DS1_METADATA_ENTRY_CNT = 3ull;
133-
core::smart_refctd_dynamic_array<IPipelineMetadata::ShaderInputSemantic> shaderInputsMetadata = core::make_refctd_dynamic_array<decltype(shaderInputsMetadata)>(DS1_METADATA_ENTRY_CNT);
134-
{
135-
ICPUDescriptorSetLayout* ds1layout = layout->getDescriptorSetLayout(1u);
136-
137-
constexpr IPipelineMetadata::E_COMMON_SHADER_INPUT types[DS1_METADATA_ENTRY_CNT]{ IPipelineMetadata::ECSI_WORLD_VIEW_PROJ, IPipelineMetadata::ECSI_WORLD_VIEW, IPipelineMetadata::ECSI_WORLD_VIEW_INVERSE_TRANSPOSE };
138-
constexpr uint32_t sizes[DS1_METADATA_ENTRY_CNT]{ sizeof(SBasicViewParameters::MVP), sizeof(SBasicViewParameters::MV), sizeof(SBasicViewParameters::NormalMat) };
139-
constexpr uint32_t relOffsets[DS1_METADATA_ENTRY_CNT]{ offsetof(SBasicViewParameters,MVP), offsetof(SBasicViewParameters,MV), offsetof(SBasicViewParameters,NormalMat) };
140-
for (uint32_t i = 0u; i < DS1_METADATA_ENTRY_CNT; ++i)
141-
{
142-
auto& semantic = (shaderInputsMetadata->end() - i - 1u)[0];
143-
semantic.type = types[i];
144-
semantic.descriptorSection.type = IPipelineMetadata::ShaderInput::ET_UNIFORM_BUFFER;
145-
semantic.descriptorSection.uniformBufferObject.binding = ds1layout->getBindings().begin()[0].binding;
146-
semantic.descriptorSection.uniformBufferObject.set = 1u;
147-
semantic.descriptorSection.uniformBufferObject.relByteoffset = relOffsets[i];
148-
semantic.descriptorSection.uniformBufferObject.bytesize = sizes[i];
149-
semantic.descriptorSection.shaderAccessFlags = ICPUSpecializedShader::ESS_VERTEX;
150-
}
151-
}
152-
CMTLPipelineMetadata::SMTLMaterialParameters materialparams;
153-
auto pipelineNoUV = core::make_smart_refctd_ptr<ICPURenderpassIndependentPipeline>(core::smart_refctd_ptr_static_cast<ICPUPipelineLayout>(layout->clone()), nullptr, nullptr, vtxParams, blendParams, primParams, rasterParams);
154-
pipelineNoUV->setShaderAtIndex(ICPURenderpassIndependentPipeline::ESSI_VERTEX_SHADER_IX, shaders[0].first.get());
155-
pipelineNoUV->setShaderAtIndex(ICPURenderpassIndependentPipeline::ESSI_FRAGMENT_SHADER_IX, shaders[0].second.get());
156-
AssetManager->setAssetMetadata(pipelineNoUV.get(), core::make_smart_refctd_ptr<CMTLPipelineMetadata>(materialparams, std::string("Default no-uv material"), nullptr, 0u, core::smart_refctd_ptr(shaderInputsMetadata)));
157-
insertPipelineIntoCache(std::move(pipelineNoUV), DEFAULT_MTL_PIPELINE_NO_UV_CACHE_KEY, AssetManager);
158-
159-
//uv
160-
vtxParams.enabledAttribFlags |= (1u << UV);
161-
vtxParams.attributes[UV].binding = BND_NUM;
162-
vtxParams.attributes[UV].format = EF_R32G32_SFLOAT;
163-
vtxParams.attributes[UV].relativeOffset = offsetof(SObjVertex, uv);
164-
165-
auto pipelineUV = core::make_smart_refctd_ptr<ICPURenderpassIndependentPipeline>(std::move(layout), nullptr, nullptr, vtxParams, blendParams, primParams, rasterParams);
166-
pipelineUV->setShaderAtIndex(ICPURenderpassIndependentPipeline::ESSI_VERTEX_SHADER_IX, shaders[1].first.get());
167-
pipelineUV->setShaderAtIndex(ICPURenderpassIndependentPipeline::ESSI_FRAGMENT_SHADER_IX, shaders[1].second.get());
168-
AssetManager->setAssetMetadata(pipelineUV.get(), core::make_smart_refctd_ptr<CMTLPipelineMetadata>(materialparams, std::string("Default material"), nullptr, 0u, core::smart_refctd_ptr(shaderInputsMetadata)));
169-
insertPipelineIntoCache(std::move(pipelineUV), DEFAULT_MTL_PIPELINE_UV_CACHE_KEY, AssetManager);
17063
}
17164

17265

@@ -523,7 +416,7 @@ asset::SAssetBundle COBJMeshFileLoader::loadAsset(io::IReadFile* _file, const as
523416
//if there's no pipeline for this meshbuffer, set dummy one
524417
if (!submeshes[i]->getPipeline())
525418
{
526-
auto pipeline = getDefaultAsset<ICPURenderpassIndependentPipeline, IAsset::ET_RENDERPASS_INDEPENDENT_PIPELINE>(hasUV? DEFAULT_MTL_PIPELINE_UV_CACHE_KEY : DEFAULT_MTL_PIPELINE_NO_UV_CACHE_KEY, AssetManager);
419+
auto pipeline = getDefaultAsset<ICPURenderpassIndependentPipeline, IAsset::ET_RENDERPASS_INDEPENDENT_PIPELINE>(MISSING_MTL_PIPELINE_CACHE_KEY, AssetManager);
527420
submeshes[i]->setPipeline(std::move(pipeline));
528421
}
529422
}

0 commit comments

Comments
 (0)