PS2SDK
PS2 Homebrew Libraries
graph_vram.c
1 #include <gs_psm.h>
2 
3 #include <graph_vram.h>
4 
5 static int graph_vram_pointer = 0;
6 
7 int graph_vram_allocate(int width, int height, int psm, int alignment)
8 {
9 
10  int size;
11 
12  // Calculate the size and increment the pointer
13  size = graph_vram_size(width,height,psm,alignment);
14 
15  graph_vram_pointer += size;
16 
17  // Align the pointer
18  graph_vram_pointer = -alignment & (graph_vram_pointer + (alignment-1));
19 
20  // If the pointer overflows the vram size
21  if (graph_vram_pointer > GRAPH_VRAM_MAX_WORDS)
22  {
23 
24  graph_vram_pointer -= size;
25  return -1;
26 
27  }
28 
29  return graph_vram_pointer - size;
30 
31 }
32 
33 void graph_vram_free(int address)
34 {
35 
36  graph_vram_pointer = address;
37 
38 }
39 
40 void graph_vram_clear(void)
41 {
42 
43  graph_vram_pointer = 0;
44 
45 }
46 
47 int graph_vram_size(int width, int height, int psm, int alignment)
48 {
49 
50  int size = 0;
51 
52  // First correct the buffer width to be a multiple of 64 or 128
53  // If the width is less than or equal to 16, then it's a palette
54  if (width > 16)
55  {
56  switch (psm)
57  {
58 
59  case GS_PSM_8:
60  case GS_PSM_4:
61  case GS_PSM_8H:
62  case GS_PSM_4HL:
63  case GS_PSM_4HH: width = -128 & (width + 127); break;
64  default: width = -64 & (width + 63); break;
65 
66  }
67  }
68 
69  // Texture storage size is in pixels/word
70  switch (psm)
71  {
72 
73  case GS_PSM_4: size = width*(height>>3); break;
74  case GS_PSM_8: size = width*(height>>2); break;
75  case GS_PSM_24:
76  case GS_PSM_32:
77  case GS_PSM_8H:
78  case GS_PSM_4HL:
79  case GS_PSM_4HH:
80  case GS_PSMZ_24:
81  case GS_PSMZ_32: size = width*height; break;
82  case GS_PSM_16:
83  case GS_PSM_16S:
84  case GS_PSMZ_16:
85  case GS_PSMZ_16S: size = width*(height>>1); break;
86  default: return 0;
87 
88  }
89 
90  // The buffer size is dependent on alignment
91  size = -alignment & (size + (alignment-1));
92 
93  return size;
94 
95 }
GRAPH_VRAM_MAX_WORDS
#define GRAPH_VRAM_MAX_WORDS
Definition: graph_vram.h:10
graph_vram_clear
void graph_vram_clear(void)
Definition: graph_vram.c:40
GS_PSMZ_16S
#define GS_PSMZ_16S
Definition: gs_psm.h:37
graph_vram_size
int graph_vram_size(int width, int height, int psm, int alignment)
Definition: graph_vram.c:47
GS_PSM_4HL
#define GS_PSM_4HL
Definition: gs_psm.h:27
GS_PSM_8
#define GS_PSM_8
Definition: gs_psm.h:21
GS_PSM_8H
#define GS_PSM_8H
Definition: gs_psm.h:25
GS_PSM_16
#define GS_PSM_16
Definition: gs_psm.h:15
graph_vram_allocate
int graph_vram_allocate(int width, int height, int psm, int alignment)
Definition: graph_vram.c:7
graph_vram_free
void graph_vram_free(int address)
Definition: graph_vram.c:33
GS_PSM_4
#define GS_PSM_4
Definition: gs_psm.h:23
graph_vram.h
GS_PSM_32
#define GS_PSM_32
Definition: gs_psm.h:11
GS_PSMZ_24
#define GS_PSMZ_24
Definition: gs_psm.h:33
GS_PSMZ_16
#define GS_PSMZ_16
Definition: gs_psm.h:35
GS_PSMZ_32
#define GS_PSMZ_32
Definition: gs_psm.h:31
gs_psm.h
GS_PSM_4HH
#define GS_PSM_4HH
Definition: gs_psm.h:29
GS_PSM_24
#define GS_PSM_24
Definition: gs_psm.h:13
GS_PSM_16S
#define GS_PSM_16S
Definition: gs_psm.h:17