regs_pipeline.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include "common/assert.h"
  7. #include "common/bit_field.h"
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. namespace Pica {
  11. struct PipelineRegs {
  12. enum class VertexAttributeFormat : u32 {
  13. BYTE = 0,
  14. UBYTE = 1,
  15. SHORT = 2,
  16. FLOAT = 3,
  17. };
  18. struct {
  19. BitField<0, 29, u32> base_address;
  20. PAddr GetPhysicalBaseAddress() const {
  21. return base_address * 8;
  22. }
  23. // Descriptor for internal vertex attributes
  24. union {
  25. BitField<0, 2, VertexAttributeFormat> format0; // size of one element
  26. BitField<2, 2, u32> size0; // number of elements minus 1
  27. BitField<4, 2, VertexAttributeFormat> format1;
  28. BitField<6, 2, u32> size1;
  29. BitField<8, 2, VertexAttributeFormat> format2;
  30. BitField<10, 2, u32> size2;
  31. BitField<12, 2, VertexAttributeFormat> format3;
  32. BitField<14, 2, u32> size3;
  33. BitField<16, 2, VertexAttributeFormat> format4;
  34. BitField<18, 2, u32> size4;
  35. BitField<20, 2, VertexAttributeFormat> format5;
  36. BitField<22, 2, u32> size5;
  37. BitField<24, 2, VertexAttributeFormat> format6;
  38. BitField<26, 2, u32> size6;
  39. BitField<28, 2, VertexAttributeFormat> format7;
  40. BitField<30, 2, u32> size7;
  41. };
  42. union {
  43. BitField<0, 2, VertexAttributeFormat> format8;
  44. BitField<2, 2, u32> size8;
  45. BitField<4, 2, VertexAttributeFormat> format9;
  46. BitField<6, 2, u32> size9;
  47. BitField<8, 2, VertexAttributeFormat> format10;
  48. BitField<10, 2, u32> size10;
  49. BitField<12, 2, VertexAttributeFormat> format11;
  50. BitField<14, 2, u32> size11;
  51. BitField<16, 12, u32> attribute_mask;
  52. // number of total attributes minus 1
  53. BitField<28, 4, u32> max_attribute_index;
  54. };
  55. inline VertexAttributeFormat GetFormat(int n) const {
  56. VertexAttributeFormat formats[] = {format0, format1, format2, format3,
  57. format4, format5, format6, format7,
  58. format8, format9, format10, format11};
  59. return formats[n];
  60. }
  61. inline int GetNumElements(int n) const {
  62. u32 sizes[] = {size0, size1, size2, size3, size4, size5,
  63. size6, size7, size8, size9, size10, size11};
  64. return (int)sizes[n] + 1;
  65. }
  66. inline int GetElementSizeInBytes(int n) const {
  67. return (GetFormat(n) == VertexAttributeFormat::FLOAT)
  68. ? 4
  69. : (GetFormat(n) == VertexAttributeFormat::SHORT) ? 2 : 1;
  70. }
  71. inline int GetStride(int n) const {
  72. return GetNumElements(n) * GetElementSizeInBytes(n);
  73. }
  74. inline bool IsDefaultAttribute(int id) const {
  75. return (id >= 12) || (attribute_mask & (1ULL << id)) != 0;
  76. }
  77. inline int GetNumTotalAttributes() const {
  78. return (int)max_attribute_index + 1;
  79. }
  80. // Attribute loaders map the source vertex data to input attributes
  81. // This e.g. allows to load different attributes from different memory locations
  82. struct {
  83. // Source attribute data offset from the base address
  84. u32 data_offset;
  85. union {
  86. BitField<0, 4, u32> comp0;
  87. BitField<4, 4, u32> comp1;
  88. BitField<8, 4, u32> comp2;
  89. BitField<12, 4, u32> comp3;
  90. BitField<16, 4, u32> comp4;
  91. BitField<20, 4, u32> comp5;
  92. BitField<24, 4, u32> comp6;
  93. BitField<28, 4, u32> comp7;
  94. };
  95. union {
  96. BitField<0, 4, u32> comp8;
  97. BitField<4, 4, u32> comp9;
  98. BitField<8, 4, u32> comp10;
  99. BitField<12, 4, u32> comp11;
  100. // bytes for a single vertex in this loader
  101. BitField<16, 8, u32> byte_count;
  102. BitField<28, 4, u32> component_count;
  103. };
  104. inline int GetComponent(int n) const {
  105. u32 components[] = {comp0, comp1, comp2, comp3, comp4, comp5,
  106. comp6, comp7, comp8, comp9, comp10, comp11};
  107. return (int)components[n];
  108. }
  109. } attribute_loaders[12];
  110. } vertex_attributes;
  111. struct {
  112. enum IndexFormat : u32 {
  113. BYTE = 0,
  114. SHORT = 1,
  115. };
  116. union {
  117. BitField<0, 31, u32> offset; // relative to base attribute address
  118. BitField<31, 1, IndexFormat> format;
  119. };
  120. } index_array;
  121. // Number of vertices to render
  122. u32 num_vertices;
  123. INSERT_PADDING_WORDS(0x1);
  124. // The index of the first vertex to render
  125. u32 vertex_offset;
  126. INSERT_PADDING_WORDS(0x3);
  127. // These two trigger rendering of triangles
  128. u32 trigger_draw;
  129. u32 trigger_draw_indexed;
  130. INSERT_PADDING_WORDS(0x2);
  131. // These registers are used to setup the default "fall-back" vertex shader attributes
  132. struct {
  133. // Index of the current default attribute
  134. u32 index;
  135. // Writing to these registers sets the "current" default attribute.
  136. u32 set_value[3];
  137. } vs_default_attributes_setup;
  138. INSERT_PADDING_WORDS(0x2);
  139. struct {
  140. // There are two channels that can be used to configure the next command buffer, which can
  141. // be then executed by writing to the "trigger" registers. There are two reasons why a game
  142. // might use this feature:
  143. // 1) With this, an arbitrary number of additional command buffers may be executed in
  144. // sequence without requiring any intervention of the CPU after the initial one is
  145. // kicked off.
  146. // 2) Games can configure these registers to provide a command list subroutine mechanism.
  147. BitField<0, 20, u32> size[2]; ///< Size (in bytes / 8) of each channel's command buffer
  148. BitField<0, 28, u32> addr[2]; ///< Physical address / 8 of each channel's command buffer
  149. u32 trigger[2]; ///< Triggers execution of the channel's command buffer when written to
  150. unsigned GetSize(unsigned index) const {
  151. ASSERT(index < 2);
  152. return 8 * size[index];
  153. }
  154. PAddr GetPhysicalAddress(unsigned index) const {
  155. ASSERT(index < 2);
  156. return (PAddr)(8 * addr[index]);
  157. }
  158. } command_buffer;
  159. INSERT_PADDING_WORDS(4);
  160. /// Number of input attributes to the vertex shader minus 1
  161. BitField<0, 4, u32> max_input_attrib_index;
  162. INSERT_PADDING_WORDS(2);
  163. enum class GPUMode : u32 {
  164. Drawing = 0,
  165. Configuring = 1,
  166. };
  167. GPUMode gpu_mode;
  168. INSERT_PADDING_WORDS(0x18);
  169. enum class TriangleTopology : u32 {
  170. List = 0,
  171. Strip = 1,
  172. Fan = 2,
  173. Shader = 3, // Programmable setup unit implemented in a geometry shader
  174. };
  175. BitField<8, 2, TriangleTopology> triangle_topology;
  176. u32 restart_primitive;
  177. INSERT_PADDING_WORDS(0x20);
  178. };
  179. static_assert(sizeof(PipelineRegs) == 0x80 * sizeof(u32), "PipelineRegs struct has incorrect size");
  180. } // namespace Pica