Commit graph

4925 commits

Author SHA1 Message Date
Bill Currie
e0e3998efc [qfcc] Semi-implement some glsl layout attributes
They're still just stubs, but the error noise is down.
2025-01-13 17:52:15 +09:00
Bill Currie
b28cd673ae [qfcc] Prefer fixed types over generic
This allows the various vector,vector and vector,scalar to disambiguate.
2025-01-13 17:47:52 +09:00
Bill Currie
027ad12c72 [qfcc] Support add/sub for matrix/vector and scalar operands
GLSL needs it, and it's not entirely unreasonable (though I'll probably
add some flags to control it since it doesn't always make sense).
2025-01-13 12:52:12 +09:00
Bill Currie
320f9e2fe7 [qfcc] Support type.attribute on any type
The goal is to make it easy to get size/coord/base types from image
types without creating a zillion type functions.

It was necessary to make it possible for any type to have an attribute
function (returns an expression so it can be more useful: types are
returned via type expressions). Algebra types were the first victim
(which was nice for testing).
2025-01-13 00:10:27 +09:00
Bill Currie
e9eae1c75f [qfcc] Implement incop for SPIR-V
I think I got it right, but it's hard to tell because my shaders use ++
only for the iterator of loops so the result is thrown away.
2025-01-12 23:29:57 +09:00
Bill Currie
270c57e46e [qfcc] Support assignment into array elements in SPIR-V
Just a missed detection of needing to use access chains.
2025-01-12 23:29:57 +09:00
Bill Currie
42fc1323e1 [qfcc] Clean up glsl for loops
The changes for deferred semantics in Ruamoko broke for loops for GLSL,
but matching GLSL's structure to Ruamoko's cleaned things up nicely.
2025-01-12 23:29:57 +09:00
Bill Currie
7baf93c8df [qfcc] Make short-circuit boolean logic language dependent
And disable it for GLSL. I might need to make it target-dependent
instead as I don't know if I'll be able to implement it in SPIR-V, and
when I get to the C back-end, it will be superfluous.
2025-01-12 23:29:56 +09:00
Bill Currie
03bbfde5f1 [qfcc] Get some more texture functions implemented
GLSL's texture functions are a bit of a nightmare. The return types are
wrong, but I need to decide how I want to pull the sample type from a
texture or sampler.
2025-01-10 01:00:00 +09:00
Bill Currie
09bd49814e [qfcc] Correct spirv right shift types
Spir-v is pretty slack with the exact types of the operants to the
right-shift instructions so no need to care about the type for shift so
long as it has the correct number of components.
2025-01-10 00:58:26 +09:00
Bill Currie
6e004e9baa [qfcc] Implement generic type computation
I had already implemented the code generation side (though using type
ids instead of encodings is a nice change), but I hadn't implemented the
actual evaluation or even called it. Now return types can be computed
from generic parameters (eg, ivecN from vecN).
2025-01-10 00:56:02 +09:00
Bill Currie
76fcc2f1a9 [qfcc] Support nil expressions in spir-v 2025-01-09 17:49:20 +09:00
Bill Currie
1d0135be52 [qfcc] Support nil in matrix construction
While not used by glsl, ruamoko will and qfcc does internally.
2025-01-09 17:48:39 +09:00
Bill Currie
d1364e460c [qfcc] Update some TODO entries
Mostly marking stuff as done, and adding a note to the optional params
item.
2025-01-09 17:32:07 +09:00
Bill Currie
a93c109988 [qfcc] Improve spir-v entry point handling
It's still not great (mostly on the language side, I think), but
different glsl shader types get the correct model and fragment shaders
even get the correct mode.
2025-01-09 17:29:27 +09:00
Bill Currie
4998968169 [qfcc] Emit spirv image type encodings
Access qualifiers aren't supported yet, and some of the image handling
is a bit messy (currently tied to glsl when it should be general), but
the basics are there. However, now I've got to sort out the execution
model (need Fragment or GLCompute for ImplicitLod).
2025-01-09 01:29:38 +09:00
Bill Currie
99b1859aac [qfcc] Parse the spirv grammar json files
I really don't like the way they're included (I'm really looking forward
to #embed, but gotta wait for gcc 15), and I'm a tad grumpy that the
documentation for them
(https://registry.khronos.org/SPIR-V/specs/unified1/MachineReadableGrammar.html)
is wrong (missing fields), but I think I like the result.

The grammars (core and glsl.std.450) are parsed into structs that should
be fairly easy to interpret: the instructions, kinds, and enumerant
values are sorted by name for search with bsearch. Having the data
parsed in means source code can refer to the items by name rather than
magic numbers, which will be very nice for intrinsics and image types
(and probably a few other things).
2025-01-07 03:26:12 +09:00
Bill Currie
400486931f [qfcc] Allow languages to hook number parsing
The idea is to allow certain contexts to interpret something like `2D`
as an identifier instead of 2.0 (double), or (not sure I'll go there,
5e12 as a bivector instead of a double or float.
2025-01-04 12:51:30 +09:00
Bill Currie
9aea4d4b61 [qfcc] Update glsl function declaration handling
Finally back to where I was before discovering that arrays with computed
counts didn't work.
2025-01-03 21:58:33 +09:00
Bill Currie
ea3198e8fe [qfcc] Update generic handling for the new type scheme
And clean up some redundant (premature!) spec processing.
2025-01-03 21:27:28 +09:00
Bill Currie
ae4c107384 [qfcc] Add type_process for type functions
I'm not quite sure it's what I need, but it does help with resolving
types with complex expressions (eg, `@vector(bool,@width(vec))`).
2025-01-03 17:55:08 +09:00
Bill Currie
bec8b290d2 [qfcc] Resurrect is_function
In the end, it does simplify things a lot, though it helped get function
pointers working at least a bit (they're not quite the same as C yet,
but I think that's mostly that functions can be struct fields).
2025-01-03 17:52:19 +09:00
Bill Currie
99632ddb03 [qfcc] Rework function/pointer/array declarations
They should now work in generic contexts, but the pressing need to work
on arrays was due to constant expressions for element counts breaking.
As a side effect, function pointers are now a thing (and seem to work
like they do in C)
2025-01-03 13:22:29 +09:00
Bill Currie
fd836e74e1 [qfcc] Check implicit is the same for dagged values
Not doing so results in subtle bugs such as implicit constants becoming
explicit behind the programmer's back.
2025-01-03 13:22:29 +09:00
Bill Currie
6af7ff2867 [qfcc] Ensure processed expressions are dagged
GA expressions need to be optimized so things that should cancel out do,
and this requires everything to be dagged. Doing so in expr_process gets
most of the expressions, and then a few stragglers in proc_field. There
might still be some more, but my test scene compiles again.
2025-01-03 02:34:15 +09:00
Bill Currie
34fe4efc9d Revert "[qfcc] Don't fold constants for algebra"
This reverts commit e87e01e4f2.

In the end, this wasn't necessary, and it causes problems in itself.
2025-01-03 01:03:28 +09:00
Bill Currie
6b38fdca35 [qfcc] Remove unwanted spec symbol assignments
I suspect they were once for dealing with class and typedef names in
selectors, but they're confusing things now that I'm trying to rework
type declarations, and I doubt they were ever actually needed.
2025-01-02 14:24:55 +09:00
Bill Currie
0ea47193f2 [qfcc] Support sizeof expressions in dot output 2025-01-02 11:18:38 +09:00
Bill Currie
625c53180f [qfcc] Propagate rua_ctx_t to more functions
It will eventually get to most places, but this set is necessary for
using expr_process in expr_type.c
2024-12-23 22:03:47 +09:00
Bill Currie
4bdc27b0cd [qfcc] Support struct/enum/handle in type_ref
Forward declarations in generic type refs might have surprising effects
(though they seem to work anyway), but at least they won't cause parse
errors.
2024-12-23 21:44:07 +09:00
Bill Currie
85e6df9374 [qfcc] Clean up some use of default_type
And use typename for handles (still only int/long but better than a
parse error).
2024-12-23 14:59:02 +09:00
Bill Currie
79411aabc4 [qfcc] Rework token pasting
Now it seems to actually work. I don't know if it's compliant with the
spec, but I do at least get expected results, even for some error
conditions.
2024-12-23 14:59:02 +09:00
Bill Currie
15ddd8fea9 [qfcc] Do a better check for recursive macro invocations
Simply checking if the macro's next pointer was set wasn't enough for
when the macro was at the end of the chain (or the only macro in the
chain).
2024-12-23 14:59:02 +09:00
Bill Currie
a57ea628ca [qfcc] Handle swizzle expressions when processing
I suspect this is actually a bug in parameter setup for inline function
calls, but it would certainly be a bug to get an unprocessed swizzle
expression as they're generated from field expressions.
2024-12-13 15:10:00 +09:00
Bill Currie
0337a7cd80 [qfcc] Handle integral aliases in spir-v
Needed for array indexing using unsigned indices.
2024-12-13 15:02:22 +09:00
Bill Currie
74637fffbc [qfcc] Implement the actual return from inline
It's just a jump to a label at the end of the block, but it's enough for
now as it takes care of the assumption that the return is the last
expression in the inlined function.
2024-12-12 11:19:24 +09:00
Bill Currie
626680f22f [qfcc] Handle return in inline functions
The code is pretty lousy in that it assumes there's only one `return`
and it's the end of the function, and the generated code is even worse
(too many load/store ops in the spir-v), but it looks like it at least
works. It does pass validation.
2024-12-12 11:08:30 +09:00
Bill Currie
21eed88da5 [qfcc] Allow initializers in param symtabs for spir-v
This gets the incoming parameters initialized, though currently rather
suboptimally (but that's due to the setup of the call). Next, just need
to handle `return` in inline functions.
2024-12-12 00:39:09 +09:00
Bill Currie
ae0cfe47cc [qfcc] Dereference constructor parameters
Sometimes, premature optimization can help, but whatever. Finally,
alias.vert compiles, but the output is fairly broken (incorrect handling
of `return` and the parameters aren't initialized).
2024-12-11 23:22:28 +09:00
Bill Currie
3eef231d70 [qfcc] Give anonymous block instances a name
They need a valid name for the stricter checks. The name is taken from
the interface name, and since that needs to be unique, prepending a `.`
should be enough.
2024-12-11 23:22:05 +09:00
Bill Currie
14ac685330 [qfcc] Initialize parameters with their arguments
However, once past a couple of errors, they're not getting initialized
in the spir-v, so I've probably missed something there. For now I've
kept things simple and made them regular variables regardless of
qualifiers (and no support for out/inout yet).
2024-12-11 23:16:57 +09:00
Bill Currie
5380c2b04c [qfcc] Process block result even when independent
If the block's result is just a variable reference, it won't match any
expression in the block's list so it needs to be processed independently
in such cases. The `mix(genFType x, genFType y, float a)` inline now
gets as far as spir-v code gen although there are still many issues to
fix (parameter symbols, `return` handling, etc).
2024-12-11 18:24:23 +09:00
Bill Currie
5b13bcf11c [qfcc] Report undefined symbols in proc_symbol
This required always recording the unmangled function symbol, but helps
with error checks in the long run.
2024-12-11 16:31:24 +09:00
Bill Currie
fc519054f2 [qfcc] Check for errors in call processing
Wasn't nice getting segfaults in the constructor code due to simple
errors.
2024-12-11 16:07:43 +09:00
Bill Currie
a0f09b13cf [qfcc] Set current function for inlined functions
It improves error reporting and is even a solution for what to do with
return statements in inline functions.
2024-12-11 16:05:50 +09:00
Bill Currie
36c0b45bab [qfcc] Add an @construct type function
I decided to not mess with actual casts. In retrospect, this is probably
for the best as it avoids any bit-cast conflicts, but should allow for
block initializers with a few tweaks. `@construct (type, args...)` in
Ruamoko follows the same path as `type(args...)` in glsl.
2024-12-11 13:54:11 +09:00
Bill Currie
6840a208b9 [qfcc] Implement inline function calls
They're buggy in that the defspaces for parameters and locals are
incorrect (they need to point to the calling scope's space). Also,
parameters are not yet hooked up correctly. However, errors (because I
need to allow casts from scalars to vectors) do get handled.
2024-12-11 03:16:15 +09:00
Bill Currie
36cf1f948e [qfcc] Add bypass scopes
Because the symbol tables for generic functions are ephemeral (as such),
they need to be easily removed from the scope chain, it's easiest if
definitions are never added to them (instead, they get added to the
parent symbol table). This keeps handling of function declarations or
definitions and their parameter scopes simple as the function gets put
in the global scope still, and the parameter scope simply gets
reconnected to the global scope (really, the generic scope's parent)
when the parameter scope is popped within a generic scope.
2024-12-11 03:06:50 +09:00
Bill Currie
c8158f9ebe [qfcc] Support type expressions in default_type
Needed for using type expressions in casts (eg in generic functions).
2024-12-11 02:46:47 +09:00
Bill Currie
b2049f496b [qfcc] Save caller to new_symbol* in symbol_t
I should go through and do this for all the structs (not looking forward
to expr_t) as it's sometimes quite handy to know who created something.
2024-12-11 02:41:13 +09:00
Bill Currie
306306fc5a [qfcc] Move semantic processing into build_code_function
In the end, I could have done the null context check for pascal, but I
think the xvalue support will come in handy for implementing inline
functions.
2024-12-08 20:01:06 +09:00
Bill Currie
7379b1226d [qfcc] Check for procedures properly in return value
I guess either I goofed and thought void functions had no return type at
all, or at one stage that was the case, but either way, I had never
tested procedures.
2024-12-08 19:17:24 +09:00
Bill Currie
35c0b84ccc [qfcc] Ensure pascal functions always have a block
I had never tested empty functions, so I hadn't noticed they cause a
segfault.
2024-12-08 19:16:29 +09:00
Bill Currie
4a8c53aa6f [qfcc] Add partial support for renamed builtins
The back-end support for renamed builtins (fte's = #0:name) was needed
for pascal functions because the internal name is now prefixed with an @
to allow the lvalue/rvalue selection of behavior for function symbols
2024-12-08 19:13:49 +09:00
Bill Currie
27c3ee4c39 [qfcc] Use deferred semantics for quake pascal
This catches qp up with qc and glsl, which means I can modify all three
languages to support inline functions at the same time. There is the
minor(?) problem of attempting to pass parameters to a
function/procedure that takes none producing the wrong error, but that's
documented in the parser.
2024-12-08 19:12:04 +09:00
Bill Currie
38c64c61b9 [qfcc] Make new_call_expr non-const
I really don't remember why I made it const. Although there are still
others, new_* exression calls are meant to be mutable.
2024-12-08 19:03:41 +09:00
Bill Currie
1e19335b44 [qfcc] Add xvalue expressions and symbols
xvalue symbols refer to two expressions: an lvalue and an rvalue. They
are meant to be used with xvalue expressions.

xvalue expressions are useful when a distinction must be made between
the behavior of something (eg, a pascal function symbol) must change
depending on whether it's an lvalue (assignment of the function's return
value) or an rvalue (a call to the function, especially when the
function takes no parameters).
2024-12-08 18:59:40 +09:00
Bill Currie
ed984e6eae [qfcc] Use raw symbols for field names
Rather than new_name_expr (which messes with the symbol table): field
names just don't need lookup in general, especially for known
structs/vectors.
2024-12-08 00:03:12 +09:00
Bill Currie
213ac2a328 [qfcc] Use a context object to hold current language
Because the glsl front-end uses Ruamoko to compile its builtins, it
needs to switch languages, and the cleanest way to do so is to use a
context object that gets passed around. This removes not only the
current_language global, but also (as a bonus) any real references to
flex's scanner object (there's still a pointer in rua_ctx_t, but it's no
longer a parameter (which caused some pain in the change)).
2024-12-07 23:55:48 +09:00
Bill Currie
e87e01e4f2 [qfcc] Don't fold constants for algebra
While it is a pessimism, I really want to get rid of fold_constants (in
favor of something smarter), and it's currently in the way of more
important things.
2024-12-07 19:09:10 +09:00
Bill Currie
bbc7fcd207 [qfcc] Remove convert_name
It's no longer necessary with the shift to deferred semantics.  The
remaining relevant functionality has been moved to proc_symbol.
2024-12-07 18:35:01 +09:00
Bill Currie
9c037ce79d [qfcc] Clean up some cruft in the glsl parser 2024-12-07 02:53:56 +09:00
Bill Currie
a91595ad6b [qfcc] Make the GA tests a little easier to "use"
All errors are printed now, and the line numbers of the error message
are given, making finding the offending test much easier.
2024-12-07 02:52:10 +09:00
Bill Currie
c5d77141eb [qfcc] Defer Ruamoko semantics processing
The main goal was to make it possible to give generic functions
definitions (since the code would be very dependent on the actual
parameter types), but will also allow for inline functions. It also
helped move a lot of the back-end dependent code out of semantics
processing and almost completely (if not completely) out of the parser.
Possibly more importantly, it gets the dags flushing out of the parser,
which means such is now shared by all front-ends.

There's probably a lot of dead code in expr.c now, but that can be taken
care of another time.
2024-12-07 02:38:00 +09:00
Bill Currie
d08816cb6b [qfcc] Update offset for aliased defs
When getting an offset alias of an already aliased def, the offset needs
to be updated so it points to the correct part of the def. This is
actually a rather old bug but it got uncovered somehow with my rework of
the ruamoko semantic processing (probably an altered cast).
2024-12-07 00:22:53 +09:00
Bill Currie
4e3bacd827 [qfcc] Fix handling of a single id in ... macros
If the id isn't a parameter or other macro, then it can't be empty and
thus the va_args aren't empty.
2024-12-06 22:38:58 +09:00
Bill Currie
bc1b9893a7 [qfcc] Fix vector constructions from scalars
Yet another bit of is_vector/is_nonscalar confusion.
2024-12-05 16:11:59 +09:00
Bill Currie
ec436ee65e [qfcc] Put definition for generic functions in genfunc
It is then copied to the metafunc when finding the actual function to
call. Fixes the last definition defining all instances.
2024-12-05 16:08:39 +09:00
Bill Currie
a5366bf7f9 [qfcc] Fix matrix indexing for ruamoko
The change to exclude matrices from is_nonscalar broke indexing of the
columns.
2024-12-05 14:24:14 +09:00
Bill Currie
639818cca5 [qfcc] Handle unsigned int correctly
I'd missed the one case, so such declarations wound up just `int`
2024-12-05 13:12:27 +09:00
Bill Currie
b4387cd881 [qfcc] Finish type decl for type_ref
I guess I'd wanted to avoid supporting automatic types in type
functions, but that broke long and unsigned int: either no type, or just
int, depending on the decl style.
2024-12-05 13:10:11 +09:00
Bill Currie
a938f1c507 [qfcc] Define the glsl mix builtins
Unfortunately, it turns out that generic functions are over-grouped so
all functions with the same name get the last definition, so `mix` with
a float (which should get GLSLstd450FMix) gets the bool version instead
(SpvOpSelect).
2024-12-05 12:19:04 +09:00
Bill Currie
9f9aecfeec [qfcc] Fix function disambiguation
I was checking for duplicate best matches too early. Local minimum (or
inflection point, really) problem.
2024-12-05 12:08:08 +09:00
Bill Currie
277c5a2318 [qfcc] Optimize matrix construction from a matrix
If the source matrix has at least as many rows as the new matrix, then
the columns are taken from the columns (possibly shortened via swizzle).
2024-12-05 11:45:01 +09:00
Bill Currie
ad45715359 [qfcc] Clean up duplicated vector list construction
I wound up with a few too many copies for my liking.
2024-12-05 11:13:08 +09:00
Bill Currie
81aa77b142 [qfcc] Optimize matrix construction from vectors
When the number of supplied vectors matches the number of columns in the
matrix and all vectors have the same width as the number of rows in the
matrix, there's no need to expand the vectors into components only to be
gathered again.
2024-12-05 10:31:32 +09:00
Bill Currie
14e39627cc [qfcc] Construct matrices by vectors
SPIR-V requires that matrices are constructed from vectors rather than
individual components. While not optimal, iqm.vert's output now passes
spirv-val. Also probably still lots wrong with fine details.
2024-12-05 10:03:01 +09:00
Bill Currie
f014d3f580 [qfcc] Clean up logical not and bool expressions
Got to get rid of a FIXME :)
2024-12-05 02:02:14 +09:00
Bill Currie
4b7025fa0b [qfcc] Implement matrix ops in spirv
And they pass spirv-val.
2024-12-05 02:00:35 +09:00
Bill Currie
a9bff23a6a [qfcc] Define the glsl normalize functions
I think I need to come up with a better way of defining glsl builtins
and the strings are rather ugly (and I don't want to use qfcc system
header files).

iqm.vert now compiles, but doesn't pass validation yet (matrix bugs).
2024-12-05 00:30:17 +09:00
Bill Currie
2a5c9ae45b [qfcc] Emit spirv array types correctly
Array count is an id, not a literal. Oops.
2024-12-05 00:27:27 +09:00
Bill Currie
c185d2463b [qfcc] Support matrices in spirv output
With a few more fixes, iqm.vert builds nicely.
2024-12-05 00:26:07 +09:00
Bill Currie
5e053fe270 [qfcc] Fix an overflow for matrix values
ex_value_t keeps getting bigger :P
2024-12-05 00:22:38 +09:00
Bill Currie
7bbbbe1f0e [qfcc] Improve glsl source line tracking
It's never nice getting an error on the wrong line.
2024-12-05 00:21:19 +09:00
Bill Currie
8cc71395cc [qfcc] Fix some more unsupported expressions in dot 2024-12-05 00:19:48 +09:00
Bill Currie
674fbae225 [qfcc] Rework binary_expr to be support matrices
And, nicely, simplify it quite a bit. I'm not sure why I didn't thinkg
of this approach before. While the ruamoko back-end doesn't support
matrices yet, the expressions are handled.

As a side effect, type checking on comparisons is "stricter" in that
more potentially bogus comparisons (eg, int-float) are caught, resulting
in a few warnings in ruamoko code and even finding a couple of bugs.
2024-12-05 00:18:47 +09:00
Bill Currie
8e2fe83c27 [qfcc] Add a function for checking type comparisons
It returns true if the comparison is valid without a diagnostic (usually
a warning), false otherwise.
2024-12-05 00:10:15 +09:00
Bill Currie
8d69ca9a99 [qfcc] Simplify type_promotes and type_demotes
Using bit masks for valid source types for each destination type makes
the logic a lot easier to read. Still had to have some explicit checks
for enums and bools.
2024-12-05 00:10:15 +09:00
Bill Currie
f4a8a5e179 [qfcc] Fix some inconsistent tokens
I need to find a good way to share token defs between bison files.
2024-12-05 00:10:15 +09:00
Bill Currie
7a30ac9527 [qfcc] Clear current function for constexpr evaluation
The code gen return statements checks for out/inout parameters in the
current function and thus could result in some undesired behavior when
constants are evaluated within such a function.
2024-12-05 00:10:15 +09:00
Bill Currie
5714433a9c [qfcc] Clean up some more bools 2024-12-05 00:10:15 +09:00
Bill Currie
b03132825e [qfcc] Fix a typo in a variable name 2024-12-04 23:48:43 +09:00
Bill Currie
4800fef4fd [qfcc] Give short and ushort width of 1
I'm not sure why I thought a width of 0 was a good idea, but it proved
to be problematic when implementing matrices.
2024-12-04 23:47:58 +09:00
Bill Currie
58b78cfdec [qfcc] Separate matrices from non-scalars
While matrices are non-scalars in math, is_nonscalar is meant for any
vector type because is_vector is specifically for the quakec vector
type.
2024-12-03 14:56:38 +09:00
Bill Currie
7c56c10d25 [qfcc] Support matrices in binary expressions
Partially. There are problems with scaling and code-gen.
2024-11-28 23:30:39 +09:00
Bill Currie
d0db75779a [qfcc] Add a check for shifts and expand math ops
I found a need to check for shifts separately (not sure it's the right
approach for that problem, though), and there are a few more math ops
than just +-*/.
2024-11-28 21:16:14 +09:00
Bill Currie
8323868b2c [qfcc] Add an opcode for outer products
I'm not sure what it's useful for, but GLSL has a function for it thus I
decided to add the instruction to the VM, so this is part of the
compiler side.
2024-11-28 21:16:14 +09:00
Bill Currie
c4f48353b9 [qfcc] Correct a typo in a comment 2024-11-28 21:05:34 +09:00
Bill Currie
0652a103d0 [qfcc] Correct edge labels for field expressions
I had f and o reversed and decided to use m (member) instead of f.
2024-11-28 20:09:51 +09:00
Bill Currie
2b70c54b15 [qfcc] Fix an uninitialized variable
I'd forgotten to do an optimized compile check :/
2024-11-26 16:26:45 +09:00