| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-06 02:07:48 +00:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-06-05 17:55:21 +02:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2012-12-10 00:40:28 -06:00
										 |  |  | #include <unordered_map>
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Core/HW/Memmap.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "VideoCommon/Statistics.h"
 | 
					
						
							|  |  |  | #include "VideoCommon/VertexLoader.h"
 | 
					
						
							|  |  |  | #include "VideoCommon/VertexLoaderManager.h"
 | 
					
						
							|  |  |  | #include "VideoCommon/VertexShaderManager.h"
 | 
					
						
							|  |  |  | #include "VideoCommon/VideoCommon.h"
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int s_attr_dirty;  // bitfield
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static VertexLoader *g_VertexLoaders[8]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-10 08:40:27 -06:00
										 |  |  | namespace std | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-01 19:24:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | template <> | 
					
						
							|  |  |  | struct hash<VertexLoaderUID> | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	size_t operator()(const VertexLoaderUID& uid) const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return uid.GetHash(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-06 02:07:48 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2012-12-10 08:40:27 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-01 19:24:27 -05:00
										 |  |  | typedef std::unordered_map<VertexLoaderUID, VertexLoader*> VertexLoaderMap; | 
					
						
							| 
									
										
										
										
											2014-06-05 17:55:21 +02:00
										 |  |  | typedef std::map<PortableVertexDeclaration, std::unique_ptr<NativeVertexFormat>> NativeVertexLoaderMap; | 
					
						
							| 
									
										
										
										
											2010-03-06 02:07:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | namespace VertexLoaderManager | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static VertexLoaderMap g_VertexLoaderMap; | 
					
						
							| 
									
										
										
										
											2014-06-05 17:55:21 +02:00
										 |  |  | static NativeVertexLoaderMap s_native_vertex_map; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | // TODO - change into array of pointers. Keep a map of all seen so far.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Init() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MarkAllDirty(); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 	for (VertexLoader*& vertexLoader : g_VertexLoaders) | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		vertexLoader = nullptr; | 
					
						
							| 
									
										
										
										
											2009-04-24 16:40:58 +00:00
										 |  |  | 	RecomputeCachedArraybases(); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Shutdown() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 	for (auto& p : g_VertexLoaderMap) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-10-29 01:09:01 -04:00
										 |  |  | 		delete p.second; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	g_VertexLoaderMap.clear(); | 
					
						
							| 
									
										
										
										
											2014-06-05 17:55:21 +02:00
										 |  |  | 	s_native_vertex_map.clear(); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 09:21:54 -04:00
										 |  |  | namespace | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | struct entry | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	std::string text; | 
					
						
							|  |  |  | 	u64 num_verts; | 
					
						
							| 
									
										
										
										
											2013-04-24 09:21:54 -04:00
										 |  |  | 	bool operator < (const entry &other) const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		return num_verts > other.num_verts; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AppendListToString(std::string *dest) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::vector<entry> entries; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t total_size = 0; | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 	for (const auto& map_entry : g_VertexLoaderMap) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		entry e; | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		map_entry.second->AppendToString(&e.text); | 
					
						
							|  |  |  | 		e.num_verts = map_entry.second->GetNumLoadedVerts(); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		entries.push_back(e); | 
					
						
							|  |  |  | 		total_size += e.text.size() + 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sort(entries.begin(), entries.end()); | 
					
						
							|  |  |  | 	dest->reserve(dest->size() + total_size); | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 	for (const entry& entry : entries) | 
					
						
							| 
									
										
										
										
											2013-04-24 09:21:54 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-02-12 16:00:34 +01:00
										 |  |  | 		dest->append(entry.text); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MarkAllDirty() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	s_attr_dirty = 0xff; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 14:48:23 +01:00
										 |  |  | static VertexLoader* RefreshLoader(int vtx_attr_group) | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2014-01-30 14:48:23 +01:00
										 |  |  | 	return g_VertexLoaders[vtx_attr_group]; | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RunVertices(int vtx_attr_group, int primitive, int count) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!count) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-01-30 14:48:23 +01:00
										 |  |  | 	RefreshLoader(vtx_attr_group)->RunVertices(vtx_attr_group, primitive, count); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-08 01:39:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | int GetVertexSize(int vtx_attr_group) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-30 14:48:23 +01:00
										 |  |  | 	return RefreshLoader(vtx_attr_group)->GetVertexSize(); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-05 17:55:21 +02:00
										 |  |  | NativeVertexFormat* GetNativeVertexFormat(const PortableVertexDeclaration& format, u32 components) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	auto& native = s_native_vertex_map[format]; | 
					
						
							|  |  |  | 	if (!native) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto raw_pointer = g_vertex_manager->CreateNativeVertexFormat(); | 
					
						
							|  |  |  | 		native = std::unique_ptr<NativeVertexFormat>(raw_pointer); | 
					
						
							|  |  |  | 		native->m_components = components; | 
					
						
							|  |  |  | 		native->Initialize(format); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return native.get(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | }  // 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; | 
					
						
							| 
									
										
										
										
											2011-01-31 01:28:32 +00:00
										 |  |  | 		cached_arraybases[sub_cmd & 0xF] = Memory::GetPointer(value); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case 0xB0: | 
					
						
							|  |  |  | 		arraystrides[sub_cmd & 0xF] = value & 0xFF; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-27 02:55:08 +00:00
										 |  |  | void FillCPMemoryArray(u32 *memory) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memory[0x30] = MatrixIndexA.Hex; | 
					
						
							|  |  |  | 	memory[0x40] = MatrixIndexB.Hex; | 
					
						
							|  |  |  | 	memory[0x50] = (u32)g_VtxDesc.Hex; | 
					
						
							|  |  |  | 	memory[0x60] = (u32)(g_VtxDesc.Hex >> 17); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 8; ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		memory[0x70 + i] = g_VtxAttr[i].g0.Hex; | 
					
						
							|  |  |  | 		memory[0x80 + i] = g_VtxAttr[i].g1.Hex; | 
					
						
							|  |  |  | 		memory[0x90 + i] = g_VtxAttr[i].g2.Hex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < 16; ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		memory[0xA0 + i] = arraybases[i]; | 
					
						
							|  |  |  | 		memory[0xB0 + i] = arraystrides[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | void RecomputeCachedArraybases() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (int i = 0; i < 16; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-01-31 01:28:32 +00:00
										 |  |  | 		cached_arraybases[i] = Memory::GetPointer(arraybases[i]); | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |