psx.wiki

GP0 Environment Commands: Difference between revisions

Revision as of 02:32, 23 October 2026
RemmyLee
(CLAUDE.md, CONTEXT.md: the gate skips doc-slop's length and lede rules)
Revision as of 02:43, 23 October 2026
RemmyLee
(GPU at register depth: GP0, GP1, GPUSTAT, VRAM, rendering, T-cache, video timing)
Line 1:Line 1:
+---
+title: GP0 Environment Commands
+type: register
+summary: GP0(E1h) to GP0(E6h), which set the drawing environment of the GPU.
+category: Hardware
+category: GPU
+---
+
+## Summary
+
+The environment commands GP0(E1h) to GP0(E6h) set the state that all later drawing commands of the [GPU](/hardware/gpu/) use: the texture page, the texture window, the drawing area, the drawing offset and the mask bit. Each is a single word whose top 8 bits are the command number.[^spx-sum] They form command group 7 in the command group table of [GP0 Drawing Commands](/hardware/gpu/gp0-drawing/).
+
+Libgpu keeps the same state in the DRAWENV structure, and PutDrawEnv() makes a DRAWENV the current drawing environment. Its members are as follows:[^lo85][^lo86]
+
+Table: DRAWENV members
+
+| Member | Content |
+| --- | --- |
+| clip | Drawing (clipping) area |
+| ofs[2] | Drawing offset |
+| tw | Texture window |
+| tpage | Texture page |
+| dtd | Dither flag (0 = off, 1 = on) |
+| dfe | Display area drawing flag |
+| isbg | Enable to auto-clear the drawing area |
+| r0, g0, b0 | Initial background color |
+
+ Within an ordering table, the special primitives DR_ENV, DR_MODE, DR_TWIN, DR_AREA and DR_OFFSET change all or part of the environment in the middle of drawing.[^lo810]
+
+Table: Environment commands
+
+| Command | Purpose | DRAWENV members | Special primitive |
+| --- | --- | --- | --- |
+| GP0(E1h) | Draw mode (texture page) | tpage, dtd, dfe | DR_MODE |
+| GP0(E2h) | Texture window | tw | DR_TWIN |
+| GP0(E3h) | Drawing area top left | clip | DR_AREA |
+| GP0(E4h) | Drawing area bottom right | clip | DR_AREA |
+| GP0(E5h) | Drawing offset | ofs | DR_OFFSET |
+| GP0(E6h) | Mask bit setting | | |
+
+A change made with PutDrawEnv() while drawing is in progress has no effect on the primitive being executed or on the rest of the current primitive list. The new environment takes effect from the next drawing operation.[^lo87]
+
+## GP0(E1h) - Draw Mode setting
+
+GP0(E1h) sets the texture page, the texture color mode, the semi-transparency rate and the dither and display flags. Its fields are as follows:[^spx-e1]
+
+Table: GP0(E1h) fields
+
+| Bits | Name | Content | GPUSTAT bits |
+| --- | --- | --- | --- |
+| 0 to 3 | TBX | Texture page X base, in units of 64 halfwords | 0 to 3 |
+| 4 | TBY | Texture page Y base, in units of 256 lines | 4 |
+| 5 to 6 | ABR | Semi-transparency (0 = B/2+F/2, 1 = B+F, 2 = B-F, 3 = B+F/4) | 5 to 6 |
+| 7 to 8 | TPF | Texture page colors (0 = 4-bit, 1 = 8-bit, 2 = 15-bit, 3 = reserved) | 7 to 8 |
+| 9 | DTD | Dither 24-bit to 15-bit (0 = off, 1 = on) | 9 |
+| 10 | DFE | Drawing to the display area (0 = prohibited, 1 = allowed) | 10 |
+| 11 | TBY2 | Texture page Y base bit 2, in units of 512 lines (2 MB VRAM only) | 15 |
+| 12 | | Textured rectangle X-flip (v2 GPU only) | |
+| 13 | | Textured rectangle Y-flip (v2 GPU only) | |
+| 14 to 23 | | Not used, should be 0 | |
+| 24 to 31 | | Command (E1h) | |
+
+In ABR, B is the pixel already in the frame buffer and F is the new pixel. Texture color setting 3 behaves the same as setting 2.[^spx-e1]
+
+A textured polygon carries its own texture page attribute, which overwrites bits 0 to 8 and bit 11 of this setting. GP0(E1h) is therefore needed for lines, rectangles and untextured polygons, and to change the dither and display flags (bits 9 and 10), which a polygon attribute cannot change.[^spx-e1]
+
+Sony calculates a texture page number from the upper left coordinates (tx, ty) of the page as tpage = ty/16 + tx/64, and notes that the tpage value also holds the CLUT mode and the semi-transparency rate.[^hw36] The tpage member of DRAWENV gives the texture page used by default, and the DR_MODE primitive changes it during drawing. Because a sprite has no texture page of its own, the libgpu example registers a DR_MODE in the same ordering table entry as the sprite, where it is executed first.[^lo823]
+
+Bits 4 and 11 together form a 2-bit texture page Y coordinate. A retail console has 1 MB of VRAM and uses bit 4 only. On a v2 GPU with the 2 MB mode enabled by GP1(09h), setting bit 11 selects a VRAM half that is not fitted, and textures disappear. A v0 GPU ignores bit 11.[^spx-e1]
+
+## GP0(E2h) - Texture Window setting
+
+GP0(E2h) limits texture coordinates to a window inside the texture page and repeats the window across the page. Its fields are in units of 8 pixels:[^spx-e2]
+
+Table: GP0(E2h) fields
+
+| Bits | Content |
+| --- | --- |
+| 0 to 4 | Texture window mask X, in 8-pixel steps |
+| 5 to 9 | Texture window mask Y, in 8-pixel steps |
+| 10 to 14 | Texture window offset X, in 8-pixel steps |
+| 15 to 19 | Texture window offset Y, in 8-pixel steps |
+| 20 to 23 | Not used, 0 |
+| 24 to 31 | Command (E2h) |
+
+The mask selects the texture coordinate bits to replace, and the offset gives their new values. Each coordinate is therefore adjusted as Texcoord = (Texcoord AND (NOT (Mask x 8))) OR ((Offset AND Mask) x 8). The GPU reads the window contents as if they were repeated throughout the texture page, although the data is stored only once.[^spx-e2]
+
+In libgpu the texture window is the tw rectangle of DRAWENV, which runs from (tw.x, tw.y) to (tw.x + tw.w, tw.y + tw.h). It is normally set to (0, 0) to (255, 255), which does not repeat the texture. When a smaller window is used to repeat a texture, the (u, v) values of the primitive should lie inside the window.[^lo86][^lo825]
+
+## GP0(E3h) and GP0(E4h) - Drawing Area setting
+
+GP0(E3h) and GP0(E4h) set the corners of the drawing area. The drawing commands GP0(20h) to GP0(7Fh) clip every pixel outside this rectangle. Both commands have the following fields:[^spx-e3]
+
+Table: GP0(E3h) and GP0(E4h) fields
+
+| Bits | Content |
+| --- | --- |
+| 0 to 9 | X coordinate, 0 to 1023 |
+| 10 to 18 | Y coordinate, 0 to 511 (v0 GPU) |
+| 19 to 23 | Not used, 0 (v0 GPU) |
+| 10 to 19 | Y coordinate, 0 to 1023 (v2 GPU) |
+| 20 to 23 | Not used, 0 (v2 GPU) |
+| 24 to 31 | Command (E3h or E4h) |
+
+In libgpu the drawing area is the clip member of DRAWENV, which is the rectangle from (clip.x, clip.y) to (clip.x + clip.w, clip.y + clip.h). Its size is not limited as long as it lies within the frame buffer.[^lo85][^lo86] If the drawing area overlaps the display area its contents appear on the screen, which is why a double buffer uses two separate areas.[^lo85]
+
+## GP0(E5h) - Drawing Offset setting
+
+GP0(E5h) sets the offset that the GPU adds to every vertex before drawing. Its fields are as follows:[^spx-e5]
+
+Table: GP0(E5h) fields
+
+| Bits | Content |
+| --- | --- |
+| 0 to 10 | X offset, -1024 to +1023 |
+| 11 to 21 | Y offset, -1024 to +1023 |
+| 22 to 23 | Not used, 0 |
+| 24 to 31 | Command (E5h) |
+
+When the GTE produces coordinates with (0, 0) at the center of the drawing area, the offset must be the center of the drawing area, X1 + (X2 - X1)/2 and Y1 + (Y2 - Y1)/2. When (0, 0) is the upper left corner, the offset is X1, Y1.[^spx-e5] In libgpu the offset is ofs[0] and ofs[1] of DRAWENV, which are added to the X and Y values of all primitives.[^lo86] For a double buffer at (0, 0) and (0, 240), buffer 0 uses the offset (0, 0) and buffer 1 the offset (0, 240).[^lo87]
+
+GP0(E3h), GP0(E4h) and GP0(E5h) do not take up space in the command FIFO, so they appear to take effect at once even while older commands wait in the FIFO. psx-spx therefore recommends sending them only when the FIFO is empty.[^spx-fifo]
+
+## GP0(E6h) - Mask Bit Setting
+
+GP0(E6h) controls the mask bit, which is bit 15 of each frame buffer pixel. Its fields are as follows:[^spx-e6]
+
+Table: GP0(E6h) fields
+
+| Bits | Name | Content | GPUSTAT bit |
+| --- | --- | --- | --- |
+| 0 | PBW | Set mask while drawing (0 = bit 15 of the texture, 1 = force bit 15 to 1) | 11 |
+| 1 | PBC | Check mask before drawing (0 = draw always, 1 = draw only where bit 15 = 0) | 12 |
+| 2 to 23 | | Not used, 0 | |
+| 24 to 31 | | Command (E6h) | |
+
+With bit 0 clear, bit 15 of each written pixel is bit 15 of the texture color, which is set for semi-transparent texels; an untextured shape writes 0. With bit 1 set, any pixel whose bit 15 is already 1 is write-protected against later drawing.[^spx-e6]
+
+The mask setting applies to all drawing commands and to the CPU-to-VRAM and VRAM-to-VRAM transfers, where it acts on each halfword. It does not apply to the fill command GP0(02h). psx-spx names Metal Gear Solid and Silent Hill as games that use it.[^spx-e6]
+
+## References
+
+[^spx-sum]: psx-spx. Graphics Processing Unit (GPU), GPU Command Summary. https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-io-ports-dma-channels-commands-vram
+[^spx-e1]: psx-spx. Graphics Processing Unit (GPU), GP0(E1h) - Draw Mode setting. https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^spx-e2]: psx-spx. Graphics Processing Unit (GPU), GP0(E2h) - Texture Window setting. https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^spx-e3]: psx-spx. Graphics Processing Unit (GPU), GP0(E3h) - Set Drawing Area top left (X1,Y1), GP0(E4h) - Set Drawing Area bottom right (X2,Y2). https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^spx-e5]: psx-spx. Graphics Processing Unit (GPU), GP0(E5h) - Set Drawing Offset (X,Y). https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^spx-e6]: psx-spx. Graphics Processing Unit (GPU), GP0(E6h) - Mask Bit Setting. https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^spx-fifo]: psx-spx. Graphics Processing Unit (GPU), GPU Rendering Attributes, Note. https://psx-spx.consoledev.net/graphicsprocessingunitgpu/#gpu-rendering-attributes
+[^hw36]: Sony Computer Entertainment Inc. PlayStation Hardware. August 1998. Page 3-6.
+[^lo85]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 8-5.
+[^lo86]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 8-6.
+[^lo87]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 8-7, Table 8-2.
+[^lo810]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 8-10, Table 8-6.
+[^lo823]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Pages 8-23 to 8-24.
+[^lo825]: Sony Computer Entertainment Inc. Run-Time Library Overview. January 1999. Page 8-25.