psx.wiki

Extended Graphics Library (libgs)

Provides functions for a 3D graphics system built on libgpu and libgte.

Extended Graphics Library (libgs)
Librarylibgs.lib
Header Filelibgs.h
SDKPsy-Q

This is an old revision of this page, as edited by RemmyLee at 03:05, 23 October 2026. Edit this revision | Latest revision

Overview

The Extended Graphics Library (libgs) integrates the 2D and 3D graphics systems of libgpu and libgte. It is designed for the standard graphics formats made by the PlayStation authoring tools. TIM stores images with their resolution, colors and color lookup tables, TMD stores objects with scale and texture address information, and HMD was added in library version 4.0.1

libgpu and libgte process data polygon by polygon, while libgs processes objects, which are groups of polygons. 3D programs are therefore easy to prototype, and attributes added to objects create special effects. libgs has more overhead than libgpu and libgte. However, its architecture is open, so a finished program can replace parts of it with user functions through a jump table.1

A program that uses libgs links with libgs.lib and includes libgs.h.1

This library is part of the run-time library that Sony distributed with the Psy-Q development tools.2

Features

The main features of libgs are the following:13

Table 1: libgs features
Feature Contents
Hierarchical coordinate systems A coordinate system may be the parent or child of another, and changes to a parent apply to its children
Light source calculation 3 light sources, depth cueing and ambient light from user parameters
Automatic division of polygons Large polygons are divided to avoid clipping problems
Semi-transparent processing Objects and textures drawn semi-transparent by attribute
Perspective texture mapping Texture mapping of objects with perspective
Viewpoint control View structures set the viewing angle
Z-sort processing The GsOT structure and the GsSort functions sort objects by Z
OT compression Objects with very different Z values sort into separate OTs that are joined before drawing
Frame double buffer Initialization and switching of two display buffers
Aspect ratio adjustment Objects keep a normal aspect ratio in modes without square dots
2D clipping and offset Clipping and an offset added to all screen coordinates
Sprite, BG and line Structures and routines for sprites, lines and cell-based scrolling backgrounds

Coordinate Systems

GsCOORDINATE2 describes a coordinate system of libgs. The systems form a hierarchy with the world coordinate system at the top, and each is integrated from the lower level to the higher. The matrix of a GsCOORDINATE2 describes the coordinate system relative to its parent. X, Y and Z are each 32 bits, and a work area speeds up the calculation. GsInitCoordinate2() initializes the structure, and its members may also be set directly.3

Rotation is executed first and translation second. When RotMatrix() creates the rotation matrix, the order of rotation is Z first and X last.3

The flg member set to 1 marks a system whose local-to-world matrix is already in workm, so it is not calculated again. A program that rewrites a GsCOORDINATE2 therefore sets flg to zero. A change to a parent applies to all its children automatically, and their flags need not be cleared.4

Objects

Objects are handled by 3D object handlers. The basic handler is GsDOBJ2, and the others are GsDOBJ3 and GsDOBJ5. The coord2 member of GsDOBJ2 points to the coordinate system of the object, so the object moves when that GsCOORDINATE2 changes. GsLinkObject4() sets the tmd member of a GsDOBJ2 to the TMD object read in.45

When the coord2 of object 2 is made a child of the GsCOORDINATE2 of object 1, moving object 1 also moves object 2, while moving object 2 does not affect object 1. The bits of the attribute member control material attenuation, the lighting mode, near clipping, back clipping and automatic division, together with display or non-display.45

Viewing System

The screen shows a projection of 3D space onto a window at a given distance in front of the viewpoint. GsSetRefView2() sets the viewpoint from a GsRVIEW2, which gives the viewpoint and a reference point, and GsSetView2() sets it from a GsVIEW2, which gives the transformation matrix directly. With the world as the parent the view is an objective camera, and with the coordinate system of an object it is a subjective camera for that object.5

GsSetProjection() sets the projection (h), which is the distance between the viewpoint and the screen. The screen has the size of the display resolution, so a 640 x 480 display gives a 640 x 480 screen. When the display does not have square dots, the vertical is adjusted. In 640 x 240 mode an object is displayed at 1/2 height. GsInit3D() initializes the 3D service and puts the origin at the center of the screen. A large projection gives a narrow image angle close to parallel projection, and a small projection a wide angle with strong perspective.56

