Code Generation Utilities¶
Utilities for normalizing and matching shader stage qualifiers.
- crosstl.translator.codegen.stage_utils.compute_local_size(execution_config=None)[source]¶
Return a three-component workgroup size from execution metadata.
- crosstl.translator.codegen.stage_utils.compute_local_size_value(value)[source]¶
Return a string representation for a local-size dimension value.
- crosstl.translator.codegen.stage_utils.normalize_stage_name(stage)[source]¶
Normalize a shader stage enum or string into a lowercase name.
- crosstl.translator.codegen.stage_utils.should_emit_qualified_function(target_stage, qualifier)[source]¶
Return whether a qualified function belongs in the target output.
- crosstl.translator.codegen.stage_utils.stage_matches(target_stage, stage)[source]¶
Return whether a stage should be emitted for the target stage.
Helpers for recognizing resource query expressions in CrossGL AST nodes.
- class crosstl.translator.codegen.resource_query.ResourceQueryMixin[source]¶
Bases:
objectHelpers for resource query metadata propagation and code generation.
- build_dimension_query_helper(helper_name, spec)[source]¶
Build helper code for a texture/image dimension query.
- build_texture_query_levels_helper(helper_name, spec)[source]¶
Build helper code for texture mip-level queries.
- collect_resource_query_names(node)[source]¶
Collect resource names used directly in resource query calls.
- collect_resource_query_requirements(node)[source]¶
Collect global and function-parameter resources needing metadata.
- dimension_query_spec(type_name)[source]¶
Return dimension/mip/sample metadata for a queryable resource type.
- generate_dimension_query(func_name, raw_args, args)[source]¶
Generate a helper call for a dimension query, if supported.
- generate_sample_count_query(func_name, raw_args, args)[source]¶
Generate a helper call for a sample-count query, if supported.
- generate_texture_query_levels(raw_args)[source]¶
Generate a helper call for
textureQueryLevels, if supported.
- needs_query_metadata(name, type_name)[source]¶
Return whether a resource declaration needs query metadata.
- query_dimension_expression(dimension, mip_arg)[source]¶
Return a metadata field expression for one queried dimension.
- query_metadata_call_arguments(func_name, raw_args, args)[source]¶
Expand call arguments with resource metadata arguments.
- query_metadata_declaration(name, type_name)[source]¶
Return a metadata variable declaration when required.
- query_metadata_expression(resource_expr)[source]¶
Return the metadata expression paired with a resource expression.
- query_metadata_name(resource_name)[source]¶
Return the generated metadata variable name for a resource.
- query_metadata_parameter(name, type_name)[source]¶
Return an extra metadata parameter declaration when required.
- query_type_name(type_name)[source]¶
Return a string type name suitable for resource query decisions.
Shared diagnostics mixin for reporting resource translation issues.
- class crosstl.translator.codegen.resource_diagnostics.ResourceDiagnosticMixin[source]¶
Bases:
objectFallback helpers for unsupported resource operations in backends.
- image_atomic_zero_value(resource_type)[source]¶
Return a neutral fallback value for image atomic operations.
- image_value_type(image_type)[source]¶
Return the scalar/vector value type loaded from an image resource.
- is_multisample_resource_type(type_name)[source]¶
Return whether a resource type denotes a multisample resource.
- is_shadow_resource_type(type_name)[source]¶
Return whether a resource type denotes a shadow sampler.
- multisample_zero_value(func_name, resource_type)[source]¶
Return a neutral fallback value for unsupported multisample calls.
- shadow_zero_value(func_name)[source]¶
Return a neutral fallback value for unsupported shadow operations.
- surface_x_offset(coord, value_type)[source]¶
Format a byte offset expression for surface x coordinates.
- unsupported_image_atomic_resource_call(func_name, resource_type, _args)[source]¶
Return diagnostic code for unsupported image atomic operations.
- unsupported_multisample_resource_call(func_name, resource_type, _args)[source]¶
Return diagnostic code for unsupported multisample operations.
- unsupported_resource_query_call(func_name, resource_type, _args)[source]¶
Return diagnostic code for unsupported resource query operations.
- unsupported_sampled_resource_call(func_name, resource_type, _args)[source]¶
Return diagnostic code for unsupported sampled-resource operations.
Helpers for collecting resource-array size hints from CrossGL AST nodes.
- crosstl.translator.codegen.resource_arrays.collect_resource_array_size_hints(*, global_arrays, function_arrays, fixed_global_array_sizes=None, fixed_function_array_sizes=None, functions, walk_nodes, expression_name, literal_int_value, visible_literal_int_constants, function_call_name, initial_size, format_size)[source]¶
Infer resource-array sizes from literal accesses and call propagation.
- crosstl.translator.codegen.resource_arrays.format_array_declarator(mapped_base, name, array_suffix, dynamic_array_as_pointer=True)[source]¶
Format a typed C-style array declarator for generated code.
- crosstl.translator.codegen.resource_arrays.split_array_suffix(array_suffix)[source]¶
Split a C-style array suffix into dimension strings.
Utilities for lowering vector arithmetic expressions during code generation.
- class crosstl.translator.codegen.vector_arithmetic.VectorArithmeticMixin[source]¶
Bases:
objectHelpers for inferring and lowering vector arithmetic expressions.
- collect_function_return_types(ast_node)[source]¶
Collect function return types from global and stage-local functions.
- lower_vector_binary_operation(left_node, left_expr, right_node, right_expr, operator)[source]¶
Lower vector binary arithmetic into a helper call when required.
- require_vector_binary_helper(vector_info, operator, operand_shape)[source]¶
Register and return a helper function for vector binary arithmetic.
- resource_call_result_type(func_name, raw_args)[source]¶
Infer the result type of resource-related intrinsic calls.
- vector_scalar_parameter_type(vector_info)[source]¶
Return the scalar parameter type used by generated vector helpers.
AST utilities for CrossGL code generators.
- class crosstl.translator.codegen.ast_utils.ASTUtils[source]¶
Bases:
objectUtilities for AST processing and type conversion.
- static expression_to_string(expr)[source]¶
Render a simple expression node to a string for declarations.
- Return type:
str
- static get_function_info(function, backend='generic')[source]¶
Return normalized metadata for a function declaration.
- static get_legacy_compatible_semantic(node)[source]¶
Return semantic metadata from either legacy or current AST nodes.
- Return type:
Optional[str]
- static get_legacy_compatible_type(node, backend='generic')[source]¶
Return a type string for either legacy or current AST nodes.
- Return type:
str
- static get_member_info(member, backend='generic')[source]¶
Return normalized metadata for a struct member.
- static get_parameter_info(parameter, backend='generic')[source]¶
Return normalized metadata for a function parameter.
- static get_semantic_from_attributes(attributes)[source]¶
Return the first recognized semantic attribute name.
- Return type:
Optional[str]
- static get_type_string(type_node, backend='generic')[source]¶
Convert a TypeNode to a backend-specific string.
- Return type:
str
- static get_variable_info(variable, backend='generic')[source]¶
Return normalized metadata for a variable declaration.
- static is_legacy_ast_node(node)[source]¶
Return whether a node uses the older
vtypestring shape.- Return type:
bool
Utility functions for array handling in code generators.
This module provides common functions for handling array types, array access, and type detection across different code generators.
- crosstl.translator.codegen.array_utils.collect_literal_int_constants(constants)[source]¶
Collect resolvable integer compile-time constants by name.
- Return type:
Dict[str,int]
- crosstl.translator.codegen.array_utils.collect_struct_member_types(structs, type_name_string)[source]¶
Collect raw struct member types for expression type inference.
- Return type:
Dict[str,Dict[str,str]]
- crosstl.translator.codegen.array_utils.detect_array_element_type(array_type, type_mapping=None)[source]¶
Detect the element type of an array based on its type string.
- Parameters:
array_type (
str) – The array type stringtype_mapping (
Dict[str,Any]) – Optional mapping of types to use for lookups
- Return type:
str- Returns:
The detected element type string
- crosstl.translator.codegen.array_utils.evaluate_literal_int_expression(expr, constants=None)[source]¶
Evaluate a narrow integer-only AST expression.
This is used for declaration sizing hints where accepting dynamic values would be unsafe. It intentionally supports only literals, unary signs, and basic arithmetic over integer literals. Named constants are resolved only from the explicit constants map supplied by the caller.
- Return type:
Optional[int]
- crosstl.translator.codegen.array_utils.format_array_type(base_type, size, lang_style='glsl')[source]¶
Format an array type according to the target language style.
- Parameters:
base_type (
str) – The base type of the array (e.g., “float”, “vec3”)size (
Optional[int]) – The size of the array, or None for dynamic arrayslang_style (
str) – The language style (‘glsl’, ‘hlsl’, ‘metal’, ‘spirv’)
- Return type:
str- Returns:
The formatted array type string for the target language
- crosstl.translator.codegen.array_utils.format_c_style_array_declaration(type_name, variable_name)[source]¶
Format a declaration when the type string includes C-style array suffixes.
Converts “float[4]”, “vec3[]”, or “float[3][4]” into “float name[4]”, “vec3 name[]”, or “float name[3][4]”. Non-array type strings are returned as “type name”.
- Return type:
str
- crosstl.translator.codegen.array_utils.get_array_size_from_node(node)[source]¶
Extract array size from an AST ArrayNode.
- Parameters:
node – The ArrayNode to extract size from
- Return type:
Optional[int]- Returns:
The array size as an integer, or None for dynamic arrays
- crosstl.translator.codegen.array_utils.parse_array_type(type_name)[source]¶
Parse an array type string into base type and size.
- Parameters:
type_name (
str) – The array type string (e.g., “float[4]”, “vec3[]”)- Return type:
Tuple[str,Optional[int]]- Returns:
Tuple of (base_type, size) where size is None for dynamic arrays
- crosstl.translator.codegen.array_utils.split_array_type_suffix(type_name)[source]¶
Split an array type string while preserving the full array suffix.
Unlike parse_array_type(), this keeps non-literal sizes such as “float[(2 + 1) * 2]” intact instead of treating them as unsized arrays.
- Return type:
Tuple[str,str]