summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/Src/XFStructs.cpp
blob: 670e4bf1887bbe0cb76580f81fd70e8f00f7e98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2003 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#include "Common.h"
#include "VideoCommon.h"
#include "XFMemory.h"
#include "CPMemory.h"
#include "NativeVertexWriter.h"
#include "VertexShaderManager.h"
#include "PixelShaderManager.h"

// LoadXFReg 0x10
void LoadXFReg(u32 transferSize, u32 baseAddress, u32 *pData)
{
    u32 address = baseAddress;
    for (int i = 0; i < (int)transferSize; i++)
    {
        address = baseAddress + i;

        // Setup a Matrix
        if (address < XFMEM_ERROR)
        {
            VertexManager::Flush();
			VertexShaderManager::InvalidateXFRange(address, address + transferSize);
            //PRIM_LOG("xfmem write: 0x%x-0x%x\n", address, address+transferSize);

            memcpy_gc((u32*)&xfmem[address], &pData[i], transferSize*4);
            i += transferSize;
        }
		else if (address >= XFMEM_SETTEXMTXINFO && address <= XFMEM_SETTEXMTXINFO+7)
		{
			xfregs.texcoords[address - XFMEM_SETTEXMTXINFO].texmtxinfo.hex = pData[i];
		}
		else if (address >= XFMEM_SETPOSMTXINFO && address <= XFMEM_SETPOSMTXINFO+7)
		{
			xfregs.texcoords[address - XFMEM_SETPOSMTXINFO].postmtxinfo.hex = pData[i];
		}
		else if (address >= XFMEM_SETVIEWPORT && address <= XFMEM_SETVIEWPORT+5)
		{
			VertexManager::Flush();
			u32 Index = address - XFMEM_SETVIEWPORT;
            VertexShaderManager::SetViewport((float*)&pData[i],Index);
			PixelShaderManager::SetViewport((float*)&pData[i],Index);
            if(Index == 0)
			{
				i += 5;
			}

		}
		else if (address >= XFMEM_SETPROJECTION && address <= XFMEM_SETPROJECTION+7)
		{
			VertexManager::Flush();
			u32 Index = address - XFMEM_SETPROJECTION;
            VertexShaderManager::SetProjection((float*)&pData[i],Index);
			if(Index == 0)
			{
				i += 7;
			}			
		}
        else if (address < 0x2000)
        {
            u32 data = pData[i];
            switch (address)
            {
            case XFMEM_ERROR:
            case XFMEM_DIAG:
            case XFMEM_STATE0: // internal state 0
            case XFMEM_STATE1: // internal state 1
            case XFMEM_CLOCK:
			case XFMEM_SETGPMETRIC:
                break;

            case XFMEM_CLIPDISABLE:
				//if (data & 1) {} // disable clipping detection
				//if (data & 2) {} // disable trivial rejection
				//if (data & 4) {} // disable cpoly clipping acceleration
                break;

            case XFMEM_VTXSPECS: //__GXXfVtxSpecs, wrote 0004
                xfregs.hostinfo = *(INVTXSPEC*)&data;
                break;

            case XFMEM_SETNUMCHAN:
				if ((u32)xfregs.nNumChans != (data & 3))
				{
                    VertexManager::Flush();
                    xfregs.nNumChans = data & 3;
                }
                break;

            case XFMEM_SETCHAN0_AMBCOLOR: // Channel Ambient Color
			case XFMEM_SETCHAN1_AMBCOLOR:
				{
					u8 chan = address - XFMEM_SETCHAN0_AMBCOLOR;
					if (xfregs.colChans[chan].ambColor != data) 
					{
						VertexManager::Flush();
						xfregs.colChans[chan].ambColor = data;
						VertexShaderManager::SetMaterialColor(chan, data);
					}
					break;
				}

            case XFMEM_SETCHAN0_MATCOLOR: // Channel Material Color
			case XFMEM_SETCHAN1_MATCOLOR:
				{
					u8 chan = address - XFMEM_SETCHAN0_MATCOLOR;
					if (xfregs.colChans[chan].matColor != data)
					{
						VertexManager::Flush();
						xfregs.colChans[chan].matColor = data;
						VertexShaderManager::SetMaterialColor(address - XFMEM_SETCHAN0_AMBCOLOR, data);
					}
					break;
				}

            case XFMEM_SETCHAN0_COLOR: // Channel Color
			case XFMEM_SETCHAN1_COLOR:
				{
					u8 chan = address - XFMEM_SETCHAN0_COLOR;
					if (xfregs.colChans[chan].color.hex != (data & 0x7fff))
					{
						VertexManager::Flush();
						xfregs.colChans[chan].color.hex = data;
					}
					break;
				}

            case XFMEM_SETCHAN0_ALPHA: // Channel Alpha
			case XFMEM_SETCHAN1_ALPHA:
				{
					u8 chan = address - XFMEM_SETCHAN0_ALPHA;
					if (xfregs.colChans[chan].alpha.hex != (data & 0x7fff))
					{
						VertexManager::Flush();
						xfregs.colChans[chan].alpha.hex = data;
					}
					break;
				}

            case XFMEM_DUALTEX:
                if (xfregs.bEnableDualTexTransform != (data & 1)) 
				{
                    VertexManager::Flush();
                    xfregs.bEnableDualTexTransform = data & 1;
                }
                break;


            case XFMEM_SETMATRIXINDA:
                //_assert_msg_(GX_XF, 0, "XF matrixindex0");
				VertexShaderManager::SetTexMatrixChangedA(data); // ?
                break;
            case XFMEM_SETMATRIXINDB:
                //_assert_msg_(GX_XF, 0, "XF matrixindex1");
                VertexShaderManager::SetTexMatrixChangedB(data); // ?
                break;
            case XFMEM_SETNUMTEXGENS: // GXSetNumTexGens
                if ((u32)xfregs.numTexGens != data)
				{
                    VertexManager::Flush();
                    xfregs.numTexGens = data;
                }
                break;			

			// --------------
			// Unknown Regs
			// --------------

			// Maybe these are for Normals?
			case 0x1048: //xfregs.texcoords[0].nrmmtxinfo.hex = data; break; ??
            case 0x1049:
            case 0x104a:
            case 0x104b:
            case 0x104c:
            case 0x104d:
            case 0x104e:
            case 0x104f:
				DEBUG_LOG(VIDEO, "Possible Normal Mtx XF reg?: %x=%x\n", address, data);
				break;

			case 0x1013:
            case 0x1014:
            case 0x1015:
            case 0x1016:
            case 0x1017:

            default:
                WARN_LOG(VIDEO, "Unknown XF Reg: %x=%x\n", address, data);
                break;
            }
        }
    }
}

// TODO - verify that it is correct. Seems to work, though.
void LoadIndexedXF(u32 val, int array)
{
    int index = val >> 16;
    int address = val & 0xFFF; // check mask
    int size = ((val >> 12) & 0xF) + 1;
    //load stuff from array to address in xf mem

    VertexManager::Flush();
    VertexShaderManager::InvalidateXFRange(address, address+size);
    //PRIM_LOG("xfmem iwrite: 0x%x-0x%x\n", address, address+size);

    for (int i = 0; i < size; i++)
        xfmem[address + i] = Memory_Read_U32(arraybases[array] + arraystrides[array] * index + i * 4);
}