SkillAgentSearch skills...

Tinygl

The penultimate portable graphics library

Install / Use

/learn @C-Chads/Tinygl
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

TinyGL

tgl logo A major overhaul of Fabrice Bellard's TinyGL to be more useful as a software rasterizer.

Now with limited multithreading support

Tightly tweaked and tuned for performance

The library has been tightly tuned using valgrind, perf stat, and other tools.

On a single thread on an i7-6700 (Skylake, 2015), the standard "gears" demo runs at a higher framerate than glxgears on Mesa using a Ryzen 3900x (2019) (NOTE: TinyGL Compared without SDL overhead)

I think I can safely say, this is the fastest single-threaded FOSS software GL implementation in existence.

It's probably also the most portable

Safety features

TinyGL contains the following safety features:

  1. compiletime options for glGetError() functionality which obviously slows down speed but increases debugging capability.

  2. OpenGL 2.0 buffers, for easy memory management (Anything you put in a buffer using glBufferData will be free'd upon glClose())

  3. Fully leak checked using Valgrind- The only leaks you'll see are from your system's SDL. the Raw demos have zero leaks.

Incredibly portable

TinyGL is written in pure C99, and requires very few functions from the C standard library, it doesn't even require malloc and free (The calls are aliased to gl_malloc() and gl_free(), which you can replace with your own memory management model if you desire)

How portable?

  • TinyGL still compiles under bellard's Tiny C Compiler.

The SDL examples are now confirmed building with TCC, using TinyCC built from the latest source.

  • TinyGL has been compiled for the Nintendo 3DS (proof of concept in another repository)

You can test compiling TinyGL and running it on platforms without SDL by running the RAW DEMOS, which do not require ANYTHING Except the C standard library and stdio. You may have to change the destination written to by the raw demos on platforms without standard file systems.

These are the C standard library includes used in the RAW DEMOS.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

If your system supports it, the library can also take advantage of alignas to get improved SIMD support, which can be disabled in zfeatures. This adds a dependency to stdalign.h but greatly increases vertex processing speed.

(This is disabled by default for maximum portability)

If you are unsure if your target platform can support TinyGL, compile it with the buildtime and runtime tests enabled (They are, by default)

if you get a TGL_BUILDT error, then you've failed the buildtime test.

if you try to initialize the library and you get a crash with a print to standard out "TINYGL_FAILED_RUNTIME_COMPAT_TEST" then you've failed the runtime test.

The SDL examples have been tested building on Debian 10 and Windows 10, while tinygl itself has been confirmed to compile on many more platforms.

Includes a small SIMD-accelerated public domain replacement for GLU

the "Chad Math Library" has been included (CC0, public domain) for your programming needs.

it is simd accelerated on supported platforms, but it contains ZERO platform-specific code- it relies on an optimizing compiler to get vectorized ops. SIMD acceleration is improved by enabling alignas.

NOTE: There are graphical artifacts visible in these gifs which have been corrected in this version of the library.

Without Polygon Stipple:

GIF Video of demo

With Polygon Stipple:

GIF Video of demo

Hello World test:

model loading demo

Texturing Test:

Screenshot of Texture test

Model loading tests:

model loading demo

model loading demo

Without lighting:

model loading demo

model loading demo

This is a demo of the NO_DRAW_COLOR feature. Notice that the object appears to have a hole in it.

model loading demo

Blending:

model loading demo

Specular:

GIF Video of demo

OpenIMGUI standard demo:

OpenIMGUI

TinyGL 0.8 (c) 1997-2021 Fabrice Bellard, C-Chads, Gek (see License, it's free software)

This is a maintained fork of TinyGL, by the C-Chads. It is a small, suckless Software-only partial GL 1.1 implementation.

The original project was by Fabrice Bellard. We have forked it.

The changelog is as such:

  • Disabled 8, 15, and 24 bit rendering modes. 16 and 32 are the only supported rendering modes (Coincidentally, they are also the fastest)

  • Allowed the fixed texture size to be changed at compile time. It must be a power of 2, but that is the only limitation.

  • Removed the entire GLX/NanoGLX part of the library. Not portable and mostly useless.

  • Implemented new functions and some more of GL 1.1's prototypes including polygon stipple.

  • Triangles can now be lit and textured at the same time!

  • Removed unused functions which bloat binary size and lengthen compile times.

  • Added support for glDepthMask and glDisable(GL_DEPTH_TEST) as per-GL-spec

  • ADDED BLENDING SUPPORT!

  • Added glDrawPixels

  • Added glPixelZoom

  • Added glRasterPos2f,3f,4f,2fv,3fv,4fv

  • Added glGetString() for GL_VENDOR, GL_RENDERER, GL_VERSION, and optionally GL_LICENSE

  • Added comprehensive, usable glGetError() functionality for debugging.

  • Fixed a myriad of bugs and... weirdnesses

  • Fixed clientside arrays

  • Tuned the triangle rasterizer to near-perfection.

  • Tuned the transformations to absolute perfection

  • Added glDrawArrays

  • Added Buffers (For memory management purposes)

  • Added glTexImage1D (... it just resizes it to 2D, but it works!)

  • Added glPixelSize (TODO is to implement distance scaling)

  • Fixed specular rendering

  • Added way more compile time options

  • Fixed all the memory leaks.

  • added Openmp multithreading and glPostProcess()

  • Line rendering now obeys glDepthMask and glDepthTest.

  • Implemented glRectf

  • Implemented compiletime toggles for GL_SELECT and GL_FEEDBACK which significantly boosts performance. Also, implemented GL_FEEDBACK.

  • Accept PR from RobLoach to add Cmake support

Note that this Softrast is not GL 1.1 compliant and does not constitute a complete GL implementation.

You will have to tweak your code to work with this library. That said, once you have, it will run anywhere that you can get C99. TinyGL has very few external dependencies.

Notable limitations:

  • The only supported texture size and format is decided at compile time. you can set it in zfeatures.h

  • A lot of prototypes are missing.

  • glPolygonOffset doesn't change anything about how rendering occurs. It does nothing, at the moment. The "implementation specific multiplier" is 0.

  • There is no stencil buffer.

  • Blending can't use alpha values. the rasterizer has no concept of alpha.

  • There is no mipmapping, antialiasing, or any form of texture filtering.

  • No edge clamping. S and T are wrapped.

  • Display lists can be infinitely nested and doing so will crash TinyGL.

  • Lit triangles will use the current material properties, even if they are textured. If the diffuse color is black, then your textured triangles will appear black.

  • Textured triangles are affected by their vertex colors- the per-vertex color is used as a "mask" for the texture on triangles. It is recommended you call glColor3f(1,1,1); before rendering a textured object to get the expected result- you only need to make this call once, and it can be before glBegin.

  • the X dimension of the rendering window with must be a multiple of 4.

  • Line rendering is not blended

  • The ARB extension for point sprite size attenuation is not enabled.

  • Point smoothing is not implemented, points are always squares of a solid color.

  • glCopyTexImage2D only works with the size of texture you decided at compile time.

  • <Undocumented limitations that have not been tested>

HOW DO I USE THIS LIBRARY???

TinyGL is not header only, it is a combination of C files, internal headers, and external headers.

The internal headers are only used while compiling the library,

the external headers (gl.h, zfeatures.h, zbuffer.h) are required to use the library.

You CAN compile the library along with your final program into a single compilation unit without separating out the library.

Doing so is the most likely compiling method for embedded platforms and how I got TinyGL running on the 3DS.

The codebase uses very simple compiler flags to compile- in fact it will compile if you just compile all c files together

in the src/ directory.

You can compile the code yourself without makefiles using these directives:

# inside the src directory
gcc -O3 -c *.c 
ar rcs libTinyGL.a *.o
# the library is now compiled
cp libTinyGL.a ../lib
cd ..
cd SDL_Examples/
# build the menu demo
gcc -O3 menu.c -o menu -lSDL ../lib/libTinyGL.a -lm
# gears
gcc -O3 gears.c -o gears -lSDL ../lib/libTinyGL.a -lm

This is how you use TinyGL in a program:

//First you have to include
//(Note that you must either link against libTinyGL.a or compile it in the same compilation unit as your program)
#include "../include/GL/gl.h"
#include "../include/zbuffer.h"

/*
	Somewhere in your program...
*/

//Next, open a framebuffer.
//The "0" parameter is where you pass in a framebuffer pointer if you've already made one.
ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);

//Tell TinyGL to initialize on that framebuffer
glInit(frameBuffer);

//Begin making TinyGL calls!

//When you want to copy to your target screen
//Pitch is the width of the target in bytes, or bytes per pixel times width;
ZB_copyFrameBuffer(frameBuffer, screen->pixels, screen->pitch);




//At the end of your application, when you want to clean up.
ZB_close(frameBuffer);
glClose();

Note that while you... can invoke ZB_Resize to resize the framebuffer, you really shouldn't. It isn't tested.

WHAT ARE THE MINIMUM REQUIREMENTS OF THIS LIBRARY?

View on GitHub
GitHub Stars551
CategoryDevelopment
Updated23d ago
Forks64

Languages

C

Security Score

85/100

Audited on Mar 5, 2026

No findings