//////////////////////////////////////////////////////////////////////////////
//
// Asura_PS2_WaterDefines.h
//
// Copyright (c) 2004 Rebellion. All rights reserved.
//
//  Notes:
//
//
// VU memory usage:  NOTE :: this hasn't been updated to reflect the water renderer values
//
// (MP - 06.07.2005) Basically ignore much of this. Much of it is either
// wrong or completely irrelevant to the water renderer. Hopefully I will get
// time to sort it out... but then hopefully I won't need to touch this code
// again.
//
//      Buffer sizes, etc.:
//
//          GIF tags                    = 4               \
//          Constant cam data buffer    = 4 * 5            |-- constant input
//          Constant shadow data buffer = 1               /
//          
//          Renderstate                 = 2 * 10          \
//          Per-mesh camera data buffer = 2 * 4 * 12       |
//          Lighting data buffers       = 2 * (1 + 14)     |__ input that changes sporadically, though at least once per mesh
//          Shadow data buffers         = 2 * 6            |
//          ST scale/trans parameters   = 1                |
//          Y scale/trans parameters    = 2               /
//          
//          Input buffers               = 2 * 4N          >--- input per strip
//
//          ST overwrite buffer         = N               \___ intermediate data
//          Clip input buffer           = 3N              /
//
//          Geom output buffers         = 2 * (1 + 3N)    \___ output data
//          RS output buffer            = 8               /
//
//          VI reg stack                = 6               >--- internal usage
//
//      Max vertices:
//
//          4 + 4*5 + 1 + 2*10 + 2*4*12 + 2*(1+14) + 2*6 + 1 + 2 + 2*4N + N + 3N + 2*(1+3N) + 8 + 6 <= 1024
//              =>
//          202 + 18N <= 1024
//              =>  .
//          N <= 45.6
//
//          However, set N=44 to allow a little slack for future changes.
//
//      Memory layout:
//      
//             0   /--------------------------\
//                 |         GIF tags         |    4 QWs - geometry setup and draw, shadow setup and draw
//             4   |--------------------------|
//                 |    constant cam data     |   20 QWs - (SCISSOR, XYOFFSET, GBC->S scale, GBC->S trans, GBC->SC scale) * 4
//            24   |--------------------------|
//                 |          sun dir         |    1 QW  - sun dir XYZ
//            25   |--------------------------|
//                 |        renderstate       |   20 QWs - (base (ALPHA, TEX1, TEX0, MIPTBP1, TEST), self illum ALPHA, ST overwrite (ALPHA, TEX1, TEX0, MIPTBP1)) * 2
//            45   |--------------------------|
//                 |     per-mesh cam data    |   96 QWs - (O->GBC mat, O->V mat, tex mat) * 4 * 2
//           141   |--------------------------|
//                 |          unused          |   42 QWs -
//           183   |--------------------------|
//                 |  ST scale/trans params   |    1 QW
//           184   |--------------------------|
//                 |   Y scale/trans params   |    2 QWs - (angle params, Y params)
//           186   |--------------------------|
//                 |       input buffers      |  352 QWs - (XYZ, ST, RGBA, normal) * N * 2
//           538   |--------------------------|
//                 |    ST overwrite buffer   |   47 QWs - (ST) * N, plus space for overrun of up to three vertices
//           585   |--------------------------|
//                 |     clip input buffer    |  132 QWs - (XYZW, ST + should-clip flag, RGBA) * N
//           717   |--------------------------|
//                 |      output buffers      |  266 QWs - (draw GIF tag, (ST, RGBAQ, XYZ) * N) * 2
//           983   |--------------------------|
//                 |     RS output buffer     |    8 QWs - (setup GIF tag, up to 7 registers)
//           991   |--------------------------|
//                 |       VI reg stack       |    6 QWs
//           997   |--------------------------|
//                 |         <unused>         |   27 QWs
//          1024   \--------------------------/
//
//      Memory layout of the output buffers during clipping:
//
//                 :                          :
//             K   |--------------------------|
//                 |   clipped output buffer  | 1+3M QWs
//                 |--------------------------|
//                 |    clipper workspace 1   |   36 QWs
//                 |--------------------------|
//                 |    clipper workspace 2   |   36 QWs
//                 |--------------------------|
//                 |   clipped ST Overwrites  |    M QWs
//                 |--------------------------|
//                 :                          :
//
//          where M is the maximum number of vertices that will fit.
//
//              1+3M + 36 + 36 + M <= 133
//                  =>
//              73 + 4M <= 133
//                  =>
//              M <= 15
//
//          => set M=15.
//
//          (NB: M must be at least 9 since just a single input tri can
//          generate that many output vertices.)
//
//////////////////////////////////////////////////////////////////////////////

