Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage inputs not marked as used using isParameterLocationUsed #5756

Closed
PierreEVEN opened this issue Dec 4, 2024 · 2 comments · Fixed by #5906
Closed

Stage inputs not marked as used using isParameterLocationUsed #5756

PierreEVEN opened this issue Dec 4, 2024 · 2 comments · Fixed by #5906
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang

Comments

@PierreEVEN
Copy link

Hi,
I want to detect if a stage input parameter (varying in) is used by a given stage.
I tried to use slang::IMetadata::isParameterLocationUsed but it always returns false.

I haven't found any other way of obtaining this information, so this case is maybe not handled yet.

Here is a full example code :

Slang shader

#include <filesystem>
#include <iostream>
#include <optional>
#include <string>

#include "slang-com-ptr.h"
#include "slang.h"

// recursively iterate into varying parameter struct members to know which one are used
static void try_register_variable(slang::VariableLayoutReflection* variable, slang::IMetadata* metadata)
{
	if (variable->getTypeLayout()->getKind() == slang::TypeReflection::Kind::Struct)
	{
		for (size_t fi = 0; fi < variable->getTypeLayout()->getFieldCount(); ++fi)
			try_register_variable(variable->getTypeLayout()->getFieldByIndex(fi), metadata);
	}
	else
	{
		if (variable->getTypeLayout()->getBindingRangeCount() == 1)
		{
			bool b_used = true;
			metadata->isParameterLocationUsed((SlangParameterCategory)variable->getCategory(),
			                                  variable->getBindingSpace(), variable->getBindingIndex(), b_used);
                       // Will always be false even if "Pos" is used in our example
			std::cout << "name=" << variable->getName() << " : used=" << b_used << "\n";
		}
	}
}

std::optional<std::string> test_which_varying_inputs_used(const std::filesystem::path& module_path)
{
	Slang::ComPtr<slang::IBlob> diagnostics;

	// CREATE GLOBAL SESSION
	Slang::ComPtr<slang::IGlobalSession> global_session;
	if (SLANG_FAILED(slang::createGlobalSession(global_session.writeRef())))
		return "Failed to create global slang compiler session";

	// CREATE SESSION
	slang::SessionDesc sessionDesc;
	slang::TargetDesc targetDesc;
	targetDesc.format = SLANG_SPIRV;
	targetDesc.profile = global_session->findProfile("spirv_1_5");
	if (targetDesc.profile == SLANG_PROFILE_UNKNOWN)
		return "Failed to find slang profile 'spirv_1_5'";
	sessionDesc.targets = &targetDesc;
	sessionDesc.targetCount = 1;
	Slang::ComPtr<slang::ISession> session;
	if (SLANG_FAILED(global_session->createSession(sessionDesc, session.writeRef())))
		return "Failed to create slang compiler session";

	// LOAD EXAMPLE MODULE FROM PATH 'module_path'
	slang::IModule* module = session->loadModule(module_path.string().c_str(), diagnostics.writeRef());
	if (diagnostics)
		return static_cast<const char*>(diagnostics->getBufferPointer());

	// ITERATE OVER ENTRY POINTS
	for (SlangInt32 ep_i = 0; ep_i < module->getDefinedEntryPointCount(); ++ep_i)
	{
		Slang::ComPtr<slang::IEntryPoint> entry_point;
		if (SLANG_FAILED(module->getDefinedEntryPoint(ep_i, entry_point.writeRef())))
			return "Failed to get entry point";

		// CREATE COMPOSITE COMPONENT TYPE
		std::vector<slang::IComponentType*> components{entry_point};
		Slang::ComPtr<slang::IComponentType> program;
		if (SLANG_FAILED(
			session->createCompositeComponentType(components.data(), components.size(), program.writeRef())))
			return "Failed to create stage program";

		// LINK PROGRAM
		Slang::ComPtr<slang::IComponentType> linked_program;
		program->link(linked_program.writeRef(), diagnostics.writeRef());
		if (diagnostics)
			return static_cast<const char*>(diagnostics->getBufferPointer());

		// GET REFLECTION DATA
		slang::IMetadata* metadata;
		linked_program->getEntryPointMetadata(0, 0, &metadata, diagnostics.writeRef());
		if (diagnostics)
			return static_cast<const char*>(diagnostics->getBufferPointer());

		// ITERATE OVER PARAMETERS
		for (uint32_t pi = 0; pi < entry_point->getLayout()->getEntryPointByIndex(0)->getParameterCount(); ++pi)
			try_register_variable(entry_point->getLayout()->getEntryPointByIndex(0)->getParameterByIndex(pi), metadata);
	}

	return {};
}

int main(int argc, char** argv)
{
	if (argc != 2)
	{
		std::cerr << "expected slang shader path \n";
		return -1;
	}

	//std::filesystem::path file_path = std::filesystem::path(__FILE__).parent_path() / "test_shader.slang";
	std::filesystem::path file_path = argv[1];

	if (const auto error = test_which_varying_inputs_used(file_path))
	{
		std::cerr << "ERROR : " << *error << "\n";
		return -1;
	}
	std::cout << "Done\n";
}
@bmillsNV
Copy link
Collaborator

bmillsNV commented Dec 5, 2024

@PierreEVEN is this issue blocking you? Or are you OK if we target this for Q1 2025?

@bmillsNV bmillsNV added the Needs reporter feedback Bugs awaiting more information from the reporter label Dec 5, 2024
@PierreEVEN
Copy link
Author

PierreEVEN commented Dec 6, 2024

This is a personal project, so no worries about the deadlines 👌
(in the meantime I'll use spirv-reflect)

@csyonghe csyonghe self-assigned this Dec 18, 2024
@csyonghe csyonghe added goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang and removed Needs reporter feedback Bugs awaiting more information from the reporter labels Dec 18, 2024
@csyonghe csyonghe added this to the Q4 2024 (Fall) milestone Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants