Commit Graph

106 Commits

Author SHA1 Message Date
Jelle Raaijmakers
526390ec06 LibSoftGPU: Move back to i32-based subpixels
Our move to floating point precision has eradicated the pixel artifacts
in Quake 1, but introduced new and not so subtle rendering glitches in
games like Tux Racer. This commit changes three things to get the best
of both worlds:

1. Subpixel logic based on `i32` types was reintroduced, the number of
   bits is set to 6. This reintroduces the artifacts in Quake 1 but
   fixes rendering of Tux Racer.

2. Before triangle culling, subpixel coordinates are calculated and
   stored in `Triangle`. These coordinates are rounded, which fixes the
   Quake 1 artifacts. Tux Racer is unaffected.

3. The triangle area (actually parallelogram area) is also stored in
   `Triangle` so we don't need to recalculate it later on. In our
   previous subpixel code, there was a subtle disconnect between the
   two calculations (one with and one without subpixel precision) which
   resulted in triangles incorrectly being culled. This fixes some
   remaining Quake 1 artifacts.
2022-05-05 20:50:46 +02:00
Jelle Raaijmakers
e70dc20650 LibSoftGPU: Replace some interpolate() calls with VectorN::dot
The dot product is effectively the same as `interpolate()`, allowing us
to use `VectorN`. No functional changes.
2022-05-05 20:50:46 +02:00
Jelle Raaijmakers
dfb218f6a8 LibSoftGPU: East-const interpolate
No functional changes.
2022-05-05 20:50:46 +02:00
Jelle Raaijmakers
6090e79191 LibSoftGPU: Use u64 for Device statistics vars
No functional changes.
2022-05-05 20:50:46 +02:00
Jelle Raaijmakers
e1a6966863 LibSoftGPU: Check for bottom edge in top-left rule in Device
If a triangle edge is completely horizontal and moving in a positive X
direction, we were erroneously treating it as a top edge. This adds
a better check that accounts for those edges. :^)
2022-04-20 14:12:56 +02:00
Jelle Raaijmakers
f680d82086 LibSoftGPU: Reuse edge function for front/back culling
We sat on a throne of lies: our `edge_function()` returned positive
values for _clockwise_ vertex rotation instead of _counter-clockwise_,
which was hidden by the fact that we were forcing everything into CW
rotation by an erroneous area comparison (`> 0` instead of `< 0`).

This simplifies our culling code significantly.
2022-04-20 14:12:56 +02:00
Jelle Raaijmakers
26a463506e LibSoftGPU: Use AK::abs directly instead of fabsf
Let's not go through LibC.
2022-04-20 14:12:56 +02:00
Jesse Buhagiar
c9f44c746a LibGL+LibSoftGPU: Add GL_ADD Texture Environment 2022-04-09 11:40:33 +02:00
Stephan Unverwerth
5bb76e9b63 LibGL+LibGPU+LibSoftGPU: Load SoftGPU driver dynamically
This loads libsoftgpu.so during GLContext creation and instantiates the
device class which is then passed into the GLContext constructor.
2022-04-06 11:32:24 +02:00
Stephan Unverwerth
4a99875582 LibGL+LibGPU+LibSoftGPU: Add virtual base class for Images
This introduces a new device independent base class for Images in LibGPU
that also keeps track of the device from which it was created in order
to prevent assigning images across devices.
2022-04-06 11:32:24 +02:00
Stephan Unverwerth
1f3642ed48 LibGPU+LibSoftGPU: Move RasterizerOptions into LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
5d2740217f LibGL+LibGPU+LibSoftGPU: Move Vertex.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
5bf224708f LibGPU+LibSoftGPU: Move LightModelParameters into LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
8f359bf758 LibGPU+LibSoftGPU: Move RasterPosition into its own header in LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
e416380826 LibGL+LibGPU+LibSoftGPU: Move StencilConfiguration.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
b652db1f54 LibGL+LibGPU+LibSoftGPU: Move Material.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
7da9447619 LibGL+LibGPU+LibSoftGPU: Move Light.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
e7450fa940 LibGL+LibGPU+LibSoftGPU: Move SamplerConfig to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
4e0643ae97 LibGL+LibGPU+LibSoftGPU: Move ImageFormat.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
24d420312c LibGL+LibGPU+LibSoftGPU: Move Enums.h to LibGPU 2022-04-06 11:32:24 +02:00
Stephan Unverwerth
ac033dd9b6 LibGL+LibGPU+LibSoftGPU: Move DeviceConfig to LibGPU
This introduces a new abstraction layer, LibGPU, that serves as the
usermode interface to GPU devices. To get started we just move the
DeviceConfig there and make sure everything still works :^)
2022-04-06 11:32:24 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Jelle Raaijmakers
8a3242cb83 LibGL+LibSoftGPU+LibGfx: Reimplement normal transformation
We now support generating top-left submatrices from a `Gfx::Matrix`
and we move the normal transformation calculation into
`SoftGPU::Device`. No functional changes.
2022-03-27 09:19:43 -07:00
Jelle Raaijmakers
bc5e5afc7b LibSoftGPU+LibGfx: Transform and normalize normals before lighting
We were transforming the vertices' normals twice (bug 1) and
normalizing them after lighting (bug 2). In the lighting code, we were
then diverting from the spec to deal with the normal situation, which
is now no longer needed.