// B E G I N   F I L E   W R A P P E R ///////////////////////////////////////

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef ___ASURA_PS2_WATERDEFINES_H___
#define ___ASURA_PS2_WATERDEFINES_H___ 1

// M A C R O S ///////////////////////////////////////////////////////////////

// Limits.
#define ASURA_PS2_WATER__MAX_VERTS_PER_STRIP              (44)
#define ASURA_PS2_WATER__MAX_CAMERAS                      (4)

// ITOP data: mesh flags.
#define ASURA_PS2_WATER__FLAG__SQUARE_FRESNEL			  (0x002)	// new flag - on by default, off by request
#define ASURA_PS2_WATER__FLAG__SKIP_KICKS				  (0x004)
#define ASURA_PS2_WATER__FLAG__SWAP_BLEND_HORIZON		  (0x008)
#define ASURA_PS2_WATER__FLAG__HORIZON_REFLECT            (0x020)
#define ASURA_PS2_WATER__FLAG__SUN_REFLECTIVE             (0x040)
#define ASURA_PS2_WATER__MASK__PASS_1                     (ASURA_PS2_WATER__FLAG__HORIZON_REFLECT)
#define ASURA_PS2_WATER__MASK__PASS_2                     (ASURA_PS2_WATER__FLAG__SUN_REFLECTIVE )
#define ASURA_PS2_WATER__MASK__MULTIPASS                  (ASURA_PS2_WATER__MASK__PASS_1 | ASURA_PS2_WATER__MASK__PASS_2)

// ITOP data: cam flags.
#define	ASURA_PS2_WATER__FLAG__VIS_FOR_MORE_THAN_ONE_CAM  (0x100)
#define ASURA_PS2_WATER__MASK__CAM_VIS_FLAGS              (0x0AA)
#define ASURA_PS2_WATER__MASK__CAM_CLIP_FLAGS             (0x055)
#define ASURA_PS2_WATER__MASK__CAM_VIS_AND_CLIP_FLAGS     (ASURA_PS2_WATER__MASK__CAM_VIS_FLAGS | ASURA_PS2_WATER__MASK__CAM_CLIP_FLAGS)

// ITOP data: per-strip, inserted into the packet data at export time.
#define ASURA_PS2_WATER__STRIP_MASK__VERTEX_QTY           (0x07F)
// (Space for flags available at 0x080 and 0x100 but not currently required.)

// These match the VU1 clipping bits.
#define ASURA_PS2_WATER__CLIP_RIGHT                       (0x001)
#define ASURA_PS2_WATER__CLIP_LEFT                        (0x002)
#define ASURA_PS2_WATER__CLIP_BOTTOM                      (0x004)
#define ASURA_PS2_WATER__CLIP_TOP                         (0x008)
#define ASURA_PS2_WATER__CLIP_FAR                         (0x010)
#define ASURA_PS2_WATER__CLIP_NEAR                        (0x020)
#define ASURA_PS2_WATER__CLIP_ALL                         (0x03F)

// Data area addresses.
#define ASURA_PS2_WATER__ADDR__GIF_TAGS                   (0)
#define ASURA_PS2_WATER__ADDR__CONST_CAM_DATA             (4)
#define ASURA_PS2_WATER__ADDR__NEG_SUN_DIR                (24)
#define ASURA_PS2_WATER__ADDR__RENDERSTATE_BFR_0          (25)
#define ASURA_PS2_WATER__ADDR__RENDERSTATE_BFR_1          (37)
#define ASURA_PS2_WATER__ADDR__PER_OBJ_CAM_DATA_BFR_0     (49)
#define ASURA_PS2_WATER__ADDR__PER_OBJ_CAM_DATA_BFR_1     (105)
#define ASURA_PS2_WATER__ADDR__Y_SCALE_AND_TRANS_PARAMS   (203)
#define ASURA_PS2_WATER__ADDR__INPUT_BFR_0                (211)
#define ASURA_PS2_WATER__ADDR__INPUT_BFR_1                (241)
#define ASURA_PS2_WATER__ADDR__VERT_STACK                 (271) // generated verts buffer - in world space and transformed, together
#define ASURA_PS2_WATER__ADDR__ST_OVERWRITE_BFR           (667)
#define ASURA_PS2_WATER__ADDR__CLIP_INPUT_BFR             (714)
#define ASURA_PS2_WATER__ADDR__GEOM_OUTPUT_BFR_0          (846)
#define ASURA_PS2_WATER__ADDR__GEOM_OUTPUT_BFR_1          (925)
#define ASURA_PS2_WATER__ADDR__RS_OUTPUT_BFR              (1004)
#define ASURA_PS2_WATER__ADDR__VI_REG_STACK               (1014)
#define ASURA_PS2_WATER__ADDR__END_OF_RAM_USED			  (1020)	// of 1023, leaving 4 QW.

