Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question ComputeShader set value for Keyword

Discussion in 'General Graphics' started by Shushustorm, May 15, 2023.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hey everyone!
    I don't think this is possible, but maybe I'm missing something; let's say there's something like
    Code (CSharp):
    1. #if CHANNEL_R
    2. #elif CHANNEL_G
    3. #elif CHANNEL_B
    4. #elif CHANNEL_A
    5. #endif
    Let's say only one channel should be set at any time.
    Now when I want to set a keyword, I always have to disable all the other keywords.
    It'd be great if something like this existed:
    Code (CSharp):
    1. #if CHANNEL==R
    2. #elif CHANNEL==G
    3. #elif CHANNEL==B
    4. #elif CHANNEL==A
    5. #endif
    Then, one could set the keyword and assign a value to it like: EnableKeyword(CHANNEL,A)?
    Best wishes,
    Shu
     
  2. kripto289

    kripto289

    Joined:
    Feb 21, 2013
    Posts:
    501
    It's not possible, because
    1. #if CHANNEL_R
    2. #elif CHANNEL_G
    3. #elif CHANNEL_B
    4. #elif CHANNEL_A
    create 4 variants.
    https://docs.unity3d.com/Manual/shader-variants.html

    In your case "if CHANNEL==R" can create infinite shader variants.
    For example
    if CHANNEL==0
    if CHANNEL==1
    if CHANNEL==2
    if CHANNEL==3
    if CHANNEL==100000
    if CHANNEL==32423453453245342
    if CHANNEL==
     
    Shushustorm likes this.
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks for the reply! I guess I understand the core of the issue, but when, for example, I never assign a value of 100000, there shouldn't be a variant for that value? So the compiler should check for keywords and values and base variants according to both? Anyway, since it's not possible, it's not possible! I'll keep going with the keywords the way they're intended!
     
  4. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    The keyword value could be entered by the user which is impossible to predict:
    material.SetKeyword("CHANNEL", Console.In.ReadLine());

    There is no way to determine the valid keyword values automatically, however, you can design a system where you specify all allowed values in the #pragma multi_compile statement (I'm doing something along those lines in my personal engine). Unity just chose not to support that.

    So instead of
    #pragma multi_compile CHANNEL_R CHANNEL_G CHANNEL_B CHANNEL_A

    you could have a system that allowed you to write
    #define R 0
    #define G 1
    #define B 2
    #define A 3
    #pragma multi_compile CHANNEL=R CHANNEL=G CHANNEL=B CHANNEL=A
     
    Last edited: May 16, 2023
    Shushustorm likes this.
  5. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Well that would be quite interesting, but since it's not supported, I guess it's a dead end. Anyway, interesting nonetheless!