summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/Src/VertexLoaderManager.cpp
blob: 176e9ad0c71779b703ab253dc782a8025c7fa440 (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
// 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 <algorithm>
#ifdef _MSC_VER
#include <hash_map>
using stdext::hash_map;
#else
#include <ext/hash_map>
using __gnu_cxx::hash_map;
#endif
#include <map>
#include <vector>

#include "VideoCommon.h"
#include "Statistics.h"

#include "VertexShaderManager.h"
#include "VertexLoader.h"
#include "VertexLoaderManager.h"

static int s_attr_dirty;  // bitfield

static VertexLoader *g_VertexLoaders[8];

#ifdef _MSC_VER
namespace stdext {
	inline size_t hash_value(const VertexLoaderUID& uid) {
		return uid.GetHash();
	}
}
#else
namespace __gnu_cxx {
	template<> struct hash<VertexLoaderUID> {
		size_t operator()(const VertexLoaderUID& uid) const {
			return uid.GetHash();
		}
	};
}
#endif

namespace VertexLoaderManager
{

typedef hash_map<VertexLoaderUID, VertexLoader*> VertexLoaderMap;
static VertexLoaderMap g_VertexLoaderMap;
// TODO - change into array of pointers. Keep a map of all seen so far.

void Init()
{
	MarkAllDirty();
	for (int i = 0; i < 8; i++)
		g_VertexLoaders[i] = NULL;
	RecomputeCachedArraybases();
}

void Shutdown()
{
	for (VertexLoaderMap::iterator iter = g_VertexLoaderMap.begin(); iter != g_VertexLoaderMap.end(); ++iter)
	{
		delete iter->second;
	}
	g_VertexLoaderMap.clear();
}

namespace {
struct entry {
	std::string text;
	u64 num_verts;
	bool operator < (const entry &other) const {
		return num_verts > other.num_verts;
	}
};
}

void AppendListToString(std::string *dest)
{
	std::vector<entry> entries;

	size_t total_size = 0;
	for (VertexLoaderMap::const_iterator iter = g_VertexLoaderMap.begin(); iter != g_VertexLoaderMap.end(); ++iter)
	{
		entry e;
		iter->second->AppendToString(&e.text);
		e.num_verts = iter->second->GetNumLoadedVerts();
		entries.push_back(e);
		total_size += e.text.size() + 1;
	}
	sort(entries.begin(), entries.end());
	dest->reserve(dest->size() + total_size);
	for (std::vector<entry>::const_iterator iter = entries.begin(); iter != entries.end(); ++iter) {
		dest->append(iter->text);
	}
}

void MarkAllDirty()
{
	s_attr_dirty = 0xff;
}

static void RefreshLoader(int vtx_attr_group)
{
	if ((s_attr_dirty >> vtx_attr_group) & 1)
	{
		VertexLoaderUID uid;
		uid.InitFromCurrentState(vtx_attr_group);
		VertexLoaderMap::iterator iter = g_VertexLoaderMap.find(uid);
		if (iter != g_VertexLoaderMap.end())
		{
			g_VertexLoaders[vtx_attr_group] = iter->second;
		}
		else
		{
			VertexLoader *loader = new VertexLoader(g_VtxDesc, g_VtxAttr[vtx_attr_group]);
			g_VertexLoaderMap[uid] = loader;
			g_VertexLoaders[vtx_attr_group] = loader;
			INCSTAT(stats.numVertexLoaders);
		}
	}
	s_attr_dirty &= ~(1 << vtx_attr_group);
}

void RunVertices(int vtx_attr_group, int primitive, int count)
{
	if (!count)
		return;

	RefreshLoader(vtx_attr_group);
	g_VertexLoaders[vtx_attr_group]->RunVertices(vtx_attr_group, primitive, count);
}

int GetVertexSize(int vtx_attr_group)
{
	RefreshLoader(vtx_attr_group);
	return g_VertexLoaders[vtx_attr_group]->GetVertexSize();
}

}  // namespace

void LoadCPReg(u32 sub_cmd, u32 value)
{
	switch (sub_cmd & 0xF0)
	{
	case 0x30:
		VertexShaderManager::SetTexMatrixChangedA(value);
		break;

	case 0x40:
		VertexShaderManager::SetTexMatrixChangedB(value);
		break;

	case 0x50:
		g_VtxDesc.Hex &= ~0x1FFFF;  // keep the Upper bits
		g_VtxDesc.Hex |= value;
		s_attr_dirty = 0xFF;
		break;

	case 0x60:
		g_VtxDesc.Hex &= 0x1FFFF;  // keep the lower 17Bits
		g_VtxDesc.Hex |= (u64)value << 17;
		s_attr_dirty = 0xFF;
		break;

	case 0x70:
		_assert_((sub_cmd & 0x0F) < 8);
		g_VtxAttr[sub_cmd & 7].g0.Hex = value;
		s_attr_dirty |= 1 << (sub_cmd & 7);
		break;

	case 0x80:
		_assert_((sub_cmd & 0x0F) < 8);
		g_VtxAttr[sub_cmd & 7].g1.Hex = value;
		s_attr_dirty |= 1 << (sub_cmd & 7);
		break;

	case 0x90:
		_assert_((sub_cmd & 0x0F) < 8);
		g_VtxAttr[sub_cmd & 7].g2.Hex = value;
		s_attr_dirty |= 1 << (sub_cmd & 7);
		break;

	// Pointers to vertex arrays in GC RAM
	case 0xA0:
		arraybases[sub_cmd & 0xF] = value;
		cached_arraybases[sub_cmd & 0xF] = Memory_GetPtr(value);
		break;

	case 0xB0:
		arraystrides[sub_cmd & 0xF] = value & 0xFF;
		break;
	}
}

void RecomputeCachedArraybases()
{
	for (int i = 0; i < 16; i++)
	{
		cached_arraybases[i] = Memory_GetPtr(arraybases[i]);
	}
}