Light Sources

libgs has up to 3 parallel light sources. With a parallel light the brightness of a polygon depends only on the light and the angle of the polygon, and the light is set by its direction and color in a GsF_LIGHT structure with GsSetFlatLight().6

Ambient light shows the shape of an object where no light strikes it. GsSetAmbient(ONE/2, ONE/2, ONE/2) makes unlit parts half as bright as lit parts, where ONE is the fixed-point 1. Higher ambient values make the image warmer and lower values cooler.6

Depth cueing (fog) dims distant objects. It works on all non-textured polygons, but on texture-mapped polygons only when the color is black. GsSetLightMode(1) or GsSetLightMode(3) turns it on, and GsSetFogParam() sets the background color with a GsFOGPARAM structure. A whitish color gives fog and black gives a night effect.67

Material lighting makes an object look metallic with a steeper light attenuation curve. It is selected with GsSetLightMode(2) or GsSetLightMode(3) and the attenuation bits of the attribute member, but the manual states that this is not possible in the current version.7

Ordering Tables

The PlayStation removes hidden surfaces by Z sorting, which the ordering table (OT) speeds up. An OT is a kind of Z ruler in memory, and each graduation may hold any number of polygons. Each polygon hangs at the graduation of its Z value, and the polygons at the far end of the OT, with the largest Z, are drawn first.7

The GsOT structure handles an OT in libgs. Its org member points to the actual OT, its length member gives the number of Z graduations, and each graduation is a GsOT_TAG. GsClearOt() initializes the OT before every sort, since only an initialized OT can be sorted again, and its arguments are offset, point and the OT handler.7

GsSortOt() sorts one OT into another. The point member gives the representative Z of the OT. Local OTs for each object sorted into one OT sort the scene by objects, which helps when the order is already known. Two OTs drawn to the two halves of the drawing area with different viewpoints give a split screen.78

An OT uses much memory, and a lower resolution saves memory but causes flicker. When the Z values of all polygons are above a known value, that value can be passed to GsClearOt() as offset, and the OT does not store the part below it.8

Z sorting can draw polygons in the wrong order and make them flicker, most often with a deep polygon, which is sorted by one average Z. Dividing the polygons avoids this at the cost of more polygons. Sorting by object units avoids it when the order of the objects is clear.8

Double Buffers

An OT cannot be changed while it is drawn, so two OTs are prepared and the program sorts into the one not being drawn. The display has a double buffer in the same way. Switching outside the vertical blank disturbs the screen. The screen being displayed and the screen being drawn are therefore separate. GsDefDispBuff() defines the two buffers, GsSwapDispBuff() switches them and GsGetActiveBuff() returns the buffer being drawn.8

Double buffering moves the display area in the frame buffer, so drawing needs an offset. The third argument of GsInitGraph() adds the offset either in libgte, when the packets are calculated, or in libgpu, when the frame buffer is drawn. The manual recommends the libgpu method with libgpu functions. In interlace mode with 480 lines the even and odd lines double the buffer. Both arguments of GsDefDispBuff() then name the same buffer.9

Clipping

libgs clips in two ways. Two-dimensional clipping clips in screen coordinates to any rectangle of the frame buffer. GsSetClip2D() sets the area in an internal variable and GsSetDrawBuffClip() makes it effective, and the area is switched with the double buffer, so that an oversized polygon does not damage the other buffer. Three-dimensional clipping clips by the distance from the viewpoint, and libgs has the following defaults:9

Table 2: Three-dimensional clipping
Clip Condition
Far clip Screen Z greater than 65536, because Z is an unsigned 16-bit value
Near clip Screen Z less than h/2, where h is the projection

Polygons close to the viewpoint, such as the road of a racing game, become very large, and clipping them by polygon leaves holes. Automatic division clips them smoothly at the near clip plane, but it is a heavy load and is used only when needed.10

Packet Buffers

libgs has three packet creation functions, GsSortObject3(), GsSortObject4() and GsSortObject5(), and two packet buffers. The preset packet buffer holds packets made ahead of time for GsSortObject5() and GsDOBJ5, and its size is fixed by the model. GsPresetObject() fills it once, and its return value shows how far the buffer is used. Preset packets do not use the run-time buffer, except when automatic division is on.10