#define ASURA_PS2_WATER__ADDR__DEBUG_PARAMS				  (ASURA_PS2_WATER__ADDR__END_OF_RAM_USED)

// Data area sizes.
#define ASURA_PS2_WATER__SIZE__GIF_TAGS                   (2)	// was 4, giving 2 shadow ones
#define ASURA_PS2_WATER__SIZE__CONST_CAM_DATA             (20)	// 5 QW per camera for scissor, xy offset, and 2 scales and an offset
#define ASURA_PS2_WATER__SIZE__SUN_DIR                    (1)
#define ASURA_PS2_WATER__SIZE__RENDERSTATE_BFR            (12)	// was 10, needed 2 more
#define ASURA_PS2_WATER__SIZE__PER_OBJ_CAM_DATA_BFR       (56)	// 14 per camera, 3 matrices and an offset / rotation / scale combination
#define ASURA_PS2_WATER__SIZE__Y_SCALE_AND_TRANS_PARAMS   (8)
#define ASURA_PS2_WATER__SIZE__INPUT_BFR                  (30)	// reduced to make it fit a whole num of triangles
#define ASURA_PS2_WATER__SIZE__ST_OVERWRITE_BFR           (47)
#define ASURA_PS2_WATER__SIZE__CLIP_INPUT_BFR             (132)
#define ASURA_PS2_WATER__SIZE__OUTPUT_BFR                 (79)
#define ASURA_PS2_WATER__SIZE__RS_OUTPUT_BFR              (10)
#define ASURA_PS2_WATER__SIZE__VI_REG_STACK               (6)

// GIF tag buffer details.
#define ASURA_PS2_WATER__OFFSET__GIF_TAGS_GEOM_SETUP      (0)
#define ASURA_PS2_WATER__OFFSET__GIF_TAGS_GEOM_DRAW       (1)
#define ASURA_PS2_WATER__OFFSET__GIF_TAGS_SHADOW_SETUP    (2)
#define ASURA_PS2_WATER__OFFSET__GIF_TAGS_SHADOW_DRAW     (3)

// Constant cam data buffer details.
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_SCISSOR        (0)
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_XYOFFSET       (1)
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_GBC2S_SCALE    (2)
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_GBC2S_TRANS    (3)
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_GBC2SC_SCALE   (4)
#define ASURA_PS2_WATER__OFFSET__CONST_CAM_NEXT_CAM       (5)

// GIF tag and renderstate input buffer details.
#define ASURA_PS2_WATER__OFFSET__RS_IN_BASE_ALPHA         (0)
#define ASURA_PS2_WATER__OFFSET__RS_IN_BASE_TEX1          (1)
#define ASURA_PS2_WATER__OFFSET__RS_IN_BASE_MIPTBP1       (2)
#define ASURA_PS2_WATER__OFFSET__RS_IN_BASE_TEX0          (3)
#define ASURA_PS2_WATER__OFFSET__RS_IN_BASE_TEST          (4)

#define ASURA_PS2_WATER__OFFSET__RS_IN_HORIZON_TEX1         (5)
#define ASURA_PS2_WATER__OFFSET__RS_IN_HORIZON_MIPTBP1      (6)
#define ASURA_PS2_WATER__OFFSET__RS_IN_HORIZON_TEX0         (7)

#define ASURA_PS2_WATER__OFFSET__RS_IN_SUN_ALPHA        (8)		// shared with horizon - both will just be additive I hope !
#define ASURA_PS2_WATER__OFFSET__RS_IN_SUN_TEX1         (9)
#define ASURA_PS2_WATER__OFFSET__RS_IN_SUN_MIPTBP1      (10)
#define ASURA_PS2_WATER__OFFSET__RS_IN_SUN_TEX0         (11)

