Skip to content

Difference with reference. #3549

Description

@LazyDodo

Trying to build a perlin noise generator and ran into bizarre looking results. I Boiled it down to the most basic example that showcases the issue for me.

#include <stdio.h>

#include "Halide.h"

using namespace Halide;

/* Basic 2d random function from https://thebookofshaders.com/11/ */
template <class T>
T rand(T x, T y)
{
	T tx = x * 12.9898f;  // 1 ok
	T ty = y * 78.233f;   // 2 ok
	T dot = ty + tx;      // 3 small error begins here
	T sval = sin(dot);    // 4 still small
	T sval_mul = sval * 43758.5453123f; //5 small error * large number = large error
	T fract = sval_mul - floor(sval_mul); // 6 yeah......
	T Res = fract; /* use this to select an output , issue starts at 3
	return Res;
}

#define size 10
int main(int argc, char **argv) {
	Var x("x"), y("y");
	Func r("r");

	/* There is no issue if offsetx == 0.0f*/
	float offsetx = 1.0f;
	r(x, y) = rand<Expr>(x + offsetx, y);
	Buffer<float> res = r.realize(size, size);
	for (int ix = 0; ix < size; ix++)
	{
		for (int iy = 0; iy < size; iy++)
		{
			float fval = rand<float>(ix+offsetx, iy);
			if (res(ix, iy) != fval)
			{
				printf("%d,%d got:%f expected:%f  diff:%f\n", ix, iy, res(ix,iy), fval , fval - res(ix,iy));
			}
		}
	}
	return 0;
}

output on win64 (can't test on any other platforms sorry)

1,2 got:0.117188 expected:0.467773  diff:0.350586
2,1 got:0.449219 expected:0.257813  diff:-0.191406
2,3 got:0.813477 expected:0.068359  diff:-0.745117
2,4 got:0.268311 expected:0.934204  diff:0.665894
2,5 got:0.546875 expected:0.834961  diff:0.288086
2,6 got:0.187500 expected:0.064453  diff:-0.123047
3,1 got:0.152344 expected:0.273438  diff:0.121094
3,2 got:0.285156 expected:0.972656  diff:0.687500
3,6 got:0.858398 expected:0.500488  diff:-0.357910
4,7 got:0.631470 expected:0.301025  diff:-0.330444
4,8 got:0.103516 expected:0.583008  diff:0.479492
4,9 got:0.738281 expected:0.875000  diff:0.136719
5,6 got:0.035156 expected:0.994141  diff:0.958984
6,1 got:0.320313 expected:0.728516  diff:0.408203
6,2 got:0.867188 expected:0.335938  diff:-0.531250
6,4 got:0.238281 expected:0.460938  diff:0.222656
7,1 got:0.089600 expected:0.423096  diff:0.333496
7,3 got:0.923828 expected:0.880859  diff:-0.042969
7,5 got:0.097656 expected:0.726563  diff:0.628906

The core issue seems to be that the halide output for Rand(1 + 1.0f,1) and rand(2,1) are vastly different, sadly for perlin noise these two values are interpolated and you get very odd looking results like this:

test

While the reference implementation is fine.

ref

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions