ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
graph.c File Reference
#include <kernel.h>
#include <tamtypes.h>
#include <stdio.h>
#include <gif_tags.h>
#include <gs_gp.h>
#include <gs_psm.h>
#include <dma.h>
#include <dma_tags.h>
#include <draw.h>
#include <graph.h>
#include <packet.h>
+ Include dependency graph for graph.c:

Go to the source code of this file.

Functions

void init_gs (framebuffer_t *frame, zbuffer_t *z)
 
void init_drawing_environment (packet_t *packet, framebuffer_t *frame, zbuffer_t *z)
 
void render (packet_t *packet, framebuffer_t *frame)
 
int main (void)
 

Function Documentation

◆ init_drawing_environment()

void init_drawing_environment ( packet_t packet,
framebuffer_t frame,
zbuffer_t z 
)

Definition at line 60 of file graph.c.

61 {
62 
63  // This is our generic qword pointer.
64  qword_t *q = packet->data;
65 
66  // This will setup a default drawing environment.
67  q = draw_setup_environment(q,0,frame,z);
68 
69  // This is where you could add various other drawing environment settings,
70  // or keep on adding onto the packet, but I'll stop with the default setup
71  // by appending a finish tag.
72 
73  q = draw_finish(q);
74 
75  // Now send the packet, no need to wait since it's the first.
77 
78  // Wait until the finish event occurs.
80 
81 }
int dma_channel_send_normal(int channel, void *data, int qwc, int flags, int spr)
Definition: dma.c:238
#define DMA_CHANNEL_GIF
Definition: dma.h:24
qword_t * draw_setup_environment(qword_t *q, int context, framebuffer_t *frame, zbuffer_t *z)
Definition: draw.c:11
void draw_wait_finish(void)
Definition: draw.c:200
qword_t * draw_finish(qword_t *q)
Definition: draw.c:188
packet_t packet
Definition: font.c:24
qword_t * data
Definition: packet.h:27

References packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_normal(), draw_finish(), draw_setup_environment(), draw_wait_finish(), and packet.

Referenced by main().

◆ init_gs()

void init_gs ( framebuffer_t frame,
zbuffer_t z 
)

Definition at line 27 of file graph.c.

28 {
29 
30  // Define a 32-bit 512x512 framebuffer.
31  frame->width = 512;
32  frame->height = 512;
33  frame->mask = 0;
34  frame->psm = GS_PSM_32;
35 
36  // Switch the mask on for some fun.
37  //frame->mask = 0xFFFF0000;
38 
39  // Allocate some vram for our framebuffer
40  frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
41 
42  // Disable the zbuffer.
43  z->enable = 0;
44  z->address = 0;
45  z->mask = 0;
46  z->zsm = 0;
47 
48  // Initialize the screen and tie the framebuffer to the read circuits.
49  graph_initialize(frame->address,frame->width,frame->height,frame->psm,0,0);
50 
51  // This is how you would define a custom mode
52  //graph_set_mode(GRAPH_MODE_NONINTERLACED,GRAPH_MODE_VGA_1024_60,GRAPH_MODE_FRAME,GRAPH_DISABLE);
53  //graph_set_screen(0,0,512,768);
54  //graph_set_bgcolor(0,0,0);
55  //graph_set_framebuffer_filtered(frame->address,frame->width,frame->psm,0,0);
56  //graph_enable_output();
57 
58 }
int graph_vram_allocate(int width, int height, int psm, int alignment)
Definition: graph_vram.c:7
#define GRAPH_ALIGN_PAGE
Definition: graph_vram.h:13
#define GS_PSM_32
Definition: gs_psm.h:11
int graph_initialize(int fbp, int width, int height, int psm, int x, int y)
Definition: graph.c:6
unsigned int address
Definition: draw_buffers.h:41
unsigned int height
Definition: draw_buffers.h:43
unsigned int psm
Definition: draw_buffers.h:44
unsigned int mask
Definition: draw_buffers.h:45
unsigned int width
Definition: draw_buffers.h:42
unsigned int mask
Definition: draw_buffers.h:53
unsigned int address
Definition: draw_buffers.h:51
unsigned int enable
Definition: draw_buffers.h:49
unsigned int zsm
Definition: draw_buffers.h:52

References framebuffer_t::address, zbuffer_t::address, zbuffer_t::enable, GRAPH_ALIGN_PAGE, graph_initialize(), graph_vram_allocate(), GS_PSM_32, framebuffer_t::height, framebuffer_t::mask, zbuffer_t::mask, framebuffer_t::psm, framebuffer_t::width, and zbuffer_t::zsm.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 145 of file graph.c.