The run-time packet buffer holds the packets that GsSortObject4() and functions such as GsSortSprite() make during execution. GsSetWorkBase() sets its start and GsGetWorkBase() returns the current pointer. The amount used per frame depends on the number of polygons.10

Preset packets need not be rewritten every frame, which saves work such as writing the U and V values. The PMD format holds both the modeling data and the preset packets. A packet group begins with header 0, whose upper 8 bits give the packet type and lower 24 bits point to the next group. Header 1 follows with the number of packets, each packet is doubled for the double buffer, and a terminator with a zero address ends the list.1011

TMD Sort

TMD data may mix polygon types at random. When the packets are made at run time the decode routines then swap out of the I-cache, and the processor cannot keep up. The TMD sort orders the polygons by type at authoring time with TMDSORT.EXE, and GsSortObject4() and GsSortObject5() are faster with sorted data.11

Packet Creating Functions

GsSortObject3() creates packets from PMD data with the GsDOBJ3 handler. GsLinkObject3() first joins the data to the handler, and TMD2PMD.EXE converts TMD to PMD. GsSortObject4() is the most general function and uses sorted TMD data with the GsDOBJ2 handler. It converts the object with the local screen matrix from GsSetLsMatrix() and the light matrix from GsSetLightMatrix(), sorts it and allocates it to the OT. GsSortObject5() uses preset packets with GsDOBJ5 and sorted TMD data.1112

The functions support the following attributes, where the efficiency is the measured value for 10 x 10 flat triangles:12

Table 3: Packet creation functions
Function Handler Material attenuation Fog Light off Near clip Back clip Semi-transparency Division Efficiency
GsSortObject3 GsDOBJ3 No No No No No No No 250K
GsSortObject4 GsDOBJ2 No Yes Yes No No Yes Yes
GsSortObject5 GsDOBJ5 No Yes Yes No No No Yes 220K

GsSortObject4() is more efficient than GsSortObject3() and less efficient than GsSortObject5().12

Packet Area

The size of the packets depends on the type and number of polygons, so only a rough estimate of the needed area can be made. A packet area that is too small overwrites the memory behind it. GsGetWorkBase() returns the area in use, from which a program can judge the danger of overflow. With automatic division a packet area from GsSetWorkBase() is needed, also with GsSortObject5(), because divided packets grow.1213

Drawing runs in the background, and the packets being drawn must not be destroyed. Two packet areas are therefore prepared as a double buffer.13

Drawing

GsDrawOt() starts drawing, which runs in the background. The drawing area changes each time GsSwapDispBuff() is called, and enough time must be left for the drawing to finish. GsSortClear() registers a screen clear primitive in the OT with an RGB color, since the drawing area still holds the image of two frames before. A frame is processed in the following order:13

  1. Select the buffer to use.
  2. Clear the OT.
  3. Perspective transformation and Z sort.
  4. Wait for the vertical blank.
  5. Start drawing the OT.

Jump Tables

GsSortObject4() and GsSortObject5() dispatch the attributes and preset data to 64 low-level functions, and few applications use all of them. GsSortObject4J() and GsSortObject5J() take the low-level functions from a table in a GsFCALL structure, organized by polygon type, division and light source mode. Only the functions named in the table become part of the program, which makes the code smaller. A user function in the table can also replace a library function and add effects such as material attenuation or reflection mapping.14

A call to an entry without a pointer is a bus error. libgte therefore provides dummy (dmy) functions, which print their own name when an unexpected type is called.14

Scratch Pad

GsSortObject4(), GsSortObject5() and their J versions take a scratch pad address. The scratch pad is a feature of the CPU that gives fast access to up to 1 KB of data, and libgs uses it in automatic division (GsDIV1 to GsDIV5) to improve speed. Without automatic division it is not used. The use in bytes is the following:15

Table 4: Scratch pad usage
Polygon GsDIV1 GsDIV2 GsDIV3 GsDIV4 GsDIV5
Triangle 184 272 360 448 536
Quadrilateral 260 400 540 680 820

