0

I'm trying to convert one of my methods into a multi-line define directive to implement the solution in this question: Can you use #defines like method parameters in HLSL?. However, I'm not having much success with it, and am getting difficult to parse errors.

Here is what I've done:


float Random2D(float x, float y, int seed = 0)
{
    return (frac(sin(fmod(dot(float2(x, y), float2(12.9898,78.233)), 6.2831853)) * (43758.5453 + seed))) * 1;
}

#define BASE_NOISE(x, y, seed) Random2D(x, y, seed)

#define FBMNoiseSampler(x, y, octaves, persistance, lacunarity, seed)                                                           \
{                                                                                                                               \
    float value = 0;                                                                                                            \
    float amplitude = 1;                                                                                                        \
    float frequency = 1;                                                                                                        \
                                                                                                                                \
    float maxVal = 0;                                                                                                           \
    float minVal = 0;                                                                                                           \
                                                                                                                                \
    for (uint i = 0; i < octaves; i++)                                                                                          \
    {                                                                                                                           \
        float2 sample = float2(x , y) * frequency;                                                                              \
        float sampleResult = BASE_NOISE(sample.x, sample.y, seed) * 2 - 1; // Changes range from [0, 1] to [-1, 1].             \
        result += sampleResult * amplitude; // Summation of each sample value as they go up octaves                             \
                                                                                                                                \
        maxVal += 1 * amplitude;                                                                                                \
        minVal -= 1 * amplitude;                                                                                                \
                                                                                                                                \
        amplitude *= persistance; // Amplitude decreases as the octaves go up as persistance [0, 1]                             \
        frequency *= lacunarity; // Frequency increases as octaves go up as frequency [1, inf)                                  \
    }                                                                                                                           \
    // Normalizing back to [0, 1]                                                                                               \
    // Formula:     finalValue = ((initialValue - center of range) / (length of range)) + 0.5                                   \
    return ((value - (maxVal + minVal) / 2) / (maxVal - minVal)) + 0.5;                                                         \
}

And here's how I'm calling it in CSMain in a compute shader:

#undef BASE_NOISE
#define BASE_NOISE(x, y, seed) Random2D(x, y, seed) // Or any other noise function
value = FBMNoiseSampler(0.5, 0.5, 1, 0.5, 2.5, 0)

But this gives me the following errors:

Shader error in 'ExampleFile': 'Random2D': no matching 0 parameter function at kernel CSMain at ExampleFile.compute(x) (on d3d11)

Shader error in 'ExampleFile': syntax error: unexpected float constant at kernl CSMain at ExampleFile.compute(x) (on d3d11)

Shader error in 'ExampleFile': syntax error: unexpected token '{' at kernel CSMain at ExampleFile.compute(x) (on d3d11)

Where x in the line that I'm calling the macro from.

How do I properly turn the FBMNoiseSampler method into a macro? From what I can tell the backslashes are in the right places according to many example multi-line macros (e.g. https://www.geeksforgeeks.org/multiline-macros-in-c/), and I couldn't find any resources that showed how to return a value in these multi-line macros.

This is a repost (Old one deleted) with more specific examples. Any help is much appreciated.

Oliver
  • 11
  • 2

1 Answers1

0

Here are some problems with your definition:

  1. You cannot return something in a define directive (unless you mean to return in the caller function).
  2. Define directives cannot contain comments since the last \ will be ignored.
cynnad
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '23 at 13:15