This fixes the lighting of Tux in Tux Racer.
2022-03-27 09:19:43 -07:00
Jelle Raaijmakers
284a629ab4 LibGL+LibSoftGPU: Implement more of GL_LIGHT_MODEL_COLOR_CONTROL
This gets rid of a place where OpenGL was leaking into LibSoftGPU.
2022-03-27 09:19:43 -07:00
Lenny Maiorani
759857b597 LibSoftGPU: Avoid copying data when doing triangle rasterization
Several large-ish objects shouldn't be copied to the stack when a
reference will do.
2022-03-18 23:41:06 +00:00
Jelle Raaijmakers
870b835115 LibGfx+LibSoftGPU: Allow indexed reads into Gfx::Vector 2022-03-11 12:30:43 +01:00
Jelle Raaijmakers
20a5a4363c LibSoftGPU: Remove unused vector types from Device 2022-03-11 12:30:43 +01:00
Jelle Raaijmakers
d75135663b LibGfx+LibSoftGPU: Add and use Vector.xy()
Also use `.xyz()` where appropriate.
2022-03-07 11:00:45 +01:00
Jelle Raaijmakers
439617cf6f LibSoftGPU: Use lroundf instead of roundf in rasterization rect
Casting a `float` to `int` might still inadvertently floor the value,
while `lroundf` will return a properly rounded `long`.
2022-03-07 11:00:45 +01:00
Jelle Raaijmakers
37dd10fbbe LibSoftGPU: Use float instead of int for triangle screen coords
This replaces the fixed point subpixel precision logic.

GLQuake now effectively renders artifact-free. Previously white/gray
pixels would sometimes be visible at triangle edges, caused by slightly
misaligned triangle edges as a result of converting the vertex window
coordinates to `int`. These artifacts were reduced by the introduction
of subpixel precision, but not completely eliminated.

Some interesting changes in this commit:

* Applying the top-left rule for our counter-clockwise vertices is now
  done with simpler conditions: every vertex that has a Y coordinate
  lower than or equal to the previous vertex' Y coordinate is counted
  as a top or left edge. A float epsilon is used to emulate a switch
  between `> 0` and `>= 0` comparisons.

* Fog depth calculation into a `f32x4` is now done once per triangle
  instead of once per fragment, and only if fog is enabled.

* The `one_over_area` value was previously calculated as `1.0f / area`,
  where `area` was an `int`. This resulted in a lower quality
  reciprocal value whereas we can now retain floating point precision.
  The effect of this can be seen in Tux Racer, where the ice reflection
  is noticeably smoother.
2022-03-07 11:00:45 +01:00
Jelle Raaijmakers
9fc419395e LibSoftGPU: Use destination alpha for texture decal function
According to the OpenGL spec, the decal function uses the destination
(or texture) alpha instead of the source alpha. This fixes the sky in
GLQuake.
2022-03-06 01:38:01 +01:00
Jelle Raaijmakers
e1c863d99a LibSoftGPU: Use non-normalized light vector for attenuation
There were some issues with the old code: we were saving the length of
the light vector but not actually using it anywhere, if we were dealing
with a zero-vector this could potentially divide by zero resulting in a
black fragment color, and we were erroneously using P2's length instead
of P1's length when P1's W coordinate is zero in the SGI arrow
operation.

This fixes some lighting bugs in Grim Fandango, but this probably
affects all lighting as well.
2022-03-05 21:23:56 +01:00
Jelle Raaijmakers
c3273e9492 LibSoftGPU: Clean up some conditionals in Device
No functional changes.
2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
f28047de73 LibSoftGPU: Round rasterization position to nearest integer
This fixes the issue where e.g. `299.97` would be cast to an integer
value of `299`, whereas the pixel's center would lie at `299.5` and
would then erroneously be excluded.
2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
8c9fa50c61 LibSoftGPU: Apply regular cartesian coordinate system
Currently, LibSoftGPU is still OpenGL-minded in that it uses a
coordinate system with the origin of `(0, 0)` at the lower-left of
textures, buffers and window coordinates. Because we are blitting to a
`Gfx::Bitmap` that has the origin at the top-left, we need to flip the
Y-coordinates somewhere in the rasterization logic.

