blender/source/blender/gpu/shader_tool/bsl/symbol_variable.hh
Clément Foucault 3950d078b6 GPU: Shader Tool: Support vector constexpr
This is actually used in a few placed in the
codebase and is quite useful.

Current support only covers:
- scalar constructors.
- vector cast constructors.
- Relational builtin function (e.g. `equal`)

Increasing the amount of permutation inside
the `ConstexprValue` type using `std::variant` grew quadratically
and increases compilation time so much that it was scrapped and
replaced by manual switches.

Pull Request: https://projects.blender.org/blender/blender/pulls/163966
2026-09-16 17:30:59 +02:00

100 lines
2.4 KiB
C++

/* SPDX-FileCopyrightText: 2026 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup shader_tool
*/
#pragma once
#include "constexpr.hh"
#include "resource.hh"
#include "source_location.hh"
#include <cstdint>
namespace bsl {
struct SymbolScope;
struct SymbolClass;
struct SymbolTable;
/**
* Base struct for all named code entities.
*/
struct Symbol {
/* Mangled identifier (eg. for 'i' in 'struct A { static int i; }' it would be 'A_i'). */
string identifier;
/* Unresolved identifier */
string original;
SourceLocation loc;
SymbolScope *parent = nullptr;
};
using ConstexprError = ConstexprValue;
std::string constexpr_to_string(const ConstexprValue &v);
/**
* Represent variable declarations, parameters, or enum values.
*/
struct SymbolVariable : Symbol {
SymbolClass *type = nullptr;
/* Valid if the variable is a local reference.
* The variable will be substituted to this statement. */
ast::Expr reference_value = {};
/* Value if constexpr. */
ConstexprValue value = ConstexprError();
ResourceType res_type = ResourceType::NONE;
int8_t array_dimensions = 0;
/* Number of array elements in the declaration.
* 0 if not array.
* -1 if not known at compile time. */
int array_elements = 0;
/* In bytes. From scope start, taking alignment into account. */
int offset = 0;
/* In bits. From start and length in a bitfield. 0 otherwise. */
int bit_offset = 0;
int bit_length = 0;
bool is_bitfield = false;
bool is_static = false;
bool is_error = false;
bool is_constexpr = false;
bool is_const = false;
bool is_compilation_const = false;
SymbolVariable(SymbolScope *parent,
SymbolClass *type,
ast::Declarator decl,
const SymbolTable &table);
SymbolVariable(SymbolScope *parent, SymbolClass *type, ast::EnumValue decl)
: Symbol(string(decl.identifier().str()),
string(decl.identifier().str()),
decl.front(),
parent),
type(type),
is_static(true)
{
}
SymbolVariable(SymbolScope *parent, SymbolClass *type, Token id, const string &str)
: Symbol(str, str, id, parent), type(type)
{
}
void set_offset(bool is_union, int &offset);
/* Can be null if in global space. */
SymbolClass *parent_class();
string value_str() const;
};
} // namespace bsl