Commit Graph

126 Commits

Author SHA1 Message Date
Jelle Raaijmakers
00d46e5d77 LibGL+LibGPU+LibSoftGPU: Implement matrix stack per texture unit
Each texture unit now has its own texture transformation matrix stack.
Introduce a new texture unit configuration that is synced when changed.
Because we're no longer passing a silly `Vector` when drawing each
primitive, this results in a slightly improved frames per second :^)
2022-09-11 22:37:07 +01:00
Jelle Raaijmakers
dda5987684 LibGL+LibGPU+LibSoftGPU: Remove concept of layer in favor of depth
Looking at how Khronos defines layers:

  https://www.khronos.org/opengl/wiki/Array_Texture

We both have 3D textures and layers of 2D textures, which can both be
encoded in our existing `Typed3DBuffer` as depth. Since we support
depth already in the GPU API, remove layer everywhere.

Also pass in `Texture2D::LOG2_MAX_TEXTURE_SIZE` as the maximum number
of mipmap levels, so we do not allocate 999 levels on each Image
instantiation.
2022-09-11 22:37:07 +01:00
Jelle Raaijmakers
44953a4301 LibGL+LibGPU+LibSoftGPU: Implement glCopyTex(Sub)?Image2d
These two methods copy from the frame buffer to (part of) a texture.
2022-09-11 22:37:07 +01:00
Jelle Raaijmakers
eb81b66b4e LibGL+LibGPU+LibSoftGPU: Rename blit_color_buffer_to
This makes it consistent with our other `blit_from_color_buffer` and
paves the way for a third method that will be introduced in one of the
next commits.
2022-09-11 22:37:07 +01:00
Jelle Raaijmakers
1d36bfdac1 LibGL+LibSoftGPU: Implement fixed pipeline support for GL_COMBINE
`GL_COMBINE` is basically a fixed function calculator to perform simple
arithmetics on configurable fragment sources. This patch implements a
number of texture env parameters with support for the RGBA internal
format.
2022-09-11 22:37:07 +01:00
Jelle Raaijmakers
b42feb76a0 LibSoftGPU: Use approximation for maximum depth slope
OpenGL allows GPUs to approximate a triangle's maximum depth slope
which prevents a number computationally expensive instructions. On my
machine, this gives me +6% FPS in Quake III.

We are able to reuse `render_bounds` here since it is the containing
rect of the (X, Y) window coordinates of the triangle, thus its width
and height are the maximum delta X and delta Y, respectively.
2022-09-08 12:07:03 -04:00
Jelle Raaijmakers
94f016b363 LibGL+LibGPU+LibSoftGPU: Report texture env add extension
The Quake 3 port makes use of this extension to determine a more
efficient multitexturing strategy. Since LibSoftGPU supports it, let's
report the extension in LibGL. :^)
2022-08-28 23:45:43 +01:00
Jelle Raaijmakers
84c4b66721 LibGL+LibGPU+LibSoftGPU: Implement texture pixel format support
In OpenGL this is called the (base) internal format which is an
expectation expressed by the client for the minimum supported texel
storage format in the GPU for textures.

Since we store everything as RGBA in a `FloatVector4`, the only thing
we do in this patch is remember the expected internal format, and when
we write new texels we fixate the value for the alpha channel to 1 for
two formats that require it.

`PixelConverter` has learned how to transform pixels during transfer to
support this.
2022-08-27 12:28:05 +02:00
Jelle Raaijmakers
eb7c3d16fb LibGL+LibGPU+LibSoftGPU: Implement flexible pixel format conversion
A GPU (driver) is now responsible for reading and writing pixels from
and to user data. The client (LibGL) is responsible for specifying how
the user data must be interpreted or written to.

This allows us to centralize all pixel format conversion in one class,
`LibSoftGPU::PixelConverter`. For both the input and output image, it
takes a specification containing the image dimensions, the pixel type
and the selection (basically a clipping rect), and converts the pixels
from the input image to the output image.

