tu: Parse multiview render pass info
[mesa.git] / src / freedreno / vulkan / tu_util.c
index 311c3a1b141e95148bfc09f718ec8bd776c7ef96..ba1e4d53cd6cc4158d5821eb050f277e60650c4b 100644 (file)
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  */
 
+#include "tu_private.h"
+
 #include <assert.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "tu_private.h"
+#include "util/u_math.h"
 #include "vk_enum_to_str.h"
 
-#include "util/u_math.h"
+/* TODO: Add Android support to tu_log funcs */
+
+/** \see tu_loge() */
+static void
+tu_loge_v(const char *format, va_list va)
+{
+   fprintf(stderr, "vk: error: ");
+   vfprintf(stderr, format, va);
+   fprintf(stderr, "\n");
+}
+
 
 /** Log an error message.  */
 void tu_printflike(1, 2) tu_loge(const char *format, ...)
@@ -43,11 +55,11 @@ void tu_printflike(1, 2) tu_loge(const char *format, ...)
    va_end(va);
 }
 
-/** \see tu_loge() */
-void
-tu_loge_v(const char *format, va_list va)
+/** \see tu_logi() */
+static void
+tu_logi_v(const char *format, va_list va)
 {
-   fprintf(stderr, "vk: error: ");
+   fprintf(stderr, "tu: info: ");
    vfprintf(stderr, format, va);
    fprintf(stderr, "\n");
 }
@@ -62,17 +74,8 @@ void tu_printflike(1, 2) tu_logi(const char *format, ...)
    va_end(va);
 }
 
-/** \see tu_logi() */
-void
-tu_logi_v(const char *format, va_list va)
-{
-   fprintf(stderr, "tu: info: ");
-   vfprintf(stderr, format, va);
-   fprintf(stderr, "\n");
-}
-
 void tu_printflike(3, 4)
