Skip to content

Commit 2d363dc

Browse files
Samples: use new IContext::createShaderModule() API for Slang shaders
1 parent c84b824 commit 2d363dc

File tree

5 files changed

+4
-147
lines changed

5 files changed

+4
-147
lines changed

samples/001_HelloTriangle.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ VULKAN_APP_MAIN {
9393

9494
{
9595
#if defined(LVK_DEMO_WITH_SLANG)
96-
lvk::Holder<lvk::ShaderModuleHandle> vert_ = slangCreateShaderModule(ctx, codeSlang, lvk::Stage_Vert, "Shader Module: main (vert)");
97-
lvk::Holder<lvk::ShaderModuleHandle> frag_ = slangCreateShaderModule(ctx, codeSlang, lvk::Stage_Frag, "Shader Module: main (frag)");
96+
lvk::Holder<lvk::ShaderModuleHandle> vert_ = ctx->createShaderModule({codeSlang, lvk::Stage_Vert, "Shader Module: main (vert)"});
97+
lvk::Holder<lvk::ShaderModuleHandle> frag_ = ctx->createShaderModule({codeSlang, lvk::Stage_Frag, "Shader Module: main (frag)"});
9898
#else
9999
lvk::Holder<lvk::ShaderModuleHandle> vert_ = ctx->createShaderModule({codeVS, lvk::Stage_Vert, "Shader Module: main (vert)"});
100100
lvk::Holder<lvk::ShaderModuleHandle> frag_ = ctx->createShaderModule({codeFS, lvk::Stage_Frag, "Shader Module: main (frag)"});

samples/009_TriplanarMapping.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ VULKAN_APP_MAIN {
328328
}
329329

330330
#if defined(LVK_DEMO_WITH_SLANG)
331-
lvk::Holder<lvk::ShaderModuleHandle> vert_ = slangCreateShaderModule(ctx, codeSlang, lvk::Stage_Vert, "Shader Module: main (vert)");
332-
lvk::Holder<lvk::ShaderModuleHandle> frag_ = slangCreateShaderModule(ctx, codeSlang, lvk::Stage_Frag, "Shader Module: main (frag)");
331+
lvk::Holder<lvk::ShaderModuleHandle> vert_ = ctx->createShaderModule({codeSlang, lvk::Stage_Vert, "Shader Module: main (vert)"});
332+
lvk::Holder<lvk::ShaderModuleHandle> frag_ = ctx->createShaderModule({codeSlang, lvk::Stage_Frag, "Shader Module: main (frag)"});
333333
#else
334334
lvk::Holder<lvk::ShaderModuleHandle> vert_ = ctx->createShaderModule({codeVS, lvk::Stage_Vert, "Shader Module: main (vert)"});
335335
lvk::Holder<lvk::ShaderModuleHandle> frag_ = ctx->createShaderModule({codeFS, lvk::Stage_Frag, "Shader Module: main (frag)"});

samples/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,3 @@ ADD_DEMO_SOURCES("Tiny_MeshLarge"
177177
ADD_DEMO_SOURCES("Tiny_MeshLarge"
178178
"${LVK_ROOT_DIR}/third-party/deps/src/imgui/imgui_demo.cpp")
179179
ADD_DEMO_LINK_LIBRARIES("Tiny_MeshLarge" ktx)
180-
181-
# Slang
182-
if(LVK_WITH_SLANG)
183-
target_compile_definitions(LVKVulkanApp PUBLIC "LVK_WITH_SLANG=1")
184-
target_link_libraries(LVKVulkanApp PUBLIC slang-rt core)
185-
target_include_directories(LVKVulkanApp PUBLIC "${LVK_ROOT_DIR}/third-party/deps/src/slang/include")
186-
endif()

samples/VulkanApp.cpp

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515
#include <stb/stb_image.h>
1616
#include <stb/stb_image_write.h>
1717

18-
#if defined(LVK_WITH_SLANG) && LVK_WITH_SLANG
19-
#include <slang.h>
20-
#include <slang-com-helper.h>
21-
#include <slang-com-ptr.h>
22-
#include <core/slang-basic.h>
23-
#endif // defined(LVK_WITH_SLANG) && LVK_WITH_SLANG
24-
2518
#if defined(ANDROID)
2619
#include <android/asset_manager_jni.h>
2720
#include <android/native_window_jni.h>
@@ -412,124 +405,3 @@ void VulkanApp::drawFPS() {
412405
}
413406
ImGui::End();
414407
}
415-
416-
#if defined(LVK_WITH_SLANG) && LVK_WITH_SLANG
417-
418-
std::vector<uint8_t> compileSlangToSPIRV(const char* code, lvk::ShaderStage stage) {
419-
using namespace Slang;
420-
421-
ComPtr<slang::IGlobalSession> slangGlobalSession;
422-
if (SLANG_FAILED(slang::createGlobalSession(slangGlobalSession.writeRef()))) {
423-
return {};
424-
}
425-
426-
slang::CompilerOptionEntry compilerOptions[] = {
427-
{.name = slang::CompilerOptionName::Capability,
428-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "SPV_GOOGLE_user_type"}},
429-
{.name = slang::CompilerOptionName::Capability,
430-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvDerivativeControl"}},
431-
{.name = slang::CompilerOptionName::Capability,
432-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvImageQuery"}},
433-
{.name = slang::CompilerOptionName::Capability,
434-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvImageGatherExtended"}},
435-
{.name = slang::CompilerOptionName::Capability,
436-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvSparseResidency"}},
437-
{.name = slang::CompilerOptionName::Capability,
438-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvMinLod"}},
439-
{.name = slang::CompilerOptionName::Capability,
440-
.value = {.kind = slang::CompilerOptionValueKind::String, .stringValue0 = "spvFragmentFullyCoveredEXT"}},
441-
};
442-
443-
const slang::TargetDesc targetDesc = {
444-
.format = SLANG_SPIRV,
445-
.profile = slangGlobalSession->findProfile("spirv_1_6"),
446-
.flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY,
447-
.forceGLSLScalarBufferLayout = true,
448-
.compilerOptionEntries = &compilerOptions[0],
449-
.compilerOptionEntryCount = LVK_ARRAY_NUM_ELEMENTS(compilerOptions),
450-
};
451-
452-
const slang::SessionDesc sessionDesc = {
453-
.targets = &targetDesc,
454-
.targetCount = 1,
455-
};
456-
457-
ComPtr<slang::ISession> session;
458-
if (SLANG_FAILED(slangGlobalSession->createSession(sessionDesc, session.writeRef()))) {
459-
return {};
460-
}
461-
462-
slang::IModule* slangModule = nullptr;
463-
{
464-
ComPtr<slang::IBlob> diagnosticBlob;
465-
slangModule = session->loadModuleFromSourceString("", "", code, diagnosticBlob.writeRef());
466-
if (diagnosticBlob) {
467-
LLOGW("%s", (const char*)diagnosticBlob->getBufferPointer());
468-
}
469-
if (!slangModule) {
470-
return {};
471-
}
472-
}
473-
474-
ComPtr<slang::IEntryPoint> entryPointVert;
475-
ComPtr<slang::IEntryPoint> entryPointFrag;
476-
if (SLANG_FAILED(slangModule->findEntryPointByName("vertexMain", entryPointVert.writeRef()))) {
477-
LVK_ASSERT_MSG(entryPointVert, "vertexMain() not found");
478-
return {};
479-
}
480-
if (SLANG_FAILED(slangModule->findEntryPointByName("fragmentMain", entryPointFrag.writeRef()))) {
481-
LVK_ASSERT_MSG(entryPointFrag, "fragmentMain() not found");
482-
return {};
483-
}
484-
485-
Slang::List<slang::IComponentType*> componentTypes;
486-
componentTypes.add(slangModule);
487-
int entryPointCount = 0;
488-
int vertexEntryPointIndex = entryPointCount++;
489-
componentTypes.add(entryPointVert);
490-
int fragmentEntryPointIndex = entryPointCount++;
491-
componentTypes.add(entryPointFrag);
492-
493-
ComPtr<slang::IComponentType> composedProgram;
494-
{
495-
ComPtr<slang::IBlob> diagnosticBlob;
496-
SlangResult result = session->createCompositeComponentType(
497-
componentTypes.getBuffer(), componentTypes.getCount(), composedProgram.writeRef(), diagnosticBlob.writeRef());
498-
if (diagnosticBlob) {
499-
LLOGW("%s\n", (const char*)diagnosticBlob->getBufferPointer());
500-
}
501-
if (SLANG_FAILED(result)) {
502-
LVK_ASSERT_MSG(false, "Slang failed");
503-
return {};
504-
}
505-
}
506-
507-
ComPtr<slang::IBlob> spirvCode;
508-
{
509-
ComPtr<slang::IBlob> diagnosticBlob;
510-
const int entryPoint = stage == lvk::Stage_Vert ? vertexEntryPointIndex : fragmentEntryPointIndex;
511-
SlangResult result = composedProgram->getEntryPointCode(entryPoint, 0, spirvCode.writeRef(), diagnosticBlob.writeRef());
512-
if (diagnosticBlob) {
513-
LLOGW("%s\n", (const char*)diagnosticBlob->getBufferPointer());
514-
}
515-
if (SLANG_FAILED(result)) {
516-
LVK_ASSERT_MSG(false, "Slang failed");
517-
return {};
518-
}
519-
}
520-
521-
const uint8_t* ptr = reinterpret_cast<const uint8_t*>(spirvCode->getBufferPointer());
522-
523-
return std::vector<uint8_t>(ptr, ptr + spirvCode->getBufferSize());
524-
}
525-
526-
lvk::Holder<lvk::ShaderModuleHandle> slangCreateShaderModule(lvk::IContext* ctx,
527-
const char* code,
528-
lvk::ShaderStage stage,
529-
const char* debugName) {
530-
const std::vector<uint8_t> spirv = compileSlangToSPIRV(code, stage);
531-
532-
return ctx ? ctx->createShaderModule({spirv.data(), spirv.size(), stage, debugName}) : lvk::Holder<lvk::ShaderModuleHandle>();
533-
}
534-
535-
#endif // defined(LVK_WITH_SLANG) && LVK_WITH_SLANG

samples/VulkanApp.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,3 @@ class VulkanApp {
129129

130130
uint64_t frameCount_ = 0;
131131
};
132-
133-
#if defined(LVK_WITH_SLANG) && LVK_WITH_SLANG
134-
std::vector<uint8_t> compileSlangToSPIRV(const char* code, lvk::ShaderStage stage);
135-
lvk::Holder<lvk::ShaderModuleHandle> slangCreateShaderModule(lvk::IContext* ctx,
136-
const char* code,
137-
lvk::ShaderStage stage,
138-
const char* debugName);
139-
#endif // defined(LVK_WITH_SLANG) && LVK_WITH_SLANG

0 commit comments

Comments
 (0)