// Per-object cam data buffer details.
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_O2GBC_MAT    (0)
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_CAM_POS      (4)
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_TEX_MAT      (8)
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_TEX_ROTATE   (12)	// required for horizon reflection
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_TEX_OFFSET   (13)	// required for horizon reflection
#define ASURA_PS2_WATER__OFFSET__CAM_PER_OBJ_NEXT_CAM     (14)

// Y params buffer details.
#define ASURA_PS2_WATER__OFFSET__Y_PARAMS_ANGLE           (0)
#define ASURA_PS2_WATER__OFFSET__Y_PARAMS_Y               (1)

// Clipper workspace buffer details.
#define ASURA_PS2_WATER__SIZE__CLIP_WORKSPACE             (36)  // Up to 9 vertices with 4 QWs per vertex.
#define ASURA_PS2_WATER__SIZE__CLIPPED_STS                (15)  // See memory map above for calculations.
#define ASURA_PS2_WATER__OFFSET__CLIP_WORKSPACE           (46)  //                  "
#define ASURA_PS2_WATER__OFFSET__CLIPPED_STS              (118) //                  "

// Geometry output buffer details for normal geometry.
#define ASURA_PS2_WATER__OFFSET__GEOM_OUT_DRAW_GIF_TAG    (0)
#define ASURA_PS2_WATER__OFFSET__GEOM_OUT_FIRST_VERTEX    (1)


// RS output buffer details for first (base) pass.
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_SETUP_GIF_TAG   (0)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_ALPHA           (1)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_TEX1            (2)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_MIPTBP1         (3)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_TEX0            (4)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_CLAMP			  (5)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_TEST            (6)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_SCISSOR         (7)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_0_XYOFFSET        (8)
#define ASURA_PS2_WATER__RS_OUT_0_NLOOP                   (8)

// RS output buffer details for second (Horizon reflection) pass.
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_SETUP_GIF_TAG   (0)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_ALPHA           (1)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_TEX1            (2)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_MIPTBP1         (3)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_TEX0            (4)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_1_CLAMP           (5)
#define ASURA_PS2_WATER__RS_OUT_1_NLOOP                   (5)

// RS output buffer details for third (Sunlight reflection) pass.
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_SETUP_GIF_TAG   (0)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_ALPHA           (1)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_TEX1            (2)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_MIPTBP1         (3)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_TEX0            (4)
#define ASURA_PS2_WATER__OFFSET__RS_OUT_2_CLAMP           (5)
#define ASURA_PS2_WATER__RS_OUT_2_NLOOP                   (5)

// VI register stack aliases.
#define ASURA_PS2_WATER__ADDR__VI_STACK_0                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 0)
#define ASURA_PS2_WATER__ADDR__VI_STACK_1                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 1)
#define ASURA_PS2_WATER__ADDR__VI_STACK_2                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 2)
#define ASURA_PS2_WATER__ADDR__VI_STACK_3                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 3)
#define ASURA_PS2_WATER__ADDR__VI_STACK_4                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 4)
#define ASURA_PS2_WATER__ADDR__VI_STACK_5                 (ASURA_PS2_WATER__ADDR__VI_REG_STACK + 5)


// Vertex Stack data structure
#define ASURA_PS2_WATER__WORLD_VERT_OFFSET			(0)
#define ASURA_PS2_WATER__VERT_UV_OFFSET				(1)
#define ASURA_PS2_WATER__VERT_RGB_OFFSET			(2)
#define ASURA_PS2_WATER__VERT_TRANS_OFFSET			(3)	// this one is used to save transforming multiple times

// Vertex stack limits
#define ASURA_PS2_WATER__ADDR__VERT_STACK_END  (ASURA_PS2_WATER__ADDR__ST_OVERWRITE_BFR)
#define ASURA_PS2_WATER__ADDR__VERT_STACK_FULL  (ASURA_PS2_WATER__ADDR__VERT_STACK_END - 14)	// if beyond here, cannot continue


// Renderer DEBUG controls


// Compilation controls
#define CAMERA_SPECIFIC_FOV_THRESHOLD	(1)

// E N D   F I L E   W R A P P E R ///////////////////////////////////////////

#endif // #ifndef ___ASURA_PS2_WATERDEFINES_H___
