1848b8605Smrg/**********************************************************
2848b8605Smrg * Copyright 2008-2012 VMware, Inc.  All rights reserved.
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person
5848b8605Smrg * obtaining a copy of this software and associated documentation
6848b8605Smrg * files (the "Software"), to deal in the Software without
7848b8605Smrg * restriction, including without limitation the rights to use, copy,
8848b8605Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
9848b8605Smrg * of the Software, and to permit persons to whom the Software is
10848b8605Smrg * furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice shall be
13848b8605Smrg * included in all copies or substantial portions of the Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16848b8605Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18848b8605Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19848b8605Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20848b8605Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21848b8605Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22848b8605Smrg * SOFTWARE.
23848b8605Smrg *
24848b8605Smrg **********************************************************/
25848b8605Smrg
26848b8605Smrg#include "util/u_debug.h"
27848b8605Smrg
28848b8605Smrg#include "svga_resource.h"
29848b8605Smrg#include "svga_resource_buffer.h"
30848b8605Smrg#include "svga_resource_texture.h"
31848b8605Smrg#include "svga_context.h"
32848b8605Smrg#include "svga_screen.h"
33848b8605Smrg#include "svga_format.h"
34848b8605Smrg
35848b8605Smrg
36b8e80941Smrg/**
37b8e80941Smrg * This is the primary driver entrypoint for allocating graphics memory
38b8e80941Smrg * (vertex/index/constant buffers, textures, etc)
39b8e80941Smrg */
40848b8605Smrgstatic struct pipe_resource *
41848b8605Smrgsvga_resource_create(struct pipe_screen *screen,
42848b8605Smrg                     const struct pipe_resource *template)
43848b8605Smrg{
44b8e80941Smrg   struct pipe_resource *r;
45b8e80941Smrg
46848b8605Smrg   if (template->target == PIPE_BUFFER)
47b8e80941Smrg      r = svga_buffer_create(screen, template);
48848b8605Smrg   else
49b8e80941Smrg      r = svga_texture_create(screen, template);
50b8e80941Smrg
51b8e80941Smrg   if (!r) {
52b8e80941Smrg      struct svga_screen *svgascreen = svga_screen(screen);
53b8e80941Smrg      svgascreen->hud.num_failed_allocations++;
54b8e80941Smrg   }
55b8e80941Smrg
56b8e80941Smrg   return r;
57848b8605Smrg}
58848b8605Smrg
59848b8605Smrg
60848b8605Smrgstatic struct pipe_resource *
61848b8605Smrgsvga_resource_from_handle(struct pipe_screen * screen,
62848b8605Smrg                          const struct pipe_resource *template,
63b8e80941Smrg                          struct winsys_handle *whandle,
64b8e80941Smrg                          unsigned usage)
65848b8605Smrg{
66848b8605Smrg   if (template->target == PIPE_BUFFER)
67848b8605Smrg      return NULL;
68848b8605Smrg   else
69848b8605Smrg      return svga_texture_from_handle(screen, template, whandle);
70848b8605Smrg}
71848b8605Smrg
72848b8605Smrg
73848b8605Smrg/**
74848b8605Smrg * Check if a resource (texture, buffer) of the given size
75848b8605Smrg * and format can be created.
76848b8605Smrg * \Return TRUE if OK, FALSE if too large.
77848b8605Smrg */
78848b8605Smrgstatic boolean
79848b8605Smrgsvga_can_create_resource(struct pipe_screen *screen,
80848b8605Smrg                         const struct pipe_resource *res)
81848b8605Smrg{
82848b8605Smrg   struct svga_screen *svgascreen = svga_screen(screen);
83848b8605Smrg   struct svga_winsys_screen *sws = svgascreen->sws;
84848b8605Smrg   SVGA3dSurfaceFormat format;
85848b8605Smrg   SVGA3dSize base_level_size;
86848b8605Smrg   uint32 numMipLevels;
87b8e80941Smrg   uint32 arraySize;
88b8e80941Smrg   uint32 numSamples;
89848b8605Smrg
90848b8605Smrg   if (res->target == PIPE_BUFFER) {
91848b8605Smrg      format = SVGA3D_BUFFER;
92848b8605Smrg      base_level_size.width = res->width0;
93848b8605Smrg      base_level_size.height = 1;
94848b8605Smrg      base_level_size.depth = 1;
95848b8605Smrg      numMipLevels = 1;
96b8e80941Smrg      arraySize = 1;
97b8e80941Smrg      numSamples = 0;
98848b8605Smrg
99848b8605Smrg   } else {
100b8e80941Smrg      if (res->target == PIPE_TEXTURE_CUBE)
101b8e80941Smrg         assert(res->array_size == 6);
102b8e80941Smrg
103848b8605Smrg      format = svga_translate_format(svgascreen, res->format, res->bind);
104848b8605Smrg      if (format == SVGA3D_FORMAT_INVALID)
105848b8605Smrg         return FALSE;
106848b8605Smrg
107848b8605Smrg      base_level_size.width = res->width0;
108848b8605Smrg      base_level_size.height = res->height0;
109848b8605Smrg      base_level_size.depth = res->depth0;
110848b8605Smrg      numMipLevels = res->last_level + 1;
111b8e80941Smrg      arraySize = res->array_size;
112b8e80941Smrg      numSamples = res->nr_samples;
113848b8605Smrg   }
114848b8605Smrg
115848b8605Smrg   return sws->surface_can_create(sws, format, base_level_size,
116b8e80941Smrg                                  arraySize, numMipLevels, numSamples);
117848b8605Smrg}
118848b8605Smrg
119848b8605Smrg
120848b8605Smrgvoid
121848b8605Smrgsvga_init_resource_functions(struct svga_context *svga)
122848b8605Smrg{
123848b8605Smrg   svga->pipe.transfer_map = u_transfer_map_vtbl;
124848b8605Smrg   svga->pipe.transfer_flush_region = u_transfer_flush_region_vtbl;
125848b8605Smrg   svga->pipe.transfer_unmap = u_transfer_unmap_vtbl;
126b8e80941Smrg   svga->pipe.buffer_subdata = u_default_buffer_subdata;
127b8e80941Smrg   svga->pipe.texture_subdata = u_default_texture_subdata;
128b8e80941Smrg
129b8e80941Smrg   if (svga_have_vgpu10(svga)) {
130b8e80941Smrg      svga->pipe.generate_mipmap = svga_texture_generate_mipmap;
131b8e80941Smrg   } else {
132b8e80941Smrg      svga->pipe.generate_mipmap = NULL;
133b8e80941Smrg   }
134848b8605Smrg}
135848b8605Smrg
136848b8605Smrgvoid
137848b8605Smrgsvga_init_screen_resource_functions(struct svga_screen *is)
138848b8605Smrg{
139848b8605Smrg   is->screen.resource_create = svga_resource_create;
140848b8605Smrg   is->screen.resource_from_handle = svga_resource_from_handle;
141848b8605Smrg   is->screen.resource_get_handle = u_resource_get_handle_vtbl;
142848b8605Smrg   is->screen.resource_destroy = u_resource_destroy_vtbl;
143848b8605Smrg   is->screen.can_create_resource = svga_can_create_resource;
144848b8605Smrg}
145