146 {
147 
148  // The minimum buffers needed for single buffered rendering.
149  framebuffer_t frame;
150  zbuffer_t z;
151 
152  // The data packet.
154 
155  // Init GIF dma channel.
158 
159  // Init the GS, framebuffer, and zbuffer.
160  init_gs(&frame,&z);
161 
162  // Init the drawing environment and framebuffer.
163  init_drawing_environment(packet,&frame,&z);
164 
165  // Render the sample.
166  render(packet,&frame);
167 
168  // Free the vram.
169  graph_vram_free(frame.address);
170 
171  // Free the packet.
173 
174  // Disable output and reset the GS.
175  graph_shutdown();
176 
177  // Shutdown our currently used dma channel.
179 
180  // Sleep
181  SleepThread();
182 
183  return 0;
184 
185 }
int dma_channel_initialize(int channel, void *handler, int flags)
Definition: dma.c:58
void dma_channel_fast_waits(int channel)
Definition: dma.c:108
int dma_channel_shutdown(int channel, int flags)
Definition: dma.c:354
int graph_shutdown(void)
Definition: graph_mode.c:428
void graph_vram_free(int address)
Definition: graph_vram.c:30
s32 SleepThread(void)
packet_t * packet_init(int qwords, int type)
Definition: packet.c:8
#define PACKET_NORMAL
Definition: packet.h:14
void packet_free(packet_t *packet)
Definition: packet.c:63
void init_drawing_environment(packet_t *packet, framebuffer_t *frame, zbuffer_t *z)
Definition: graph.c:60
void render(packet_t *packet, framebuffer_t *frame)
Definition: graph.c:83
void init_gs(framebuffer_t *frame, zbuffer_t *z)
Definition: graph.c:27
#define NULL
Definition: tamtypes.h:91

References framebuffer_t::address, dma_channel_fast_waits(), DMA_CHANNEL_GIF, dma_channel_initialize(), dma_channel_shutdown(), graph_shutdown(), graph_vram_free(), init_drawing_environment(), init_gs(), NULL, packet, packet_free(), packet_init(), PACKET_NORMAL, render(), and SleepThread().

◆ render()

void render ( packet_t packet,
framebuffer_t frame 
)

Definition at line 83 of file graph.c.

84 {
85 
86  // Used for the render loop.
87  int loop0;
88 
89  // Used for the qword pointer.
90  qword_t *q = packet->data;
91 
92  // Since we only have one packet, we need to wait until the dmac is done
93  // before reusing our pointer;
94  dma_wait_fast();
95 
96  q = packet->data;
97  q = draw_clear(q,0,0,0,frame->width,frame->height,0,0,0);
98  q = draw_finish(q);
99 
101 
102  // Wait until the screen is cleared.
104 
105  // Update the screen.
107 
108  // Draw 20 100x100 squares from the origin point.
109  for (loop0=0;loop0<20;loop0++)
110  {
111 
112  // No dmatags in a normal transfer.
113  q = packet->data;
114 
115  // Wait for our previous dma transfer to end.
116  dma_wait_fast();
117 
118  // Draw another square on the screen.
119  PACK_GIFTAG(q, GIF_SET_TAG(4, 1, 0, 0, 0, 1),GIF_REG_AD);
120  q++;
121  PACK_GIFTAG(q, GIF_SET_PRIM(6, 0, 0, 0, 0, 0, 0, 0, 0), GIF_REG_PRIM);
122  q++;
123  PACK_GIFTAG(q, GIF_SET_RGBAQ((loop0 * 10), 0, 255 - (loop0 * 10), 0x80, 0x3F800000), GIF_REG_RGBAQ);
124  q++;
125  PACK_GIFTAG(q, GIF_SET_XYZ(( (loop0 * 20) << 4) + (2048<<4), ((loop0 * 10) << 4) + (2048<<4), 0), GIF_REG_XYZ2);
126  q++;
127  PACK_GIFTAG(q, GIF_SET_XYZ( (((loop0 * 20) + 100) << 4) + (2048<<4), (((loop0 * 10) + 100) << 4) + (2048<<4), 0), GIF_REG_XYZ2);
128  q++;
129 
130  q = draw_finish(q);
131 
132  // DMA send
134 
135  // Wait until the drawing is finished.
137 
138  // Now initiate vsync.
140 
141  }
142 
143 }
void dma_wait_fast(void)
Definition: dma.c:115
qword_t * draw_clear(qword_t *q, int context, float x, float y, float width, float height, int r, int g, int b)
Definition: draw.c:148
#define GIF_SET_RGBAQ(R, G, B, A, Q)
Definition: gif_tags.h:92
#define GIF_SET_PRIM(PRIM, IIP, TME, FGE, ABE, AA1, FST, CTXT, FIX)
Definition: gif_tags.h:85
#define GIF_REG_RGBAQ
Definition: gif_tags.h:44
#define GIF_REG_PRIM
Definition: gif_tags.h:42
#define PACK_GIFTAG(Q, D0, D1)
Definition: gif_tags.h:76
#define GIF_SET_TAG(NLOOP, EOP, PRE, PRIM, FLG, NREG)
Definition: gif_tags.h:80
#define GIF_REG_AD
Definition: gif_tags.h:72
#define GIF_SET_XYZ(X, Y, Z)
Definition: gif_tags.h:103
#define GIF_REG_XYZ2
Definition: gif_tags.h:52
void graph_wait_vsync(void)
Definition: graph.c:99

References packet_t::data, DMA_CHANNEL_GIF, dma_channel_send_normal(), dma_wait_fast(), draw_clear(), draw_finish(), draw_wait_finish(), GIF_REG_AD, GIF_REG_PRIM, GIF_REG_RGBAQ, GIF_REG_XYZ2, GIF_SET_PRIM, GIF_SET_RGBAQ, GIF_SET_TAG, GIF_SET_XYZ, graph_wait_vsync(), framebuffer_t::height, PACK_GIFTAG, packet, and framebuffer_t::width.

Referenced by main().