The library extends its area downward from the given base address, so the user program can share the scratch pad by using the area above it.15

Mip-Map

libgs supports mip-mapping, which switches the texture of a textured quadrilateral by the size of the polygon. The texture cache then hits more often and drawing takes less time. The low-level functions of GsSortObject4J() whose names end in M, in the GsTMDfast, GsTMDdiv and GsA4div groups, support it.151617

The textures come in four sizes, 1, 1/4, 1/16 and 1/64, and all four lie in the same texture page. The size to use is found from the outer product of the polygon, and the vertices of the polygon are ordered 0 and 1 across the top and 3 and 2 across the bottom.1718

Structures

In the tables below, Introduced is the run-time library version that added the item.19

libgs has the following structures:19

Table 5: libgs structures
Structure Introduced Purpose
GsBG 2.x BG (background surface) handler.
GsBOXF 2.x Rectangle handler.
GsCELL 2.x Cells constituting BG.
GsCOORD2PARAM 4.0 GsCOORDINATE2 parameter format.
GsCOORDINATE2 2.x Matrix type coordinate system.
GsDOBJ2 3.0 Three-dimensional object handler.
GsDOBJ3 3.0 Three-dimensional object handler for use with PMD format.
GsDOBJ5 3.0 Three-dimensional object handler for use with GsSortObject5().
GsFOGPARAM 2.x Fog (depth cueing) information.
GsF_LIGHT 2.x Parallel light source.
GsGLINE 3.5 Straight line handler with gradation.
GsIMAGE 2.x Information on image data composition.
GsLINE 2.x Straight line handler.
GsMAP 2.x Map comprising BG.
GsOBJTABLE2 2.x Object table information.
GsOT 2.x Ordering table header.
GsOT_TAG 2.x Ordering table unit.
GsRVIEW2 2.x View handler (Reference type).
GsSPRITE 2.x Sprite handler.
GsVIEW2 2.x View handler (matrix type).
TMD_STRUCT 4.0 TMD data object header.
_GsFCALL 3.2 The function table for GsSortObject4J() and GsSortObject5J().
_GsPOSITION 4.0 Two-dimensional offset.

Functions

libgs has the following functions:20

