Skip to content

Commit b9f4b86

Browse files
committed
parsing shaders (untested)
1 parent e6beea8 commit b9f4b86

File tree

13 files changed

+270
-128
lines changed

13 files changed

+270
-128
lines changed

.github/workflows/tests.yml

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,59 @@ jobs:
163163
ldd libcage-engine.so
164164
165165
166+
MacOS:
167+
name: macos-${{ matrix.os-version }}-${{ matrix.build-config }}
168+
runs-on: macos-${{ matrix.os-version }}
169+
strategy:
170+
fail-fast: false
171+
matrix:
172+
os-version: [14, 15]
173+
build-config: [debug, release]
174+
175+
steps:
176+
- name: Install packages
177+
run: |
178+
brew install nasm
179+
180+
- name: Versions
181+
run: |
182+
cmake --version
183+
git --version
184+
clang --version
185+
clang++ --version
186+
187+
- uses: actions/checkout@v4
188+
189+
- name: Configure
190+
run: |
191+
mkdir build
192+
cd build
193+
cmake -DCMAKE_BUILD_TYPE=${{ matrix.build-config }} ..
194+
195+
- name: Build
196+
run: |
197+
cd build
198+
cmake --build . -- -j$(( `sysctl -n hw.ncpu` + 1 ))
199+
200+
- name: Tests
201+
run: |
202+
cd build/result/${{ matrix.build-config }}
203+
./cage-test-core
204+
205+
- name: Assets
206+
run: |
207+
cd build/result/${{ matrix.build-config }}
208+
./cage-asset-database
209+
210+
- name: List dependencies
211+
run: |
212+
cd build/result/${{ matrix.build-config }}
213+
echo "cage-core:"
214+
otool -L libcage-core.dylib
215+
echo "cage-engine:"
216+
otool -L libcage-engine.dylib
217+
218+
166219
NoProfiler:
167220
name: no-profiler
168221
needs: Ubuntu
@@ -257,51 +310,3 @@ jobs:
257310
run: |
258311
cd build/result/${{ matrix.build-config }}
259312
./cage-test-core
260-
261-
262-
MacOS:
263-
name: macos-${{ matrix.os-version }}-${{ matrix.build-config }}
264-
runs-on: macos-${{ matrix.os-version }}
265-
strategy:
266-
fail-fast: false
267-
matrix:
268-
os-version: [14, 15]
269-
build-config: [debug, release]
270-
271-
steps:
272-
- name: Install packages
273-
run: |
274-
brew install nasm
275-
276-
- name: Versions
277-
run: |
278-
cmake --version
279-
git --version
280-
clang --version
281-
clang++ --version
282-
283-
- uses: actions/checkout@v4
284-
285-
- name: Configure
286-
run: |
287-
mkdir build
288-
cd build
289-
cmake -DCMAKE_BUILD_TYPE=${{ matrix.build-config }} ..
290-
291-
- name: Build
292-
run: |
293-
cd build
294-
cmake --build . -- -j$(( `sysctl -n hw.ncpu` + 1 ))
295-
296-
- name: Tests
297-
run: |
298-
cd build/result/${{ matrix.build-config }}
299-
./cage-test-core
300-
301-
- name: List dependencies
302-
run: |
303-
cd build/result/${{ matrix.build-config }}
304-
echo "cage-core:"
305-
otool -L libcage-core.dylib
306-
echo "cage-engine:"
307-
otool -L libcage-engine.dylib

data/cage/shaders/effects/effects.assets

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
scheme = pack
33
effects.pack
44

5-
[]
6-
scheme = shader
7-
bloomApply.glsl
8-
bloomGenerate.glsl
9-
dofApply.glsl
10-
dofCollect.glsl
11-
fxaa.glsl
12-
gaussianBlur.glsl
13-
luminanceApply.glsl
14-
luminanceCollection.glsl
15-
luminanceHistogram.glsl
16-
sharpening.glsl
17-
ssaoDownscaleDepth.glsl
18-
ssaoGenerate.glsl
19-
ssaoResolve.glsl
20-
tonemap.glsl
5+
#[]
6+
#scheme = shader
7+
#bloomApply.glsl
8+
#bloomGenerate.glsl
9+
#dofApply.glsl
10+
#dofCollect.glsl
11+
#fxaa.glsl
12+
#gaussianBlur.glsl
13+
#luminanceApply.glsl
14+
#luminanceCollection.glsl
15+
#luminanceHistogram.glsl
16+
#sharpening.glsl
17+
#ssaoDownscaleDepth.glsl
18+
#ssaoGenerate.glsl
19+
#ssaoResolve.glsl
20+
#tonemap.glsl

