AcademySoftwareFoundation / AcademySoftwareFoundation/OpenShadingLanguage

fmod producing the wrong results on the GPU

Open
#1,384 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.3k
Forks
414
Avg merge
3d 1h
Merged PRs (30d)
10

Description

### Problem

We have received reports of `fmod` giving the wrong answer on the GPU when the denominator is negative. I was able to confirm this behavior in `testshade` using a simple shader, given below.

Interestingly, `fmod` does produce the correct result when the argument is a local variable or a non-varying parameter, but not with varying parameters.

### Steps to Reproduce

```C
shader test (float a = -0.666667 [[ int lockgeom=0 ]],
float b = -0.666667 [[ int lockgeom=1 ]],
output color Cout = 0)
{
float denom = 0.5;
printf("fmod(%f, %f) = %f\n", a, denom, fmod(a, denom));
printf("fmod(%f, %f) = %f\n", b, denom, fmod(b, denom));
}

```
```
$ testshade --optix -g 1 1 test
fmod(-0.666667, 0.500000) = 0.333333
fmod(-0.666667, 0.500000) = -0.166667

$ testshade -g 1 1 test
fmod(-0.666667, 0.500000) = -0.166667
fmod(-0.666667, 0.500000) = -0.166667
```

### Versions

* OSL branch/version: master (b0ed183b)
* OS: Ubuntu 20.04
* C++ compiler: clang 12.0.0
* LLVM version: LLVM 12.0.0
* OIIO version: master (a07efab5)

Contributor guide

Open the contributing guide

Research direction

Reproduce the discrepancy with the supplied shader using testshade --optix and the CPU command, focusing on fmod with varying versus local or non-varying parameters. Trace the GPU/OptiX handling of fmod for those parameter kinds; done means the GPU and CPU outputs agree for the reproduction case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.