We used to do this during conversion of NDC-coordinates to window
coordinates. This resulted in some incorrect behavior when
rasterization did not pass through the vertex transformation logic,
e.g. when calling `glDrawPixels`.

This changes the coordinate system to OpenGL's throughout, only to blit
the final color buffer upside down to the target bitmap. This fixes
drawing to the depth buffer directly resulting in upside down images.
2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
971b39ae4f LibSoftGPU: Rename rgba to bgra to reflect actual value 2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
db0616c67a LibSoftGPU: Generalize pixel buffers and standardize on BGRA8888
Between the OpenGL client and server, a lot of data type and color
conversion needs to happen. We are performing these conversions both in
`LibSoftGPU` and `LibGL`, which is not ideal. Additionally, some
concepts like the color, depth and stencil buffers should share their
logic but have separate implementations.

This is the first step towards generalizing our `LibSoftGPU` frame
buffer: a generalized `Typed3DBuffer` is introduced for arbitrary 3D
value storage and retrieval, and `Typed2DBuffer` wraps around it to
provide in an easy-to-use 2D pixel buffer. The color, depth and stencil
buffers are replaced by `Typed2DBuffer` and are now managed by the new
`FrameBuffer` class.

The `Image` class now uses multiple `Typed3DBuffer`s for layers and
mipmap levels. Additionally, the textures are now always stored as
BGRA8888, only converting between formats when reading or writing
pixels.

Ideally this refactor should have no functional changes, but some
graphical glitches in Grim Fandango seem to be fixed and most OpenGL
ports get an FPS boost on my machine. :^)
2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
6318522339 LibSoftGPU: Remove Device::wait_for_all_threads()
This function was added as a FIXME but was then arbitrarily invoked in
the rest of `Device`. We are better off removing this FIXME for now and
reevaluate introducing multithreading later on, so the code is not
littered with useless empty function calls.
2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
4d53a87355 LibSoftGPU: Remove unused AK/Function.h include 2022-02-22 23:48:59 +00:00
Jelle Raaijmakers
a5ad702387 LibSoftGPU: Use fabsf instead of fabs for float
Let's not do a `float -> double -> float` roundtrip. :^)
2022-02-22 23:48:59 +00:00
Lenny Maiorani
d1a87f1219 LibSoftGPU: Use default construction to clear AlphaBlendFactors
Clearing the `m_alpha_blend_factors` is performed manually and in
separate steps. This is error prone for future developers. The
behavior is to reset the entire `struct` to the same state as default
initialization, so this simplifies it to do just that.
2022-01-27 20:39:02 +00:00
Lenny Maiorani
065525aba0 LibSoftGPU: Configure stats overlay period
Problem:
- The statistics overlay period is hardcoded to 500 ms. This time is
  very short and can result in the values being very "jumpy".

Solution:
- Increasing this value can result in more steady values which is
  useful when trying to evaluate the performance impact of a change. A
  new config value is offered in `Config.h` to let the developer
  change to any value desired.
2022-01-22 08:57:31 +03:30
Jelle Raaijmakers
a8655fbf52 LibGL+LibSoftGPU: Clean up some for loops 2022-01-20 10:35:01 +01:00
Jelle Raaijmakers
453f62c935 LibGL+LibSoftGPU: Implement GL_POLYGON_OFFSET_FILL capability 2022-01-20 10:35:01 +01:00
Stephan Unverwerth
12f63df329 LibGL+LibSoftGPU: Support generation of multiple texture coordinates 2022-01-19 19:57:49 +01:00
Stephan Unverwerth
7571ef0343 LibGL+LibSoftGPU: Add multiple texture coordinates to vertex struct
We now have one set of texture coordinates per texture unit.
Texture coordinate generation and texture coordinate assignment is
currently only stubbed. This will be rectified in another commit.
2022-01-19 19:57:49 +01:00
Stephan Unverwerth
5505f353e8 LibSoftGPU: Announce NPOT texture support via DeviceInfo struct 2022-01-19 19:57:49 +01:00
Jesse Buhagiar
865e7bbe5e LibGL+LibSoftGPU+3DFileViewer: Implement Specular highlighting :^) 2022-01-18 01:48:51 +02:00
Jesse Buhagiar
5a735602b0 LibSoftGPU: Add sgi_dot lambda
This function is used quite a bit during the lighting calculations, so
it's a bit cleaner having it in a centralized spot instead of just
arbitrarily calling `dot()` with numerous `FloatVector3` conversions.
2022-01-18 01:48:51 +02:00