1 /***************************************************************************
2 * Copyright (C) 2010 PCSX4ALL Team *
3 * Copyright (C) 2010 Unai *
4 * Copyright (C) 2016 Senquack (dansilsby <AT> gmail <DOT> com) *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
20 ***************************************************************************/
28 // Header shared between both standalone gpu_unai (gpu.cpp) and new
29 // gpulib-compatible gpu_unai (gpulib_if.cpp)
30 // -> Anything here should be for gpu_unai's private use. <-
32 ///////////////////////////////////////////////////////////////////////////////
35 //#define ENABLE_GPU_NULL_SUPPORT // Enables NullGPU support
36 //#define ENABLE_GPU_LOG_SUPPORT // Enables gpu logger, very slow only for windows debugging
37 //#define ENABLE_GPU_ARMV7 // Enables ARMv7 optimized assembly
39 //Poly routine options (default is integer math and accurate division)
40 //#define GPU_UNAI_USE_FLOATMATH // Use float math in poly routines
41 //#define GPU_UNAI_USE_FLOAT_DIV_MULTINV // If GPU_UNAI_USE_FLOATMATH is defined,
42 // use multiply-by-inverse for division
43 //#define GPU_UNAI_USE_INT_DIV_MULTINV // If GPU_UNAI_USE_FLOATMATH is *not*
44 // defined, use old inaccurate division
47 #define GPU_INLINE static inline __attribute__((always_inline))
48 #define INLINE static inline __attribute__((always_inline))
76 #define LExRead(v_) (v_.v)
82 #define LExRead(v) (v)
86 static inline u32 le32_to_u32(le32_t le)
88 return LE32TOH(LExRead(le));
91 static inline s32 le32_to_s32(le32_t le)
93 return (int32_t) LE32TOH(LExRead(le));
96 static inline u32 le32_raw(le32_t le)
101 static inline le32_t u32_to_le32(u32 u)
103 return (le32_t){ HTOLE32(u) };
106 static inline u16 le16_to_u16(le16_t le)
108 return LE16TOH(LExRead(le));
111 static inline s16 le16_to_s16(le16_t le)
113 return (int16_t) LE16TOH(LExRead(le));
116 static inline u16 le16_raw(le16_t le)
121 static inline le16_t u16_to_le16(u16 u)
123 return (le16_t){ HTOLE16(u) };
141 template<class T> static inline void SwapValues(T &x, T &y)
143 T tmp(x); x = y; y = tmp;
147 static inline T Min2 (const T a, const T b)
153 static inline T Min3 (const T a, const T b, const T c)
155 return Min2(Min2(a,b),c);
159 static inline T Max2 (const T a, const T b)
165 static inline T Max3 (const T a, const T b, const T c)
167 return Max2(Max2(a,b),c);
171 ///////////////////////////////////////////////////////////////////////////////
174 // Convert 24bpp color parameter of GPU command to 16bpp (15bpp + mask bit)
175 #define GPU_RGB16(rgb) ((((rgb)&0xF80000)>>9)|(((rgb)&0xF800)>>6)|(((rgb)&0xF8)>>3))
177 // Sign-extend 11-bit coordinate command param
178 #define GPU_EXPANDSIGN(x) (((s32)(x)<<(32-11))>>(32-11))
180 // Max difference between any two X or Y primitive coordinates
181 #define CHKMAX_X 1024
184 #define FRAME_BUFFER_SIZE (1024*512*2)
185 #define FRAME_WIDTH 1024
186 #define FRAME_HEIGHT 512
187 #define FRAME_OFFSET(x,y) (((y)<<10)+(x))
188 #define FRAME_BYTE_STRIDE 2048
189 #define FRAME_BYTES_PER_PIXEL 2
191 static inline s32 GPU_DIV(s32 rs, s32 rt)
193 return rt ? (rs / rt) : (0);
196 // 'Unsafe' version of above that doesn't check for div-by-zero
197 #define GPU_FAST_DIV(rs, rt) ((signed)(rs) / (signed)(rt))
199 // warning: gpu_arm.S asm uses this struct, update the asm if you change this
200 struct gpu_unai_inner_t {
201 le16_t* TBA; // 00 Ptr to current texture in VRAM
202 le16_t* CBA; // 04 Ptr to current CLUT in VRAM
204 // 22.10 Fixed-pt texture coords, mask, scanline advance
205 // NOTE: U,V are no longer packed together into one u32, this proved to be
206 // too imprecise, leading to pixel dropouts. Example: NFS3's skybox.
207 u32 u, v; // 08 not fractional for sprites
208 u32 u_msk, v_msk; // 10 always 22.10
211 s32 u_inc, v_inc; // 18 poly uv increment, 22.10
214 s32 y0, y1; // 18 sprite y range
218 // Color for flat-shaded, texture-blended prims
219 u8 r5, g5, b5, pad5; // 20 5-bit light for undithered prims
220 u8 r8, g8, b8, pad8; // 24 8-bit light for dithered prims
222 // Color for Gouraud-shaded prims
223 // Fixed-pt 8.8 rgb triplet
224 // Packed fixed-pt 8.3:8.3:8.2 rgb triplet
225 // layout: ccccccccXXXXXXXX for c in [r, g, b]
228 gcol_t gInc; // 30 Increment along scanline for gCol
230 // Color for flat-shaded, untextured prims
231 u16 PixelData; // 38 bgr555 color for untextured flat-shaded polys
233 u8 blit_mask; // Determines what pixels to skip when rendering.
234 // Only useful on low-resolution devices using
235 // a simple pixel-dropping downscaler for PS1
236 // high-res modes. See 'pixel_skip' option.
238 u8 ilace_mask; // Determines what lines to skip when rendering.
239 // Normally 0 when PS1 240 vertical res is in
240 // use and ilace_force is 0. When running in
241 // PS1 480 vertical res on a low-resolution
242 // device (320x240), will usually be set to 1
243 // so odd lines are not rendered. (Unless future
244 // full-screen scaling option is in use ..TODO)
249 GPUPacket PacketBuffer;
253 le16_t *downscale_vram;
255 ////////////////////////////////////////////////////////////////////////////
256 // Variables used only by older standalone version of gpu_unai (gpu.cpp)
259 u32 tex_window; // Current texture window vals (set by GP0(E2h) cmd)
262 bool fb_dirty; // Framebuffer is dirty (according to GPU)
265 // NOTE: Standalone older gpu_unai didn't care about horiz display range
266 u16 DisplayArea[6]; // [0] : Start of display area (in VRAM) X
267 // [1] : Start of display area (in VRAM) Y
268 // [2] : Display mode resolution HORIZONTAL
269 // [3] : Display mode resolution VERTICAL
270 // [4] : Vertical display range (on TV) START
271 // [5] : Vertical display range (on TV) END
273 ////////////////////////////////////////////////////////////////////////////
274 // Dma Transfers info
279 u32 *last_dma; // Last dma pointer
280 bool FrameToRead; // Load image in progress
281 bool FrameToWrite; // Store image in progress
284 ////////////////////////////////////////////////////////////////////////////
287 int skipCount; // Frame skip (0,1,2,3...)
288 bool isSkip; // Skip frame (according to GPU)
289 bool skipFrame; // Skip this frame (according to frame skip)
290 bool wasSkip; // Skip frame old value (according to GPU)
291 bool skipGPU; // Skip GPU primitives
294 // END of standalone gpu_unai variables
295 ////////////////////////////////////////////////////////////////////////////
297 u32 TextureWindowCur; // Current setting from last GP0(0xE2) cmd (raw form)
298 u8 TextureWindow[4]; // [0] : Texture window offset X
299 // [1] : Texture window offset Y
300 // [2] : Texture window mask X
301 // [3] : Texture window mask Y
303 u16 DrawingArea[4]; // [0] : Drawing area top left X
304 // [1] : Drawing area top left Y
305 // [2] : Drawing area bottom right X
306 // [3] : Drawing area bottom right Y
308 s16 DrawingOffset[2]; // [0] : Drawing offset X (signed)
309 // [1] : Drawing offset Y (signed)
311 ////////////////////////////////////////////////////////////////////////////
312 // Inner Loop parameters
314 __attribute__((aligned(32)))
315 gpu_unai_inner_t inn;
317 // End of inner Loop parameters
318 ////////////////////////////////////////////////////////////////////////////
320 bool prog_ilace_flag; // Tracks successive frames for 'prog_ilace' option
328 gpu_unai_config_t config;
330 u8 LightLUT[32*32]; // 5-bit lighting LUT (gpu_inner_light.h)
331 u32 DitherMatrix[64]; // Matrix of dither coefficients
334 static __attribute__((aligned(32))) gpu_unai_t gpu_unai;
336 // Global config that frontend can alter.. Values are read in GPU_init().
337 // TODO: if frontend menu modifies a setting, add a function that can notify
338 // GPU plugin to use new setting.
339 gpu_unai_config_t gpu_unai_config_ext;
341 ///////////////////////////////////////////////////////////////////////////////
342 // Internal inline funcs to get option status: (Allows flexibility)
343 static inline bool LightingEnabled()
345 return gpu_unai.config.lighting;
348 static inline bool FastLightingEnabled()
350 return gpu_unai.config.fast_lighting;
353 static inline bool BlendingEnabled()
355 return gpu_unai.config.blending;
358 static inline bool DitheringEnabled()
360 return gpu_unai.config.dithering;
363 static inline bool ForcedDitheringEnabled()
365 return gpu_unai.config.force_dithering;
368 static inline bool ProgressiveInterlaceEnabled()
371 // Using this old option greatly decreases quality of image. Disabled
372 // for now when using new gpulib, since it also adds more work in loops.
375 return gpu_unai.config.prog_ilace;
379 // For now, 320x240 output resolution is assumed, using simple line-skipping
380 // and pixel-skipping downscaler.
381 // TODO: Flesh these out so they return useful values based on whether
382 // running on higher-res device or a resampling downscaler is enabled.
383 static inline bool PixelSkipEnabled()
385 return gpu_unai.config.pixel_skip || gpu_unai.config.scale_hires;
388 static inline bool LineSkipEnabled()