data/cage/shaders/engine/engine.assets

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
scheme = pack
33
engine.pack
44

5-
[]
6-
scheme = shader
7-
billboard.glsl
8-
blitPixels.glsl
9-
blitScaled.glsl
10-
decal.glsl
11-
icon.glsl
12-
skybox.glsl
13-
standard.glsl
14-
text.glsl
5+
#[]
6+
#scheme = shader
7+
#billboard.glsl
8+
#blitPixels.glsl
9+
#blitScaled.glsl
10+
#decal.glsl
11+
#icon.glsl
12+
#skybox.glsl
13+
#standard.glsl
14+
#text.glsl

data/cage/shaders/gui/gui.assets

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
scheme = pack
44
gui.pack
55

6-
[]
7-
scheme = shader
8-
font.glsl
9-
element.glsl
10-
image.glsl
11-
colorPicker.glsl?F # Flat
12-
colorPicker.glsl?H # Hue
13-
colorPicker.glsl?S # Saturation & Value
6+
#[]
7+
#scheme = shader
8+
#font.glsl
9+
#element.glsl
10+
#image.glsl
11+
#colorPicker.glsl?F # Flat
12+
#colorPicker.glsl?H # Hue
13+
#colorPicker.glsl?S # Saturation & Value

sources/asset-processor/shader.cpp

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
#include <map>
22
#include <set>
33
#include <string>
4+
#include <unordered_map>
45
#include <vector>
56

67
#include "processor.h"
78

9+
#include <cage-core/containerSerialization.h>
810
#include <cage-core/hashString.h>
9-
#include <cage-core/timer.h>
11+
#include <cage-engine/spirv.h>
1012

1113
namespace
1214
{
13-
std::map<String, std::string> codes;
14-
std::map<String, String> defines;
15-
std::set<String, StringComparatorFast> onces;
15+
struct Variant
16+
{
17+
Holder<Spirv> spirv;
18+
uint32 id = 0;
19+
};
20+
21+
std::unordered_map<uint32, std::string> codes;
22+
std::map<String, String, StringComparatorFast> defines;
23+
std::set<String> onces;
1624
std::set<detail::StringBase<20>> keywords;
25+
std::vector<Variant> variants;
1726

1827
const ConfigBool configShaderPrint("cage-asset-processor/shader/preview", false);
1928

@@ -226,28 +235,22 @@ namespace
226235
return evalExp(l);
227236
}
228237

229-
void output(const String &s)
230-
{
231-
if (defines["shader"].empty())
232-
{
233-
if (!s.empty())
234-
{
235-
CAGE_LOG_DEBUG(SeverityEnum::Warning, "assetProcessor", Stringizer() + "output to unspecified shader: " + s);
236-
}
237-
return;
238-
}
239-
codes[defines["shader"]] += std::string(s.c_str(), s.length()) + "\n";
240-
}
241-
242238
uint32 shaderType(const String &name)
243239
{
240+
if (name == "")
241+
return 0;
244242
if (name == "vertex")
245243
return 1;
246244
if (name == "fragment")
247245
return 2;
248246
if (name == "compute")
249247
return 3;
250-
return 0;
248+
CAGE_THROW_ERROR(Exception, "unknwon shader stage name");
249+
}
250+
251+
void output(const String &s)
252+
{
253+
codes[shaderType(defines["shader"])] += std::string(s.c_str(), s.length()) + "\n";
251254
}
252255

253256
bool stackIsOk(const std::vector<sint32> &stack)
@@ -445,6 +448,48 @@ namespace
445448
CAGE_THROW_ERROR(Exception, "unexpected end of file; expecting $end");
446449
}
447450
}
451+
452+
std::vector<bool> selectedKeys;
453+
void generateVariants(uint32 keyIndex)
454+
{
455+
if (keyIndex == keywords.size())
456+
{
457+
std::string preamble = "";
458+
uint32 id = 0;
459+
for (uint32 i = 0; i < keywords.size(); i++)
460+
{
461+
if (selectedKeys[i])
462+
{
463+
auto it = keywords.begin();
464+
std::advance(it, i);
465+
const detail::StringBase<20> k = *it;
466+
preamble += std::string("#define ") + k.c_str() + " 1\n";
467+
id += hashRawString(k.c_str());
468+
}
469+
}
470+
try
471+
{
472+
Holder<Spirv> sp = newSpirv();
473+
GlslConfig glsl;
474+
static const std::string prepend = std::string() + "#version 450 core\n" + "// " + processor->inputName.c_str() + "\n";
475+
glsl.vertex = prepend + preamble + "#define varying out\n" + codes[0] + codes[1];
476+
glsl.fragment = prepend + preamble + "#define varying in\n" + codes[0] + codes[2];
477+
sp->importGlsl(glsl);
478+
variants.push_back({ std::move(sp), id });
479+
}
480+
catch (...)
481+
{
482+
CAGE_LOG(SeverityEnum::Warning, "assetProcessor", "failed to build shader variant");
483+
}
484+
}
485+
else
486+
{
487+
selectedKeys[keyIndex] = true;
488+
generateVariants(keyIndex + 1);
489+
selectedKeys[keyIndex] = false;
490+
generateVariants(keyIndex + 1);
491+
}
492+
}
448493
}
449494