Effectively this means we now support almost all OpenGL 1.5 formats,
and all custom logic has disappeared from:
  - `glDrawPixels`
  - `glReadPixels`
  - `glTexImage2D`
  - `glTexSubImage2D`

The new logic is still unoptimized, but on my machine I experienced no
noticeable slowdown. :^)
2022-08-27 12:28:05 +02:00
Linus Groh
173dcfb7cb Everywhere: Fix a bunch of typos 2022-05-29 15:22:00 +02:00
Jelle Raaijmakers
421a80bf43 LibSoftGPU: Update coverage bits after alpha testing
Also skip the test for the `::Always` alpha test function in the hot
loop. This test function is very unlikely to be set, so leave that up
to `::test_alpha()`.
2022-05-15 12:15:12 +02:00
Jelle Raaijmakers
1a338844fa LibSoftGPU: Make Device statistics i64 again
They were erroneously converted into `u64` by me in a previous commit,
causing the overdraw statistic to go haywire.
2022-05-15 12:15:12 +02:00
RKBethke
0836912a6d LibGL+LibGPU+LibSoftGPU: Implement and expose glClipPlane
This commit implements glClipPlane and its supporting calls, backed
by new support for user-defined clip planes in the software GPU clipper.

This fixes some visual bugs seen in the Quake III port, in which mirrors
would only reflect correctly from close distances.
2022-05-11 23:09:47 +02:00
Jelle Raaijmakers
5861f1821e LibSoftGPU: Clamp polygon depth values to 0.f - 1.f
According to the OpenGL spec, we're expected to clamp the fragment
depth values to the range `0.f - 1.f` for all polygons.

This fixes Z-fighting issues with the sky in Quake 3.
2022-05-11 20:25:17 +02:00
Jelle Raaijmakers
b8c0ebccfd LibSoftGPU: Implement depth offset factor
This implements the depth offset factor that you can set through e.g.
OpenGL's `glPolygonOffset`. Without it, triangles might start to Z-
fight amongst each other.

This fixes the floor decals in Quake 3.
2022-05-10 19:36:41 +02:00
Jelle Raaijmakers
a20bf80b05 LibGL+LibGPU+LibSoftGPU: Implement point and line drawing
Implement (anti)aliased point drawing and anti-aliased line drawing.
Supported through LibGL's `GL_POINTS`, `GL_LINES`, `GL_LINE_LOOP` and
`GL_LINE_STRIP`.

In order to support this, `LibSoftGPU`s rasterization logic was
reworked. Now, any primitive can be drawn by invoking `rasterize()`
which takes care of the quad loop and fragment testing logic. Three
callbacks need to be passed:

* `set_coverage_mask`: the primitive needs to provide initial coverage
   mask information so fragments can be discarded early.
* `set_quad_depth`: fragments survived stencil testing, so depth values
  need to be set so depth testing can take place.
* `set_quad_attributes`: fragments survived depth testing, so fragment
  shading is going to take place. All attributes like color, tex coords
  and fog depth need to be set so alpha testing and eventually,
  fragment rasterization can take place.

As of this commit, there are four instantiations of this function:

* Triangle rasterization
* Points - aliased
* Points - anti-aliased
* Lines - anti-aliased

In order to standardize vertex processing for all primitive types,
things like vertex transformation, lighting and tex coord generation
are now taking place before clipping.
2022-05-09 21:49:48 +02:00
Jelle Raaijmakers
950ded7ab9 LibSoftGPU: Mention correct i686 target in Device comment 2022-05-09 21:49:48 +02:00
Jelle Raaijmakers
7906ada015 LibSoftGPU: Update coverage mask bits during rasterization
We were not updating `coverage_bits` causing us to perform just a
little bit more work than strictly necessary.
2022-05-09 21:49:48 +02:00
Hendiadyoin1
d866637074 LibSoftGPU: Use round_to<int> in Device::get_rasterization_rect_of_size 2022-05-07 20:25:39 +02:00
Jelle Raaijmakers
a699e0a9d7 LibSoftGPU: Remove initial fog factor value from Device
This value was always overwritten. No functional changes.
2022-05-05 20:50:46 +02:00
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