Citro3d
Loading...
Searching...
No Matches
framebuffer.c
Go to the documentation of this file.
1#include "internal.h"
2
3static const u8 colorFmtSizes[] = {2,1,0,0,0};
4static const u8 depthFmtSizes[] = {0,0,1,2};
5
6u32 C3D_CalcColorBufSize(u32 width, u32 height, GPU_COLORBUF fmt)
7{
8 u32 size = width*height;
9 return size*(2+colorFmtSizes[fmt]);
10}
11
12u32 C3D_CalcDepthBufSize(u32 width, u32 height, GPU_DEPTHBUF fmt)
13{
14 u32 size = width*height;
15 return size*(2+depthFmtSizes[fmt]);
16}
17
18C3D_FrameBuf* C3D_GetFrameBuf(void)
19{
20 C3D_Context* ctx = C3Di_GetContext();
21
22 if (!(ctx->flags & C3DiF_Active))
23 return NULL;
24
25 ctx->flags |= C3DiF_FrameBuf;
26 return &ctx->fb;
27}
28
29void C3D_SetFrameBuf(C3D_FrameBuf* fb)
30{
31 C3D_Context* ctx = C3Di_GetContext();
32
33 if (!(ctx->flags & C3DiF_Active))
34 return;
35
36 if (fb != &ctx->fb)
37 memcpy(&ctx->fb, fb, sizeof(*fb));
38 ctx->flags |= C3DiF_FrameBuf;
39}
40
41void C3D_FrameBufTex(C3D_FrameBuf* fb, C3D_Tex* tex, GPU_TEXFACE face, int level)
42{
43 C3D_FrameBufAttrib(fb, tex->width, tex->height, false);
44 C3D_FrameBufColor(fb, C3D_TexGetImagePtr(tex,
45 C3Di_TexIs2D(tex) ? tex->data : tex->cube->data[face], level, NULL),
46 (GPU_COLORBUF)tex->fmt);
47}
48
49void C3Di_FrameBufBind(C3D_FrameBuf* fb)
50{
51 u32 param[4] = { 0, 0, 0, 0 };
52
53 GPUCMD_AddWrite(GPUREG_FRAMEBUFFER_INVALIDATE, 1);
54
55 param[0] = osConvertVirtToPhys(fb->depthBuf) >> 3;
56 param[1] = osConvertVirtToPhys(fb->colorBuf) >> 3;
57 param[2] = 0x01000000 | (((u32)(fb->height-1) & 0xFFF) << 12) | (fb->width & 0xFFF);
58 GPUCMD_AddIncrementalWrites(GPUREG_DEPTHBUFFER_LOC, param, 3);
59
60 GPUCMD_AddWrite(GPUREG_RENDERBUF_DIM, param[2]);
61 GPUCMD_AddWrite(GPUREG_DEPTHBUFFER_FORMAT, fb->depthFmt);
62 GPUCMD_AddWrite(GPUREG_COLORBUFFER_FORMAT, colorFmtSizes[fb->colorFmt] | ((u32)fb->colorFmt << 16));
63 GPUCMD_AddWrite(GPUREG_FRAMEBUFFER_BLOCK32, fb->block32 ? 1 : 0);
64
65 // Enable or disable color/depth buffers
66 param[0] = param[1] = fb->colorBuf ? fb->colorMask : 0;
67 param[2] = param[3] = fb->depthBuf ? fb->depthMask : 0;
68 GPUCMD_AddIncrementalWrites(GPUREG_COLORBUFFER_READ, param, 4);
69}
70
71void C3D_FrameBufClear(C3D_FrameBuf* frameBuf, C3D_ClearBits clearBits, u32 clearColor, u32 clearDepth)
72{
73 u32 size = (u32)frameBuf->width * frameBuf->height;
74 u32 cfs = colorFmtSizes[frameBuf->colorFmt];
75 u32 dfs = depthFmtSizes[frameBuf->depthFmt];
76 void* colorBufEnd = (u8*)frameBuf->colorBuf + size*(2+cfs);
77 void* depthBufEnd = (u8*)frameBuf->depthBuf + size*(2+dfs);
78
79 if (clearBits & C3D_CLEAR_COLOR)
80 {
81 if (clearBits & C3D_CLEAR_DEPTH)
82 GX_MemoryFill(
83 (u32*)frameBuf->colorBuf, clearColor, (u32*)colorBufEnd, BIT(0) | (cfs << 8),
84 (u32*)frameBuf->depthBuf, clearDepth, (u32*)depthBufEnd, BIT(0) | (dfs << 8));
85 else
86 GX_MemoryFill(
87 (u32*)frameBuf->colorBuf, clearColor, (u32*)colorBufEnd, BIT(0) | (cfs << 8),
88 NULL, 0, NULL, 0);
89 } else
90 GX_MemoryFill(
91 (u32*)frameBuf->depthBuf, clearDepth, (u32*)depthBufEnd, BIT(0) | (dfs << 8),
92 NULL, 0, NULL, 0);
93}
94
95void C3D_FrameBufTransfer(C3D_FrameBuf* frameBuf, gfxScreen_t screen, gfx3dSide_t side, u32 transferFlags)
96{
97 u32* outputFrameBuf = (u32*)gfxGetFramebuffer(screen, side, NULL, NULL);
98 u32 dim = GX_BUFFER_DIM((u32)frameBuf->width, (u32)frameBuf->height);
99 GX_DisplayTransfer((u32*)frameBuf->colorBuf, dim, outputFrameBuf, dim, transferFlags);
100}
C3D_FrameBuf * C3D_GetFrameBuf(void)
Definition: framebuffer.c:18
void C3D_FrameBufTex(C3D_FrameBuf *fb, C3D_Tex *tex, GPU_TEXFACE face, int level)
Definition: framebuffer.c:41
void C3D_FrameBufTransfer(C3D_FrameBuf *frameBuf, gfxScreen_t screen, gfx3dSide_t side, u32 transferFlags)
Definition: framebuffer.c:95
u32 C3D_CalcColorBufSize(u32 width, u32 height, GPU_COLORBUF fmt)
Definition: framebuffer.c:6
void C3D_FrameBufClear(C3D_FrameBuf *frameBuf, C3D_ClearBits clearBits, u32 clearColor, u32 clearDepth)
Definition: framebuffer.c:71
u32 C3D_CalcDepthBufSize(u32 width, u32 height, GPU_DEPTHBUF fmt)
Definition: framebuffer.c:12
void C3D_SetFrameBuf(C3D_FrameBuf *fb)
Definition: framebuffer.c:29
void C3Di_FrameBufBind(C3D_FrameBuf *fb)
Definition: framebuffer.c:49
@ C3DiF_Active
Definition: internal.h:75
@ C3DiF_FrameBuf
Definition: internal.h:80
u32 flags
Definition: internal.h:38
C3D_FrameBuf fb
Definition: internal.h:65