aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/svga/svga_resource.c
blob: b9dca1ede2bcba3d5a7a2d2a6e4fef69e1fd8e4a (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
/**********************************************************
 * Copyright 2008-2012 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, 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.
 *
 **********************************************************/

#include "util/u_debug.h"

#include "svga_resource.h"
#include "svga_resource_buffer.h"
#include "svga_resource_texture.h"
#include "svga_context.h"
#include "svga_screen.h"
#include "svga_format.h"


/**
 * This is the primary driver entrypoint for allocating graphics memory
 * (vertex/index/constant buffers, textures, etc)
 */
static struct pipe_resource *
svga_resource_create(struct pipe_screen *screen,
                     const struct pipe_resource *template)
{
   struct pipe_resource *r;

   if (template->target == PIPE_BUFFER)
      r = svga_buffer_create(screen, template);
   else
      r = svga_texture_create(screen, template);

   if (!r) {
      struct svga_screen *svgascreen = svga_screen(screen);
      svgascreen->hud.num_failed_allocations++;
   }

   return r;
}


static struct pipe_resource *
svga_resource_from_handle(struct pipe_screen * screen,
                          const struct pipe_resource *template,
                          struct winsys_handle *whandle,
                          unsigned usage)
{
   if (template->target == PIPE_BUFFER)
      return NULL;
   else
      return svga_texture_from_handle(screen, template, whandle);
}


/**
 * Check if a resource (texture, buffer) of the given size
 * and format can be created.
 * \Return TRUE if OK, FALSE if too large.
 */
static boolean
svga_can_create_resource(struct pipe_screen *screen,
                         const struct pipe_resource *res)
{
   struct svga_screen *svgascreen = svga_screen(screen);
   struct svga_winsys_screen *sws = svgascreen->sws;
   SVGA3dSurfaceFormat format;
   SVGA3dSize base_level_size;
   uint32 numMipLevels;
   uint32 arraySize;
   uint32 numSamples;

   if (res->target == PIPE_BUFFER) {
      format = SVGA3D_BUFFER;
      base_level_size.width = res->width0;
      base_level_size.height = 1;
      base_level_size.depth = 1;
      numMipLevels = 1;
      arraySize = 1;
      numSamples = 0;

   } else {
      if (res->target == PIPE_TEXTURE_CUBE)
         assert(res->array_size == 6);

      format = svga_translate_format(svgascreen, res->format, res->bind);
      if (format == SVGA3D_FORMAT_INVALID)
         return FALSE;

      base_level_size.width = res->width0;
      base_level_size.height = res->height0;
      base_level_size.depth = res->depth0;
      numMipLevels = res->last_level + 1;
      arraySize = res->array_size;
      numSamples = res->nr_samples;
   }

   return sws->surface_can_create(sws, format, base_level_size, 
                                  arraySize, numMipLevels, numSamples);
}


void
svga_init_resource_functions(struct svga_context *svga)
{
   svga->pipe.transfer_map = u_transfer_map_vtbl;
   svga->pipe.transfer_flush_region = u_transfer_flush_region_vtbl;
   svga->pipe.transfer_unmap = u_transfer_unmap_vtbl;
   svga->pipe.buffer_subdata = u_default_buffer_subdata;
   svga->pipe.texture_subdata = u_default_texture_subdata;

   if (svga_have_vgpu10(svga)) {
      svga->pipe.generate_mipmap = svga_texture_generate_mipmap;
   } else {
      svga->pipe.generate_mipmap = NULL;
   }
}

void
svga_init_screen_resource_functions(struct svga_screen *is)
{
   is->screen.resource_create = svga_resource_create;
   is->screen.resource_from_handle = svga_resource_from_handle;
   is->screen.resource_get_handle = u_resource_get_handle_vtbl;
   is->screen.resource_destroy = u_resource_destroy_vtbl;
   is->screen.can_create_resource = svga_can_create_resource;
}