summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/nouveau/nvc0/nvc0_resource.c
blob: b22bc41f82e86fd4463cd1209f1692af7f779a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "drm-uapi/drm_fourcc.h"

#include "pipe/p_context.h"
#include "nvc0/nvc0_resource.h"
#include "nouveau_screen.h"


static struct pipe_resource *
nvc0_resource_create(struct pipe_screen *screen,
                     const struct pipe_resource *templ)
{
   switch (templ->target) {
   case PIPE_BUFFER:
      return nouveau_buffer_create(screen, templ);
   default:
      return nvc0_miptree_create(screen, templ, NULL, 0);
   }
}

static struct pipe_resource *
nvc0_resource_create_with_modifiers(struct pipe_screen *screen,
                                    const struct pipe_resource *templ,
                                    const uint64_t *modifiers, int count)
{
   switch (templ->target) {
   case PIPE_BUFFER:
      return nouveau_buffer_create(screen, templ);
   default:
      return nvc0_miptree_create(screen, templ, modifiers, count);
   }
}

static void
nvc0_query_dmabuf_modifiers(struct pipe_screen *screen,
                            enum pipe_format format, int max,
                            uint64_t *modifiers, unsigned int *external_only,
                            int *count)
{
   const int s = nouveau_screen(screen)->tegra_sector_layout ? 0 : 1;
   const uint32_t uc_kind =
      nvc0_choose_tiled_storage_type(screen, format, 0, false);
   const uint32_t num_uc = uc_kind ? 6 : 0; /* max block height = 32 GOBs */
   const int num_supported = num_uc + 1; /* LINEAR is always supported */
   const uint32_t kind_gen = nvc0_get_kind_generation(screen);
   int i, num = 0;

   if (max > num_supported)
      max = num_supported;

   if (!max) {
      max = num_supported;
      external_only = NULL;
      modifiers = NULL;
   }

#define NVC0_ADD_MOD(m) do { \
   if (modifiers) modifiers[num] = m; \
   if (external_only) external_only[num] = 0; \
   num++; \
} while (0)

   for (i = 0; i < max && i < num_uc; i++)
      NVC0_ADD_MOD(DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, s, kind_gen,
                                                         uc_kind, 5 - i));

   if (i < max)
      NVC0_ADD_MOD(DRM_FORMAT_MOD_LINEAR);

#undef NVC0_ADD_MOD

   *count = num;
}

static bool
nvc0_is_dmabuf_modifier_supported(struct pipe_screen *screen,
                                  uint64_t modifier, enum pipe_format format,
                                  bool *external_only)
{
   const int s = nouveau_screen(screen)->tegra_sector_layout ? 0 : 1;
   const uint32_t uc_kind =
      nvc0_choose_tiled_storage_type(screen, format, 0, false);
   const uint32_t num_uc = uc_kind ? 6 : 0; /* max block height = 32 GOBs */
   const uint32_t kind_gen = nvc0_get_kind_generation(screen);
   int i;

   if (modifier == DRM_FORMAT_MOD_LINEAR) {
      if (external_only)
         *external_only = false;

      return true;
   }

   for (i = 0; i < num_uc; i++) {
      if (DRM_FORMAT_MOD_NVIDIA_BLOCK_LINEAR_2D(0, s, kind_gen, uc_kind, i) == modifier) {
         if (external_only)
            *external_only = false;

         return true;
      }
   }

   return false;
}

static struct pipe_resource *
nvc0_resource_from_handle(struct pipe_screen * screen,
                          const struct pipe_resource *templ,
                          struct winsys_handle *whandle,
                          unsigned usage)
{
   if (templ->target == PIPE_BUFFER) {
      return NULL;
   } else {
      struct pipe_resource *res = nv50_miptree_from_handle(screen,
                                                           templ, whandle);
      if (res)
         nv04_resource(res)->vtbl = &nvc0_miptree_vtbl;
      return res;
   }
}

static struct pipe_surface *
nvc0_surface_create(struct pipe_context *pipe,
                    struct pipe_resource *pres,
                    const struct pipe_surface *templ)
{
   if (unlikely(pres->target == PIPE_BUFFER))
      return nv50_surface_from_buffer(pipe, pres, templ);
   return nvc0_miptree_surface_new(pipe, pres, templ);
}

static struct pipe_resource *
nvc0_resource_from_user_memory(struct pipe_screen *pipe,
                               const struct pipe_resource *templ,
                               void *user_memory)
{
   ASSERTED struct nouveau_screen *screen = nouveau_screen(pipe);

   assert(screen->has_svm);
   assert(templ->target == PIPE_BUFFER);

   return nouveau_buffer_create_from_user(pipe, templ, user_memory);
}

void
nvc0_init_resource_functions(struct pipe_context *pcontext)
{
   pcontext->transfer_map = u_transfer_map_vtbl;
   pcontext->transfer_flush_region = u_transfer_flush_region_vtbl;
   pcontext->transfer_unmap = u_transfer_unmap_vtbl;
   pcontext->buffer_subdata = u_default_buffer_subdata;
   pcontext->texture_subdata = u_default_texture_subdata;
   pcontext->create_surface = nvc0_surface_create;
   pcontext->surface_destroy = nv50_surface_destroy;
   pcontext->invalidate_resource = nv50_invalidate_resource;
}

void
nvc0_screen_init_resource_functions(struct pipe_screen *pscreen)
{
   pscreen->resource_create = nvc0_resource_create;
   pscreen->resource_create_with_modifiers = nvc0_resource_create_with_modifiers;
   pscreen->query_dmabuf_modifiers = nvc0_query_dmabuf_modifiers;
   pscreen->is_dmabuf_modifier_supported = nvc0_is_dmabuf_modifier_supported;
   pscreen->resource_from_handle = nvc0_resource_from_handle;
   pscreen->resource_get_handle = u_resource_get_handle_vtbl;
   pscreen->resource_destroy = u_resource_destroy_vtbl;
   pscreen->resource_from_user_memory = nvc0_resource_from_user_memory;
}