450495
void processShader()
@@ -456,39 +501,41 @@ void processShader()
456501

457502
parse(processor->inputFileName);
458503

504+
for (const auto &it : keywords)
505+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "keyword: " + it);
506+
507+
selectedKeys.resize(keywords.size(), false);
508+
generateVariants(0);
509+
if (variants.empty())
459510
{
460-
std::string prepend = R"foo(#version 450 core
461-
)foo";
462-
prepend += std::string() + "// " + processor->inputName.c_str() + "\n";
463-
for (auto &it : codes)
464-
it.second = prepend + it.second;
511+
CAGE_THROW_ERROR(Exception, "generated no shader variants");
465512
}
513+
else
514+
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "generated variants: " + variants.size());
466515

467516
{
468517
MultiShaderHeader header;
469-
header.variantsCount = codes.size();
470518
if (defines.count("customDataCount"))
471519
{
472520
const uint32 cdc = toUint32(defines["customDataCount"]);
473521
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "custom data count: " + cdc);
474522
header.customDataCount = cdc;
475523
}
524+
header.variantsCount = variants.size();
525+
476526
MemoryBuffer buff;
477527
Serializer ser(buff);
478528
ser << header;
479-
/*
480-
for (const auto &it : keywords)
481529
{
482-
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "keyword: " + it);
483-
ser << it;
530+
std::vector<detail::StringBase<20>> ks(keywords.begin(), keywords.end());
531+
ser << ks;
484532
}
485-
*/
486-
for (const auto &it : codes)
533+
for (const Variant &v : variants)
487534
{
488-
ser << (uint32)shaderType(it.first);
489-
ser << numeric_cast<uint32>(it.second.length());
490-
ser.write(it.second);
491-
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "stage: " + it.first + ", length: " + it.second.size());
535+
const auto b = v.spirv->exportBuffer();
536+
ser << v.id;
537+
ser << numeric_cast<uint32>(b.size());
538+
ser.write(b);
492539
}
493540

494541
CAGE_LOG(SeverityEnum::Info, "assetProcessor", Stringizer() + "buffer size (before compression): " + buff.size());
@@ -508,7 +555,7 @@ void processShader()
508555
{
509556
for (const auto &it : codes)
510557
{
511-
String name = pathJoin(configGetString("cage-asset-processor/shader/path", "asset-preview"), pathReplaceInvalidCharacters(processor->inputName) + "_" + it.first + ".glsl");
558+
const String name = pathJoin(configGetString("cage-asset-processor/shader/path", "asset-preview"), Stringizer() + pathReplaceInvalidCharacters(processor->inputName) + "_" + it.first + ".glsl");
512559
FileMode fm(false, true);
513560
fm.textual = true;
514561
Holder<File> f = newFile(name, fm);

sources/include/cage-engine/assetsSchemes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ namespace cage
77
{
88
class GraphicsDevice;
99

10-
CAGE_ENGINE_API AssetsScheme genAssetSchemeShader(GraphicsDevice *device, uint32 threadIndex);
10+
CAGE_ENGINE_API AssetsScheme genAssetSchemeShader(GraphicsDevice *device);
1111
constexpr uint32 AssetSchemeIndexShader = 10;
1212

13-
CAGE_ENGINE_API AssetsScheme genAssetSchemeTexture(GraphicsDevice *device, uint32 threadIndex);
13+
CAGE_ENGINE_API AssetsScheme genAssetSchemeTexture(GraphicsDevice *device);
1414
constexpr uint32 AssetSchemeIndexTexture = 11;
1515

16-
CAGE_ENGINE_API AssetsScheme genAssetSchemeModel(GraphicsDevice *device, uint32 threadIndex);
16+
CAGE_ENGINE_API AssetsScheme genAssetSchemeModel(GraphicsDevice *device);
1717
constexpr uint32 AssetSchemeIndexModel = 12;
1818

1919
CAGE_ENGINE_API AssetsScheme genAssetSchemeRenderObject();

0 commit comments

Comments
 (0)