Darkyenus / glsl4idea

A GLSL language plugin for IntelliJ IDEA
GNU Lesser General Public License v3.0
101 stars 31 forks source link

Built-in methods, variables and structs support #6

Open Darkyenus opened 10 years ago

Darkyenus commented 10 years ago

There are already unused files for this in resources/environment.

Darkyenus commented 9 years ago

We should probably solve glsl versions first. Probably most intuitive would be to:

Using this philosophy, extensions could be handled as well.

However, this will create a lot of redundancy, because most of the functions stay the same between versions. Alternatives would be:

Question also is, how to recognize and account for GLES shaders in this system.

AbigailBuccaneer commented 9 years ago

I've added more files to this in the environment branch. I think once we have better preprocessing it makes more sense to just use one file with preprocessor directives to handle this. Alternatively, if we supported annotations (eg. GCC-like __attribute__((version(410)) or via #pragma somethingorother) that'd then give an easy way of specifying them all in one file and having information about the functions not in the current version.

GLES SL versions are 100, 300 and 310, which conveniently have never been used for GLSL versions (which started at 110 and skipped from 150 to 330). This may not always be the case, but Khronos will have to think about it as much as we will. Of course, this does mean that you can't assume that the version numbers form a single monotonic line, as I have done in the environment branch.

AbigailBuccaneer commented 9 years ago

(See #40 which is a prerequisite for this.)

Darkyenus commented 9 years ago

I'd like to keep the ability to recognise the function even if doesn't exist in the current version, but I think that the preprocessor support would have to be very advanced to be able to do that reliably. I don't know, maybe it will be easier then it seems, but so far I'm in favor of annotating somehow. Since these annotations will be supported only in environment files, we can make up our own syntax that is readable and comfortable.

Making the tag up isn't trivial though, but if we assume that GLES versions will be always different to GL versions, something like <110,120,300> float cos(float x); (for denoting that cos is available in GL 110, 120 and GLES 300 (only those versions)) might be enough. Something like <versions>{ function1; function2; ...} might help with resulting verbosity and still be easy to parse. (Maybe even easier)

AbigailBuccaneer commented 9 years ago

C++11 attributes ([[version(110, 300)]]) might be nice - they look clean and minimal like your angle-bracket suggestion, but are also supported as standard in other languages (ie. C++11) so there's some precedence for it. Double opening square brackets aren't currently valid anywhere in the language, but you can make something that's syntactically well-formed with something like 100<110,120,300>330.

Supporting attributes for things other than versions would be hypothetically useful for adding other annotations to the standard library - possibly something like [[minimum(8)]] for the implementation-defined limits that are defined in the shader environment?

AbigailBuccaneer commented 9 years ago

It can also be used for error highlighting based on expression analysis, as well as feeding more info back into the analysis:

float [[contract(x >= 0 && sqrt(x) >= 0)]] sqrt(float x);
Fox32 commented 9 years ago

First, very nice plugin!

Does this issue include the missing deduction of return values for build-in functions, or should I create a new one? For example:

float unpackFloat(vec4 v) {
  return dot(v, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));
}

It is able to deduce the types of the arguments to dot but unable to deduce it's return type and shows a warning.

Darkyenus commented 9 years ago

@Fox32 Thanks!

It does, support for methods here means that the plugin will know which methods there are, what arguments do they take and what type they return.

More thoughts on the issue: What if the importing wasn't done by annotated source file, but through something directly in Java? There could be an object which would allow specifying and searching for methods/etc., let's call it FunctionRepository which would allow doing something like this:

class GLSLFunctionRepository extends FunctionRepository {
    //function(name, returnType, argTypes...);
    function("sin", GEN_TYPE, GEN_TYPE);
    function("cos", GEN_TYPE, GEN_TYPE);
    function("doMagic", FLOAT, GEN_TYPE, GEN_TYPE).since(130).sinceES(150);
    ...
}

FunctionRepository fr;
FunctionType ft = fr.findFunction("doMagic", VEC2, VEC2);
ft.returnType == FLOAT

Similar "Repository" for variables and types or it could be integrated into one.

I am seriously considering this, because it will be less work, faster, more flexible, predictable, and we will have tools to deal with it (refactoring java is easier than glsl with custom annotations).

AbigailBuccaneer commented 9 years ago

Having had a further look at how function lookups and parameter info is handled, I suspect building the function list in Java would be easier. (Specifically, a ResolveResult can be either a PsiElementResolveResult or a custom implementation that can be used to resolve to built-in functions.)

wyozi commented 8 years ago

In my game engine I support importing shaders using #pragma import filename. I've been thinking of a way to allow plugin user to specify what kind of syntax their imports use and then add support for file imports to glsl4idea. Haven't had any good ideas about that yet but the same concept could be used for built in functions.

The built-in functions are conveniently stored in files as is, so if built-in function loading was implemented by implicitly (without explicit annotations or directives) importing the resource files, it would solve a few problems:

While Java function repository idea would definitely be faster, I think it would also be less flexible. I will maybe look more into the idea later.

Also sorry about messy text flow, it's late.

darkoffalex commented 4 years ago

Hello. Thanks for plugin. What is the status of the issue now? When can we expect this enhancement to be implemented?

Darkyenus commented 4 years ago

I don't currently have much time to work on my OSS, so don't expect this to be done anytime soon.