Table 6: libgs functions
Function Introduced Purpose
dmyGsPrst... 3.2 Jump Table dummy function group (PMD).
dmyGsTMD... 3.2 Jump Table dummy function group (TMD).
GsA4div... 4.1 Low-level functions for GsSortObject4J() (performs automatic division).
GsClearOt 2.x Initialize a libgs ordering table structure.
GsClearVcount 3.2 Clear vertical retrace counter.
GsCutOt 2.x OT separation.
GsDefDispBuff 2.x Define double buffers.
GsDefDispBuff2 3.0 Define double buffers.
GsDrawOt 2.x Process GPU commands registered to OT.
GsDrawOtIO 3.5 Process GPU commands (I/O version) allocated to OT.
GsGetActiveBuffer 2.x Get a buffer number during drawing.
GsGetLs 2.x Calculate a local screen matrix.
GsGetLw 2.x Calculate a local world matrix.
GsGetLws 3.0 Calculate local world and local screen matrices.
GsGetTimInfo 2.x Find TIM format header.
GsGetVcount 2.x Get the value of the vertical retrace counter.
GsGetWorkBase 2.x Get address for storing current drawing commands.
GsInit3D 2.x Initialize the graphics system.
GsInitCoordinate2 2.x Initialize a local coordinate system.
GsInitFixBg16, GsInitFixBg32 3.0 Initialize BG work area (high-speed).
GsInitGraph 2.x Initialize the graphics system.
GsInitGraph2 3.0 Initialize the graphics system.
GsInitVcount 2.x Initialize vertical retrace counter.
GsLinkObject3 3.0 Link an object with PMD data; for GsSortObject3().
GsLinkObject4 3.0 Link an object to TMD data; for GsSortObject4().
GsLinkObject5 3.0 Link an object to TMD data; or GsSortObject5().
GsMapModelingData 2.x Map TMD data to real addresses.
GsMulCoord0 3.0 MATRIX multiplication.
GsMulCoord2 2.x MATRIX multiplication.
GsMulCoord3 2.x MATRIX multiplication.
GsPresetObject 3.0 Create a preset packet for a GsDOBJ5-type object.
GsPrst... 4.1 Low-level functions for GsSortObject5J().
GsScaleScreen 3.2 Scale the screen coordinate system.
GsSetAmbient 2.x Set color and brightness of ambient lighting.
GsSetClip 3.0 Set drawing clipping area.
GsSetClip2 3.0 Set a drawing clipping area.
GsSetClip2D 2.x Set two-dimensional clipping.
GsSetDrawBuffClip 2.x Set drawing clipping area.
GsSetDrawBuffOffset 2.x Set the drawing offset.
GsSetFlatLight 2.x Set a parallel light source.
GsSetFogParam 2.x Set the fog parameter.
GsSetLightMatrix 2.x Set a light matrix.
GsSetLightMatrix2 3.0 Set a light matrix.
GsSetLightMode 2.x Set light source mode.
GsSetLsMatrix 2.x Set a local screen matrix.
GsSetOffset 3.0 Set an offset.
GsSetOrign 3.2 Set offset that is valid if the screen is switched.
GsSetProjection 2.x Set the projection plane position.
GsSetRefView2 2.x Set world-to-screen matrix.
GsSetRefView2L 3.5 Set viewpoint (high-precision version).
GsSetView2 2.x Set viewpoint.
GsSetWorkBase 2.x Set address for storing drawing commands.
GsSortBg, GsSortFastBg 3.0 Register BG in the OT.
GsSortBoxFill 2.x Register a rectangle in the OT.
GsSortClear 2.x Register a screen clear command in the OT.
GsSortFixBg16, GsSortFixBg32 3.0 Register BG in the OT (high-speed).
GsSortGLine, GsSortLine 3.0 Register a straight line in the OT.
GsSortObject3 3.0 Register an object to the ordering table (for use with GsDOBJ3).
GsSortObject4 3.0 Register an object to the ordering table (for use with GsDOBJ2).
GsSortObject4J 3.2 Register an object to ordering table (jump table version).
GsSortObject5 3.0 Register an object to the ordering table (for use with GsDOBJ5).
GsSortObject5J 3.2 Register an object to the ordering table (jump table version).
GsSortOt 3.3 Insert an OT into another OT.
GsSortPoly 2.x Register a polygon drawing primitive in the OT.
GsSortSprite, GsSortFastSprite, GsSortFlipSprite 2.x Register a Sprite in the OT.
GsSwapDispBuffer 2.x Swaps double buffers.
GsTMDdiv... 4.1 Low-level functions for GsSortObject4J() (performs fixed division).

Macros

libgs has the following macros:21

Table 7: libgs macros
Macro Introduced Purpose
GsClearDispArea 3.0 Clears screen.
GsIncFrame 3.2 Updates the frame ID.
GsSetAzwh 3.3 Sets conditions for active subdivision.

References


  1. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-3.

  2. ^ Sony Computer Entertainment Inc. Programmer Tools CD (DTL-S2002), README.TXT. 11/26/96. Section "CD-ROM Contents - Highlights", directories \PSX and \PSYQ.

  3. ^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-4.

  4. ^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-5.

  5. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-6.

  6. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-7.

  7. ^ a b c d e Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-8.

  8. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-9.

  9. ^ a b Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-10.

  10. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-11.

  11. ^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-12.

  12. ^ a b c d Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-13.

  13. ^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-14.

  14. ^ a b Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-15.

  15. ^ a b c Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-16.

  16. ^ Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-17.

  17. ^ a b Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-18.

  18. ^ Sony Computer Entertainment Inc. Run-Time Library Overview. Run-Time Library 4.4. January 1999. Page 10-19.

  19. ^ a b Sony Computer Entertainment Inc. Run-Time Library Reference. Run-Time Library 4.4. January 1999. Pages 9-3 to 9-30.

  20. ^ Sony Computer Entertainment Inc. Run-Time Library Reference. Run-Time Library 4.4. January 1999. Pages 9-31 to 9-101.

  21. ^ Sony Computer Entertainment Inc. Run-Time Library Reference. Run-Time Library 4.4. January 1999. Pages 9-110 to 9-112.

Categories: Psy-Q