Pica: Implement vertex loading.

This commit is contained in:
Tony Wasserka 2014-07-26 16:19:11 +02:00
parent 1a43f69477
commit d443f0a921
2 changed files with 102 additions and 8 deletions

View File

@ -4,6 +4,7 @@
#include "pica.h" #include "pica.h"
#include "command_processor.h" #include "command_processor.h"
#include "math.h"
namespace Pica { namespace Pica {
@ -17,7 +18,85 @@ static inline void WritePicaReg(u32 id, u32 value) {
registers[id] = value; registers[id] = value;
switch(id) { switch(id) {
// TODO: Perform actions for anything which requires special treatment here... // It seems like these trigger vertex rendering
case PICA_REG_INDEX(trigger_draw):
case PICA_REG_INDEX(trigger_draw_indexed):
{
const auto& attribute_config = registers.vertex_attributes;
const u8* const base_address = Memory::GetPointer(attribute_config.GetBaseAddress());
// Information about internal vertex attributes
const u8* vertex_attribute_sources[16];
u32 vertex_attribute_strides[16];
u32 vertex_attribute_formats[16];
u32 vertex_attribute_elements[16];
u32 vertex_attribute_element_size[16];
// Setup attribute data from loaders
for (int loader = 0; loader < 12; ++loader) {
const auto& loader_config = attribute_config.attribute_loaders[loader];
const u8* load_address = base_address + loader_config.data_offset;
// TODO: What happens if a loader overwrites a previous one's data?
for (int component = 0; component < loader_config.component_count; ++component) {
u32 attribute_index = loader_config.GetComponent(component);
vertex_attribute_sources[attribute_index] = load_address;
vertex_attribute_strides[attribute_index] = loader_config.byte_count;
vertex_attribute_formats[attribute_index] = (u32)attribute_config.GetFormat(attribute_index);
vertex_attribute_elements[attribute_index] = attribute_config.GetNumElements(attribute_index);
vertex_attribute_element_size[attribute_index] = attribute_config.GetElementSizeInBytes(attribute_index);
load_address += attribute_config.GetStride(attribute_index);
}
}
// Load vertices
bool is_indexed = (id == PICA_REG_INDEX(trigger_draw_indexed));
const auto& index_info = registers.index_array;
const u8* index_address_8 = (u8*)base_address + index_info.offset;
const u16* index_address_16 = (u16*)index_address_8;
bool index_u16 = (bool)index_info.format;
for (int index = 0; index < registers.num_vertices; ++index)
{
int vertex = is_indexed ? (index_u16 ? index_address_16[index] : index_address_8[index]) : index;
if (is_indexed) {
// TODO: Implement some sort of vertex cache!
}
// Initialize data for the current vertex
struct {
Math::Vec4<float24> attr[16];
} input;
for (int i = 0; i < attribute_config.GetNumTotalAttributes(); ++i) {
for (int comp = 0; comp < vertex_attribute_elements[i]; ++comp) {
const u8* srcdata = vertex_attribute_sources[i] + vertex_attribute_strides[i] * vertex + comp * vertex_attribute_element_size[i];
const float srcval = (vertex_attribute_formats[i] == 0) ? *(s8*)srcdata :
(vertex_attribute_formats[i] == 1) ? *(u8*)srcdata :
(vertex_attribute_formats[i] == 2) ? *(s16*)srcdata :
*(float*)srcdata;
input.attr[i][comp] = float24::FromFloat32(srcval);
DEBUG_LOG(GPU, "Loaded component %x of attribute %x for vertex %x (index %x) from 0x%08x + 0x%08x + 0x%04x: %f",
comp, i, vertex, index,
attribute_config.GetBaseAddress(),
vertex_attribute_sources[i] - base_address,
srcdata - vertex_attribute_sources[i],
input.attr[i][comp].ToFloat32());
}
}
// TODO: Run vertex data through vertex shader
if (is_indexed) {
// TODO: Add processed vertex to vertex cache!
}
// TODO: Submit vertex to primitive assembly
}
break;
}
default: default:
break; break;

View File

@ -64,7 +64,7 @@ struct Regs {
inline u32 GetBaseAddress() const { inline u32 GetBaseAddress() const {
// TODO: Ugly, should fix PhysicalToVirtualAddress instead // TODO: Ugly, should fix PhysicalToVirtualAddress instead
return (base_address * 8) - Memory::FCRAM_PADDR + Memory::HEAP_GSP_VADDR; return DecodeAddressRegister(base_address) - Memory::FCRAM_PADDR + Memory::HEAP_GSP_VADDR;
} }
// Descriptor for internal vertex attributes // Descriptor for internal vertex attributes
@ -110,12 +110,12 @@ struct Regs {
} }
inline int GetNumElements(int n) const { inline int GetNumElements(int n) const {
int sizes[] = { u64 sizes[] = {
size0, size1, size2, size3, size0, size1, size2, size3,
size4, size5, size6, size7, size4, size5, size6, size7,
size8, size9, size10, size11 size8, size9, size10, size11
}; };
return sizes[n]+1; return (int)sizes[n]+1;
} }
inline int GetElementSizeInBytes(int n) const { inline int GetElementSizeInBytes(int n) const {
@ -128,7 +128,7 @@ struct Regs {
} }
inline int GetNumTotalAttributes() const { inline int GetNumTotalAttributes() const {
return num_extra_attributes+1; return (int)num_extra_attributes+1;
} }
// Attribute loaders map the source vertex data to input attributes // Attribute loaders map the source vertex data to input attributes
@ -158,12 +158,12 @@ struct Regs {
}; };
inline int GetComponent(int n) const { inline int GetComponent(int n) const {
int components[] = { u64 components[] = {
comp0, comp1, comp2, comp3, comp0, comp1, comp2, comp3,
comp4, comp5, comp6, comp7, comp4, comp5, comp6, comp7,
comp8, comp9, comp10, comp11 comp8, comp9, comp10, comp11
}; };
return components[n]; return (int)components[n];
} }
} attribute_loaders[12]; } attribute_loaders[12];
} vertex_attributes; } vertex_attributes;
@ -180,7 +180,16 @@ struct Regs {
}; };
} index_array; } index_array;
INSERT_PADDING_WORDS(0xd8); // Number of vertices to render
u32 num_vertices;
INSERT_PADDING_WORDS(0x5);
// These two trigger rendering of triangles
u32 trigger_draw;
u32 trigger_draw_indexed;
INSERT_PADDING_WORDS(0xd0);
#undef INSERT_PADDING_WORDS_HELPER1 #undef INSERT_PADDING_WORDS_HELPER1
#undef INSERT_PADDING_WORDS_HELPER2 #undef INSERT_PADDING_WORDS_HELPER2
@ -207,6 +216,9 @@ struct Regs {
ADD_FIELD(viewport_size_y); ADD_FIELD(viewport_size_y);
ADD_FIELD(vertex_attributes); ADD_FIELD(vertex_attributes);
ADD_FIELD(index_array); ADD_FIELD(index_array);
ADD_FIELD(num_vertices);
ADD_FIELD(trigger_draw);
ADD_FIELD(trigger_draw_indexed);
#undef ADD_FIELD #undef ADD_FIELD
#endif // _MSC_VER #endif // _MSC_VER
@ -249,6 +261,9 @@ ASSERT_REG_POSITION(viewport_size_x, 0x41);
ASSERT_REG_POSITION(viewport_size_y, 0x43); ASSERT_REG_POSITION(viewport_size_y, 0x43);
ASSERT_REG_POSITION(vertex_attributes, 0x200); ASSERT_REG_POSITION(vertex_attributes, 0x200);
ASSERT_REG_POSITION(index_array, 0x227); ASSERT_REG_POSITION(index_array, 0x227);
ASSERT_REG_POSITION(num_vertices, 0x228);
ASSERT_REG_POSITION(trigger_draw, 0x22e);
ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
#undef ASSERT_REG_POSITION #undef ASSERT_REG_POSITION
#endif // !defined(_MSC_VER) #endif // !defined(_MSC_VER)