shader_header.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2018 yuzu 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 <optional>
  7. #include "common/bit_field.h"
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. namespace Tegra::Shader {
  11. enum class OutputTopology : u32 {
  12. PointList = 1,
  13. LineStrip = 6,
  14. TriangleStrip = 7,
  15. };
  16. enum class PixelImap : u8 {
  17. Unused = 0,
  18. Constant = 1,
  19. Perspective = 2,
  20. ScreenLinear = 3,
  21. };
  22. // Documentation in:
  23. // http://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html
  24. struct Header {
  25. union {
  26. BitField<0, 5, u32> sph_type;
  27. BitField<5, 5, u32> version;
  28. BitField<10, 4, u32> shader_type;
  29. BitField<14, 1, u32> mrt_enable;
  30. BitField<15, 1, u32> kills_pixels;
  31. BitField<16, 1, u32> does_global_store;
  32. BitField<17, 4, u32> sass_version;
  33. BitField<21, 5, u32> reserved;
  34. BitField<26, 1, u32> does_load_or_store;
  35. BitField<27, 1, u32> does_fp64;
  36. BitField<28, 4, u32> stream_out_mask;
  37. } common0;
  38. union {
  39. BitField<0, 24, u32> shader_local_memory_low_size;
  40. BitField<24, 8, u32> per_patch_attribute_count;
  41. } common1;
  42. union {
  43. BitField<0, 24, u32> shader_local_memory_high_size;
  44. BitField<24, 8, u32> threads_per_input_primitive;
  45. } common2;
  46. union {
  47. BitField<0, 24, u32> shader_local_memory_crs_size;
  48. BitField<24, 4, OutputTopology> output_topology;
  49. BitField<28, 4, u32> reserved;
  50. } common3;
  51. union {
  52. BitField<0, 12, u32> max_output_vertices;
  53. BitField<12, 8, u32> store_req_start; // NOTE: not used by geometry shaders.
  54. BitField<20, 4, u32> reserved;
  55. BitField<24, 8, u32> store_req_end; // NOTE: not used by geometry shaders.
  56. } common4;
  57. union {
  58. struct {
  59. INSERT_UNION_PADDING_BYTES(3); // ImapSystemValuesA
  60. INSERT_UNION_PADDING_BYTES(1); // ImapSystemValuesB
  61. INSERT_UNION_PADDING_BYTES(16); // ImapGenericVector[32]
  62. INSERT_UNION_PADDING_BYTES(2); // ImapColor
  63. union {
  64. BitField<0, 8, u16> clip_distances;
  65. BitField<8, 1, u16> point_sprite_s;
  66. BitField<9, 1, u16> point_sprite_t;
  67. BitField<10, 1, u16> fog_coordinate;
  68. BitField<12, 1, u16> tessellation_eval_point_u;
  69. BitField<13, 1, u16> tessellation_eval_point_v;
  70. BitField<14, 1, u16> instance_id;
  71. BitField<15, 1, u16> vertex_id;
  72. };
  73. INSERT_UNION_PADDING_BYTES(5); // ImapFixedFncTexture[10]
  74. INSERT_UNION_PADDING_BYTES(1); // ImapReserved
  75. INSERT_UNION_PADDING_BYTES(3); // OmapSystemValuesA
  76. INSERT_UNION_PADDING_BYTES(1); // OmapSystemValuesB
  77. INSERT_UNION_PADDING_BYTES(16); // OmapGenericVector[32]
  78. INSERT_UNION_PADDING_BYTES(2); // OmapColor
  79. INSERT_UNION_PADDING_BYTES(2); // OmapSystemValuesC
  80. INSERT_UNION_PADDING_BYTES(5); // OmapFixedFncTexture[10]
  81. INSERT_UNION_PADDING_BYTES(1); // OmapReserved
  82. } vtg;
  83. struct {
  84. INSERT_UNION_PADDING_BYTES(3); // ImapSystemValuesA
  85. INSERT_UNION_PADDING_BYTES(1); // ImapSystemValuesB
  86. union {
  87. BitField<0, 2, PixelImap> x;
  88. BitField<2, 2, PixelImap> y;
  89. BitField<4, 2, PixelImap> z;
  90. BitField<6, 2, PixelImap> w;
  91. u8 raw;
  92. } imap_generic_vector[32];
  93. INSERT_UNION_PADDING_BYTES(2); // ImapColor
  94. INSERT_UNION_PADDING_BYTES(2); // ImapSystemValuesC
  95. INSERT_UNION_PADDING_BYTES(10); // ImapFixedFncTexture[10]
  96. INSERT_UNION_PADDING_BYTES(2); // ImapReserved
  97. struct {
  98. u32 target;
  99. union {
  100. BitField<0, 1, u32> sample_mask;
  101. BitField<1, 1, u32> depth;
  102. BitField<2, 30, u32> reserved;
  103. };
  104. } omap;
  105. bool IsColorComponentOutputEnabled(u32 render_target, u32 component) const {
  106. const u32 bit = render_target * 4 + component;
  107. return omap.target & (1 << bit);
  108. }
  109. PixelImap GetPixelImap(u32 attribute) const {
  110. const auto get_index = [this, attribute](u32 index) {
  111. return static_cast<PixelImap>(
  112. (imap_generic_vector[attribute].raw >> (index * 2)) & 3);
  113. };
  114. std::optional<PixelImap> result;
  115. for (u32 component = 0; component < 4; ++component) {
  116. const PixelImap index = get_index(component);
  117. if (index == PixelImap::Unused) {
  118. continue;
  119. }
  120. if (result && result != index) {
  121. LOG_CRITICAL(HW_GPU, "Generic attribute conflict in interpolation mode");
  122. }
  123. result = index;
  124. }
  125. return result.value_or(PixelImap::Unused);
  126. }
  127. } ps;
  128. std::array<u32, 0xF> raw;
  129. };
  130. u64 GetLocalMemorySize() const {
  131. return (common1.shader_local_memory_low_size |
  132. (common2.shader_local_memory_high_size << 24));
  133. }
  134. };
  135. static_assert(sizeof(Header) == 0x50, "Incorrect structure size");
  136. } // namespace Tegra::Shader