-  __tu_finishme(const char *file, int line, const char *format, ...)
+   __tu_finishme(const char *file, int line, const char *format, ...)
 {
    va_list ap;
    char buffer[256];
@@ -113,3 +116,133 @@ __vk_errorf(struct tu_instance *instance,
 
    return error;
 }
+
+static void
+tu_tiling_config_update_tile_layout(struct tu_framebuffer *fb,
+                                    const struct tu_device *dev,
+                                    const struct tu_render_pass *pass)
+{
+   const uint32_t tile_align_w = pass->tile_align_w;
+   const uint32_t max_tile_width = 1024;
+
+   /* start from 1 tile */
+   fb->tile_count = (VkExtent2D) {
+      .width = 1,
+      .height = 1,
+   };
+   fb->tile0 = (VkExtent2D) {
+      .width = util_align_npot(fb->width, tile_align_w),
+      .height = align(fb->height, TILE_ALIGN_H),
+   };
+
+   if (unlikely(dev->physical_device->instance->debug_flags & TU_DEBUG_FORCEBIN)) {
+      /* start with 2x2 tiles */
+      fb->tile_count.width = 2;
+      fb->tile_count.height = 2;
+      fb->tile0.width = util_align_npot(DIV_ROUND_UP(fb->width, 2), tile_align_w);
+      fb->tile0.height = align(DIV_ROUND_UP(fb->height, 2), TILE_ALIGN_H);
+   }
+
+   /* do not exceed max tile width */
+   while (fb->tile0.width > max_tile_width) {
+      fb->tile_count.width++;
+      fb->tile0.width =
+         util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
+   }
+
+   /* will force to sysmem, don't bother trying to have a valid tile config
+    * TODO: just skip all GMEM stuff when sysmem is forced?
+    */
+   if (!pass->gmem_pixels)
+      return;
+
+   /* do not exceed gmem size */
+   while (fb->tile0.width * fb->tile0.height > pass->gmem_pixels) {
+      if (fb->tile0.width > MAX2(tile_align_w, fb->tile0.height)) {
+         fb->tile_count.width++;
+         fb->tile0.width =
+            util_align_npot(DIV_ROUND_UP(fb->width, fb->tile_count.width), tile_align_w);
+      } else {
+         /* if this assert fails then layout is impossible.. */
+         assert(fb->tile0.height > TILE_ALIGN_H);
+         fb->tile_count.height++;
+         fb->tile0.height =
+            align(DIV_ROUND_UP(fb->height, fb->tile_count.height), TILE_ALIGN_H);
+      }
+   }
+}
+
+static void
+tu_tiling_config_update_pipe_layout(struct tu_framebuffer *fb,
+                                    const struct tu_device *dev)
+{
+   const uint32_t max_pipe_count = 32; /* A6xx */
+
+   /* start from 1 tile per pipe */
+   fb->pipe0 = (VkExtent2D) {
+      .width = 1,
+      .height = 1,
+   };
+   fb->pipe_count = fb->tile_count;
+
+   while (fb->pipe_count.width * fb->pipe_count.height > max_pipe_count) {
+      if (fb->pipe0.width < fb->pipe0.height) {
+         fb->pipe0.width += 1;
+         fb->pipe_count.width =
+            DIV_ROUND_UP(fb->tile_count.width, fb->pipe0.width);
+      } else {
+         fb->pipe0.height += 1;
+         fb->pipe_count.height =
+            DIV_ROUND_UP(fb->tile_count.height, fb->pipe0.height);
+      }
+   }
+}
+
+static void
+tu_tiling_config_update_pipes(struct tu_framebuffer *fb,
+                              const struct tu_device *dev)
+{
+   const uint32_t max_pipe_count = 32; /* A6xx */
+   const uint32_t used_pipe_count =
+      fb->pipe_count.width * fb->pipe_count.height;
+   const VkExtent2D last_pipe = {
+      .width = (fb->tile_count.width - 1) % fb->pipe0.width + 1,
+      .height = (fb->tile_count.height - 1) % fb->pipe0.height + 1,
+   };
+
+   assert(used_pipe_count <= max_pipe_count);
+   assert(max_pipe_count <= ARRAY_SIZE(fb->pipe_config));
+
+   for (uint32_t y = 0; y < fb->pipe_count.height; y++) {
+      for (uint32_t x = 0; x < fb->pipe_count.width; x++) {
+         const uint32_t pipe_x = fb->pipe0.width * x;
+         const uint32_t pipe_y = fb->pipe0.height * y;
+         const uint32_t pipe_w = (x == fb->pipe_count.width - 1)
+                                    ? last_pipe.width
+                                    : fb->pipe0.width;
+         const uint32_t pipe_h = (y == fb->pipe_count.height - 1)
+                                    ? last_pipe.height
+                                    : fb->pipe0.height;
+         const uint32_t n = fb->pipe_count.width * y + x;
+
+         fb->pipe_config[n] = A6XX_VSC_PIPE_CONFIG_REG_X(pipe_x) |
+                                  A6XX_VSC_PIPE_CONFIG_REG_Y(pipe_y) |
+                                  A6XX_VSC_PIPE_CONFIG_REG_W(pipe_w) |
+                                  A6XX_VSC_PIPE_CONFIG_REG_H(pipe_h);
+         fb->pipe_sizes[n] = CP_SET_BIN_DATA5_0_VSC_SIZE(pipe_w * pipe_h);
+      }
+   }
+
+   memset(fb->pipe_config + used_pipe_count, 0,
+          sizeof(uint32_t) * (max_pipe_count - used_pipe_count));
+}
+
+void
+tu_framebuffer_tiling_config(struct tu_framebuffer *fb,
+                             const struct tu_device *device,
+                             const struct tu_render_pass *pass)
+{
+   tu_tiling_config_update_tile_layout(fb, device, pass);
+   tu_tiling_config_update_pipe_layout(fb, device);
+   tu_tiling_config_update_pipes(fb, device);
+}