• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  ***************************************************************************/
23 
24 #include "swr_context.h"
25 #include "swr_public.h"
26 #include "swr_screen.h"
27 #include "swr_resource.h"
28 #include "swr_fence.h"
29 #include "gen_knobs.h"
30 
31 #include "pipe/p_screen.h"
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "util/format/u_format.h"
35 #include "util/u_inlines.h"
36 #include "util/u_cpu_detect.h"
37 #include "util/format/u_format_s3tc.h"
38 #include "util/u_string.h"
39 #include "util/u_screen.h"
40 
41 #include "frontend/sw_winsys.h"
42 
43 #include "jit_api.h"
44 
45 #include "memory/TilingFunctions.h"
46 
47 #include <stdio.h>
48 #include <map>
49 
50 /*
51  * Max texture sizes
52  * XXX Check max texture size values against core and sampler.
53  */
54 #define SWR_MAX_TEXTURE_SIZE (2 * 1024 * 1024 * 1024ULL) /* 2GB */
55 /* Not all texture formats can fit into 2GB limit, but we have to
56    live with that. See lp_limits.h for more details */
57 #define SWR_MAX_TEXTURE_2D_SIZE 16384
58 #define SWR_MAX_TEXTURE_3D_LEVELS 12  /* 2K x 2K x 2K for now */
59 #define SWR_MAX_TEXTURE_CUBE_LEVELS 14  /* 8K x 8K for now */
60 #define SWR_MAX_TEXTURE_ARRAY_LAYERS 512 /* 8K x 512 / 8K x 8K x 512 */
61 
62 /* Default max client_copy_limit */
63 #define SWR_CLIENT_COPY_LIMIT 8192
64 
65 /* Flag indicates creation of alternate surface, to prevent recursive loop
66  * in resource creation when msaa_force_enable is set. */
67 #define SWR_RESOURCE_FLAG_ALT_SURFACE (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
68 
69 
70 static const char *
swr_get_name(struct pipe_screen * screen)71 swr_get_name(struct pipe_screen *screen)
72 {
73    static char buf[100];
74    snprintf(buf, sizeof(buf), "SWR (LLVM " MESA_LLVM_VERSION_STRING ", %u bits)",
75             lp_native_vector_width);
76    return buf;
77 }
78 
79 static const char *
swr_get_vendor(struct pipe_screen * screen)80 swr_get_vendor(struct pipe_screen *screen)
81 {
82    return "Intel Corporation";
83 }
84 
85 static bool
swr_is_format_supported(struct pipe_screen * _screen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned bind)86 swr_is_format_supported(struct pipe_screen *_screen,
87                         enum pipe_format format,
88                         enum pipe_texture_target target,
89                         unsigned sample_count,
90                         unsigned storage_sample_count,
91                         unsigned bind)
92 {
93    struct swr_screen *screen = swr_screen(_screen);
94    struct sw_winsys *winsys = screen->winsys;
95    const struct util_format_description *format_desc;
96 
97    assert(target == PIPE_BUFFER || target == PIPE_TEXTURE_1D
98           || target == PIPE_TEXTURE_1D_ARRAY
99           || target == PIPE_TEXTURE_2D
100           || target == PIPE_TEXTURE_2D_ARRAY
101           || target == PIPE_TEXTURE_RECT
102           || target == PIPE_TEXTURE_3D
103           || target == PIPE_TEXTURE_CUBE
104           || target == PIPE_TEXTURE_CUBE_ARRAY);
105 
106    if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
107       return false;
108 
109    format_desc = util_format_description(format);
110    if (!format_desc)
111       return false;
112 
113    if ((sample_count > screen->msaa_max_count)
114       || !util_is_power_of_two_or_zero(sample_count))
115       return false;
116 
117    if (bind & PIPE_BIND_DISPLAY_TARGET) {
118       if (!winsys->is_displaytarget_format_supported(winsys, bind, format))
119          return false;
120    }
121 
122    if (bind & PIPE_BIND_RENDER_TARGET) {
123       if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
124          return false;
125 
126       if (mesa_to_swr_format(format) == (SWR_FORMAT)-1)
127          return false;
128 
129       /*
130        * Although possible, it is unnatural to render into compressed or YUV
131        * surfaces. So disable these here to avoid going into weird paths
132        * inside gallium frontends.
133        */
134       if (format_desc->block.width != 1 || format_desc->block.height != 1)
135          return false;
136    }
137 
138    if (bind & PIPE_BIND_DEPTH_STENCIL) {
139       if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
140          return false;
141 
142       if (mesa_to_swr_format(format) == (SWR_FORMAT)-1)
143          return false;
144    }
145 
146    if (bind & PIPE_BIND_VERTEX_BUFFER) {
147       if (mesa_to_swr_format(format) == (SWR_FORMAT)-1) {
148          return false;
149       }
150    }
151 
152    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC ||
153        format_desc->layout == UTIL_FORMAT_LAYOUT_FXT1)
154    {
155       return false;
156    }
157 
158    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
159        format != PIPE_FORMAT_ETC1_RGB8) {
160       return false;
161    }
162 
163    if ((bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW)) &&
164        ((bind & PIPE_BIND_DISPLAY_TARGET) == 0)) {
165       /* Disable all 3-channel formats, where channel size != 32 bits.
166        * In some cases we run into crashes (in generate_unswizzled_blend()),
167        * for 3-channel RGB16 variants, there was an apparent LLVM bug.
168        * In any case, disabling the shallower 3-channel formats avoids a
169        * number of issues with GL_ARB_copy_image support.
170        */
171       if (format_desc->is_array &&
172           format_desc->nr_channels == 3 &&
173           format_desc->block.bits != 96) {
174          return false;
175       }
176    }
177 
178    return TRUE;
179 }
180 
181 static int
swr_get_param(struct pipe_screen * screen,enum pipe_cap param)182 swr_get_param(struct pipe_screen *screen, enum pipe_cap param)
183 {
184    switch (param) {
185       /* limits */
186    case PIPE_CAP_MAX_RENDER_TARGETS:
187       return PIPE_MAX_COLOR_BUFS;
188    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
189       return SWR_MAX_TEXTURE_2D_SIZE;
190    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
191       return SWR_MAX_TEXTURE_3D_LEVELS;
192    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
193       return SWR_MAX_TEXTURE_CUBE_LEVELS;
194    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
195       return MAX_SO_STREAMS;
196    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
197    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
198       return MAX_ATTRIBUTES * 4;
199    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
200    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
201       return 1024;
202    case PIPE_CAP_MAX_VERTEX_STREAMS:
203       return 4;
204    case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
205       return 2048;
206    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
207       return SWR_MAX_TEXTURE_ARRAY_LAYERS;
208    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
209    case PIPE_CAP_MIN_TEXEL_OFFSET:
210       return -8;
211    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
212    case PIPE_CAP_MAX_TEXEL_OFFSET:
213       return 7;
214    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
215       return 4;
216    case PIPE_CAP_GLSL_FEATURE_LEVEL:
217       return 330;
218    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
219       return 140;
220    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
221       return 16;
222    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
223       return 64;
224    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
225       return 65536;
226    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
227       return 1;
228    case PIPE_CAP_MAX_VIEWPORTS:
229       return KNOB_NUM_VIEWPORTS_SCISSORS;
230    case PIPE_CAP_ENDIANNESS:
231       return PIPE_ENDIAN_NATIVE;
232 
233       /* supported features */
234    case PIPE_CAP_NPOT_TEXTURES:
235    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
236    case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
237    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
238    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
239    case PIPE_CAP_VERTEX_SHADER_SATURATE:
240    case PIPE_CAP_POINT_SPRITE:
241    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
242    case PIPE_CAP_OCCLUSION_QUERY:
243    case PIPE_CAP_QUERY_TIME_ELAPSED:
244    case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
245    case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
246    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
247    case PIPE_CAP_TEXTURE_SWIZZLE:
248    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
249    case PIPE_CAP_INDEP_BLEND_ENABLE:
250    case PIPE_CAP_INDEP_BLEND_FUNC:
251    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
252    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
253    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
254    case PIPE_CAP_DEPTH_CLIP_DISABLE:
255    case PIPE_CAP_PRIMITIVE_RESTART:
256    case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
257    case PIPE_CAP_TGSI_INSTANCEID:
258    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
259    case PIPE_CAP_START_INSTANCE:
260    case PIPE_CAP_SEAMLESS_CUBE_MAP:
261    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
262    case PIPE_CAP_CONDITIONAL_RENDER:
263    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
264    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
265    case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
266    case PIPE_CAP_USER_VERTEX_BUFFERS:
267    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
268    case PIPE_CAP_QUERY_TIMESTAMP:
269    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
270    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
271    case PIPE_CAP_DRAW_INDIRECT:
272    case PIPE_CAP_UMA:
273    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
274    case PIPE_CAP_CLIP_HALFZ:
275    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
276    case PIPE_CAP_DEPTH_BOUNDS_TEST:
277    case PIPE_CAP_CLEAR_TEXTURE:
278    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
279    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
280    case PIPE_CAP_CULL_DISTANCE:
281    case PIPE_CAP_CUBE_MAP_ARRAY:
282    case PIPE_CAP_DOUBLES:
283    case PIPE_CAP_TEXTURE_QUERY_LOD:
284    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
285    case PIPE_CAP_TGSI_TG4_COMPONENT_IN_SWIZZLE:
286    case PIPE_CAP_QUERY_SO_OVERFLOW:
287    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
288       return 1;
289 
290    /* MSAA support
291     * If user has explicitly set max_sample_count = 1 (via SWR_MSAA_MAX_COUNT)
292     * then disable all MSAA support and go back to old (FAKE_SW_MSAA) caps. */
293    case PIPE_CAP_TEXTURE_MULTISAMPLE:
294    case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:
295       return (swr_screen(screen)->msaa_max_count > 1) ? 1 : 0;
296    case PIPE_CAP_FAKE_SW_MSAA:
297       return (swr_screen(screen)->msaa_max_count > 1) ? 0 : 1;
298 
299    /* fetch jit change for 2-4GB buffers requires alignment */
300    case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
301    case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
302    case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
303       return 1;
304 
305       /* unsupported features */
306    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
307    case PIPE_CAP_PCI_GROUP:
308    case PIPE_CAP_PCI_BUS:
309    case PIPE_CAP_PCI_DEVICE:
310    case PIPE_CAP_PCI_FUNCTION:
311    case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
312       return 0;
313    case PIPE_CAP_MAX_GS_INVOCATIONS:
314       return 32;
315    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
316       return 1 << 27;
317    case PIPE_CAP_MAX_VARYINGS:
318       return 32;
319 
320    case PIPE_CAP_VENDOR_ID:
321       return 0xFFFFFFFF;
322    case PIPE_CAP_DEVICE_ID:
323       return 0xFFFFFFFF;
324    case PIPE_CAP_ACCELERATED:
325       return 0;
326    case PIPE_CAP_VIDEO_MEMORY: {
327       /* XXX: Do we want to return the full amount of system memory ? */
328       uint64_t system_memory;
329 
330       if (!os_get_total_physical_memory(&system_memory))
331          return 0;
332 
333       return (int)(system_memory >> 20);
334    }
335    default:
336       return u_pipe_screen_get_param_defaults(screen, param);
337    }
338 }
339 
340 static int
swr_get_shader_param(struct pipe_screen * screen,enum pipe_shader_type shader,enum pipe_shader_cap param)341 swr_get_shader_param(struct pipe_screen *screen,
342                      enum pipe_shader_type shader,
343                      enum pipe_shader_cap param)
344 {
345    if (shader != PIPE_SHADER_VERTEX &&
346        shader != PIPE_SHADER_FRAGMENT &&
347        shader != PIPE_SHADER_GEOMETRY &&
348        shader != PIPE_SHADER_TESS_CTRL &&
349        shader != PIPE_SHADER_TESS_EVAL)
350       return 0;
351 
352    if (param == PIPE_SHADER_CAP_MAX_SHADER_BUFFERS ||
353        param == PIPE_SHADER_CAP_MAX_SHADER_IMAGES) {
354       return 0;
355    }
356 
357    return gallivm_get_shader_param(param);
358 }
359 
360 
361 static float
swr_get_paramf(struct pipe_screen * screen,enum pipe_capf param)362 swr_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
363 {
364    switch (param) {
365    case PIPE_CAPF_MAX_LINE_WIDTH:
366    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
367    case PIPE_CAPF_MAX_POINT_WIDTH:
368       return 255.0; /* arbitrary */
369    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
370       return 0.0;
371    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
372       return 0.0;
373    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
374       return 16.0; /* arbitrary */
375    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
376    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
377    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
378       return 0.0f;
379    }
380    /* should only get here on unhandled cases */
381    debug_printf("Unexpected PIPE_CAPF %d query\n", param);
382    return 0.0;
383 }
384 
385 SWR_FORMAT
mesa_to_swr_format(enum pipe_format format)386 mesa_to_swr_format(enum pipe_format format)
387 {
388    static const std::map<pipe_format,SWR_FORMAT> mesa2swr = {
389       /* depth / stencil */
390       {PIPE_FORMAT_Z16_UNORM,              R16_UNORM}, // z
391       {PIPE_FORMAT_Z32_FLOAT,              R32_FLOAT}, // z
392       {PIPE_FORMAT_Z24_UNORM_S8_UINT,      R24_UNORM_X8_TYPELESS}, // z
393       {PIPE_FORMAT_Z24X8_UNORM,            R24_UNORM_X8_TYPELESS}, // z
394       {PIPE_FORMAT_Z32_FLOAT_S8X24_UINT,   R32_FLOAT_X8X24_TYPELESS}, // z
395 
396       /* alpha */
397       {PIPE_FORMAT_A8_UNORM,               A8_UNORM},
398       {PIPE_FORMAT_A16_UNORM,              A16_UNORM},
399       {PIPE_FORMAT_A16_FLOAT,              A16_FLOAT},
400       {PIPE_FORMAT_A32_FLOAT,              A32_FLOAT},
401 
402       /* odd sizes, bgr */
403       {PIPE_FORMAT_B5G6R5_UNORM,           B5G6R5_UNORM},
404       {PIPE_FORMAT_B5G6R5_SRGB,            B5G6R5_UNORM_SRGB},
405       {PIPE_FORMAT_B5G5R5A1_UNORM,         B5G5R5A1_UNORM},
406       {PIPE_FORMAT_B5G5R5X1_UNORM,         B5G5R5X1_UNORM},
407       {PIPE_FORMAT_B4G4R4A4_UNORM,         B4G4R4A4_UNORM},
408       {PIPE_FORMAT_B8G8R8A8_UNORM,         B8G8R8A8_UNORM},
409       {PIPE_FORMAT_B8G8R8A8_SRGB,          B8G8R8A8_UNORM_SRGB},
410       {PIPE_FORMAT_B8G8R8X8_UNORM,         B8G8R8X8_UNORM},
411       {PIPE_FORMAT_B8G8R8X8_SRGB,          B8G8R8X8_UNORM_SRGB},
412 
413       /* rgb10a2 */
414       {PIPE_FORMAT_R10G10B10A2_UNORM,      R10G10B10A2_UNORM},
415       {PIPE_FORMAT_R10G10B10A2_SNORM,      R10G10B10A2_SNORM},
416       {PIPE_FORMAT_R10G10B10A2_USCALED,    R10G10B10A2_USCALED},
417       {PIPE_FORMAT_R10G10B10A2_SSCALED,    R10G10B10A2_SSCALED},
418       {PIPE_FORMAT_R10G10B10A2_UINT,       R10G10B10A2_UINT},
419 
420       /* rgb10x2 */
421       {PIPE_FORMAT_R10G10B10X2_USCALED,    R10G10B10X2_USCALED},
422 
423       /* bgr10a2 */
424       {PIPE_FORMAT_B10G10R10A2_UNORM,      B10G10R10A2_UNORM},
425       {PIPE_FORMAT_B10G10R10A2_SNORM,      B10G10R10A2_SNORM},
426       {PIPE_FORMAT_B10G10R10A2_USCALED,    B10G10R10A2_USCALED},
427       {PIPE_FORMAT_B10G10R10A2_SSCALED,    B10G10R10A2_SSCALED},
428       {PIPE_FORMAT_B10G10R10A2_UINT,       B10G10R10A2_UINT},
429 
430       /* bgr10x2 */
431       {PIPE_FORMAT_B10G10R10X2_UNORM,      B10G10R10X2_UNORM},
432 
433       /* r11g11b10 */
434       {PIPE_FORMAT_R11G11B10_FLOAT,        R11G11B10_FLOAT},
435 
436       /* 32 bits per component */
437       {PIPE_FORMAT_R32_FLOAT,              R32_FLOAT},
438       {PIPE_FORMAT_R32G32_FLOAT,           R32G32_FLOAT},
439       {PIPE_FORMAT_R32G32B32_FLOAT,        R32G32B32_FLOAT},
440       {PIPE_FORMAT_R32G32B32A32_FLOAT,     R32G32B32A32_FLOAT},
441       {PIPE_FORMAT_R32G32B32X32_FLOAT,     R32G32B32X32_FLOAT},
442 
443       {PIPE_FORMAT_R32_USCALED,            R32_USCALED},
444       {PIPE_FORMAT_R32G32_USCALED,         R32G32_USCALED},
445       {PIPE_FORMAT_R32G32B32_USCALED,      R32G32B32_USCALED},
446       {PIPE_FORMAT_R32G32B32A32_USCALED,   R32G32B32A32_USCALED},
447 
448       {PIPE_FORMAT_R32_SSCALED,            R32_SSCALED},
449       {PIPE_FORMAT_R32G32_SSCALED,         R32G32_SSCALED},
450       {PIPE_FORMAT_R32G32B32_SSCALED,      R32G32B32_SSCALED},
451       {PIPE_FORMAT_R32G32B32A32_SSCALED,   R32G32B32A32_SSCALED},
452 
453       {PIPE_FORMAT_R32_UINT,               R32_UINT},
454       {PIPE_FORMAT_R32G32_UINT,            R32G32_UINT},
455       {PIPE_FORMAT_R32G32B32_UINT,         R32G32B32_UINT},
456       {PIPE_FORMAT_R32G32B32A32_UINT,      R32G32B32A32_UINT},
457 
458       {PIPE_FORMAT_R32_SINT,               R32_SINT},
459       {PIPE_FORMAT_R32G32_SINT,            R32G32_SINT},
460       {PIPE_FORMAT_R32G32B32_SINT,         R32G32B32_SINT},
461       {PIPE_FORMAT_R32G32B32A32_SINT,      R32G32B32A32_SINT},
462 
463       /* 16 bits per component */
464       {PIPE_FORMAT_R16_UNORM,              R16_UNORM},
465       {PIPE_FORMAT_R16G16_UNORM,           R16G16_UNORM},
466       {PIPE_FORMAT_R16G16B16_UNORM,        R16G16B16_UNORM},
467       {PIPE_FORMAT_R16G16B16A16_UNORM,     R16G16B16A16_UNORM},
468       {PIPE_FORMAT_R16G16B16X16_UNORM,     R16G16B16X16_UNORM},
469 
470       {PIPE_FORMAT_R16_USCALED,            R16_USCALED},
471       {PIPE_FORMAT_R16G16_USCALED,         R16G16_USCALED},
472       {PIPE_FORMAT_R16G16B16_USCALED,      R16G16B16_USCALED},
473       {PIPE_FORMAT_R16G16B16A16_USCALED,   R16G16B16A16_USCALED},
474 
475       {PIPE_FORMAT_R16_SNORM,              R16_SNORM},
476       {PIPE_FORMAT_R16G16_SNORM,           R16G16_SNORM},
477       {PIPE_FORMAT_R16G16B16_SNORM,        R16G16B16_SNORM},
478       {PIPE_FORMAT_R16G16B16A16_SNORM,     R16G16B16A16_SNORM},
479 
480       {PIPE_FORMAT_R16_SSCALED,            R16_SSCALED},
481       {PIPE_FORMAT_R16G16_SSCALED,         R16G16_SSCALED},
482       {PIPE_FORMAT_R16G16B16_SSCALED,      R16G16B16_SSCALED},
483       {PIPE_FORMAT_R16G16B16A16_SSCALED,   R16G16B16A16_SSCALED},
484 
485       {PIPE_FORMAT_R16_UINT,               R16_UINT},
486       {PIPE_FORMAT_R16G16_UINT,            R16G16_UINT},
487       {PIPE_FORMAT_R16G16B16_UINT,         R16G16B16_UINT},
488       {PIPE_FORMAT_R16G16B16A16_UINT,      R16G16B16A16_UINT},
489 
490       {PIPE_FORMAT_R16_SINT,               R16_SINT},
491       {PIPE_FORMAT_R16G16_SINT,            R16G16_SINT},
492       {PIPE_FORMAT_R16G16B16_SINT,         R16G16B16_SINT},
493       {PIPE_FORMAT_R16G16B16A16_SINT,      R16G16B16A16_SINT},
494 
495       {PIPE_FORMAT_R16_FLOAT,              R16_FLOAT},
496       {PIPE_FORMAT_R16G16_FLOAT,           R16G16_FLOAT},
497       {PIPE_FORMAT_R16G16B16_FLOAT,        R16G16B16_FLOAT},
498       {PIPE_FORMAT_R16G16B16A16_FLOAT,     R16G16B16A16_FLOAT},
499       {PIPE_FORMAT_R16G16B16X16_FLOAT,     R16G16B16X16_FLOAT},
500 
501       /* 8 bits per component */
502       {PIPE_FORMAT_R8_UNORM,               R8_UNORM},
503       {PIPE_FORMAT_R8G8_UNORM,             R8G8_UNORM},
504       {PIPE_FORMAT_R8G8B8_UNORM,           R8G8B8_UNORM},
505       {PIPE_FORMAT_R8G8B8_SRGB,            R8G8B8_UNORM_SRGB},
506       {PIPE_FORMAT_R8G8B8A8_UNORM,         R8G8B8A8_UNORM},
507       {PIPE_FORMAT_R8G8B8A8_SRGB,          R8G8B8A8_UNORM_SRGB},
508       {PIPE_FORMAT_R8G8B8X8_UNORM,         R8G8B8X8_UNORM},
509       {PIPE_FORMAT_R8G8B8X8_SRGB,          R8G8B8X8_UNORM_SRGB},
510 
511       {PIPE_FORMAT_R8_USCALED,             R8_USCALED},
512       {PIPE_FORMAT_R8G8_USCALED,           R8G8_USCALED},
513       {PIPE_FORMAT_R8G8B8_USCALED,         R8G8B8_USCALED},
514       {PIPE_FORMAT_R8G8B8A8_USCALED,       R8G8B8A8_USCALED},
515 
516       {PIPE_FORMAT_R8_SNORM,               R8_SNORM},
517       {PIPE_FORMAT_R8G8_SNORM,             R8G8_SNORM},
518       {PIPE_FORMAT_R8G8B8_SNORM,           R8G8B8_SNORM},
519       {PIPE_FORMAT_R8G8B8A8_SNORM,         R8G8B8A8_SNORM},
520 
521       {PIPE_FORMAT_R8_SSCALED,             R8_SSCALED},
522       {PIPE_FORMAT_R8G8_SSCALED,           R8G8_SSCALED},
523       {PIPE_FORMAT_R8G8B8_SSCALED,         R8G8B8_SSCALED},
524       {PIPE_FORMAT_R8G8B8A8_SSCALED,       R8G8B8A8_SSCALED},
525 
526       {PIPE_FORMAT_R8_UINT,                R8_UINT},
527       {PIPE_FORMAT_R8G8_UINT,              R8G8_UINT},
528       {PIPE_FORMAT_R8G8B8_UINT,            R8G8B8_UINT},
529       {PIPE_FORMAT_R8G8B8A8_UINT,          R8G8B8A8_UINT},
530 
531       {PIPE_FORMAT_R8_SINT,                R8_SINT},
532       {PIPE_FORMAT_R8G8_SINT,              R8G8_SINT},
533       {PIPE_FORMAT_R8G8B8_SINT,            R8G8B8_SINT},
534       {PIPE_FORMAT_R8G8B8A8_SINT,          R8G8B8A8_SINT},
535 
536       /* These formats are valid for vertex data, but should not be used
537        * for render targets.
538        */
539 
540       {PIPE_FORMAT_R32_FIXED,              R32_SFIXED},
541       {PIPE_FORMAT_R32G32_FIXED,           R32G32_SFIXED},
542       {PIPE_FORMAT_R32G32B32_FIXED,        R32G32B32_SFIXED},
543       {PIPE_FORMAT_R32G32B32A32_FIXED,     R32G32B32A32_SFIXED},
544 
545       {PIPE_FORMAT_R64_FLOAT,              R64_FLOAT},
546       {PIPE_FORMAT_R64G64_FLOAT,           R64G64_FLOAT},
547       {PIPE_FORMAT_R64G64B64_FLOAT,        R64G64B64_FLOAT},
548       {PIPE_FORMAT_R64G64B64A64_FLOAT,     R64G64B64A64_FLOAT},
549 
550       /* These formats have entries in SWR but don't have Load/StoreTile
551        * implementations. That means these aren't renderable, and thus having
552        * a mapping entry here is detrimental.
553        */
554       /*
555 
556       {PIPE_FORMAT_L8_UNORM,               L8_UNORM},
557       {PIPE_FORMAT_I8_UNORM,               I8_UNORM},
558       {PIPE_FORMAT_L8A8_UNORM,             L8A8_UNORM},
559       {PIPE_FORMAT_L16_UNORM,              L16_UNORM},
560       {PIPE_FORMAT_UYVY,                   YCRCB_SWAPUVY},
561 
562       {PIPE_FORMAT_L8_SRGB,                L8_UNORM_SRGB},
563       {PIPE_FORMAT_L8A8_SRGB,              L8A8_UNORM_SRGB},
564 
565       {PIPE_FORMAT_DXT1_RGBA,              BC1_UNORM},
566       {PIPE_FORMAT_DXT3_RGBA,              BC2_UNORM},
567       {PIPE_FORMAT_DXT5_RGBA,              BC3_UNORM},
568 
569       {PIPE_FORMAT_DXT1_SRGBA,             BC1_UNORM_SRGB},
570       {PIPE_FORMAT_DXT3_SRGBA,             BC2_UNORM_SRGB},
571       {PIPE_FORMAT_DXT5_SRGBA,             BC3_UNORM_SRGB},
572 
573       {PIPE_FORMAT_RGTC1_UNORM,            BC4_UNORM},
574       {PIPE_FORMAT_RGTC1_SNORM,            BC4_SNORM},
575       {PIPE_FORMAT_RGTC2_UNORM,            BC5_UNORM},
576       {PIPE_FORMAT_RGTC2_SNORM,            BC5_SNORM},
577 
578       {PIPE_FORMAT_L16A16_UNORM,           L16A16_UNORM},
579       {PIPE_FORMAT_I16_UNORM,              I16_UNORM},
580       {PIPE_FORMAT_L16_FLOAT,              L16_FLOAT},
581       {PIPE_FORMAT_L16A16_FLOAT,           L16A16_FLOAT},
582       {PIPE_FORMAT_I16_FLOAT,              I16_FLOAT},
583       {PIPE_FORMAT_L32_FLOAT,              L32_FLOAT},
584       {PIPE_FORMAT_L32A32_FLOAT,           L32A32_FLOAT},
585       {PIPE_FORMAT_I32_FLOAT,              I32_FLOAT},
586 
587       {PIPE_FORMAT_I8_UINT,                I8_UINT},
588       {PIPE_FORMAT_L8_UINT,                L8_UINT},
589       {PIPE_FORMAT_L8A8_UINT,              L8A8_UINT},
590 
591       {PIPE_FORMAT_I8_SINT,                I8_SINT},
592       {PIPE_FORMAT_L8_SINT,                L8_SINT},
593       {PIPE_FORMAT_L8A8_SINT,              L8A8_SINT},
594 
595       */
596    };
597 
598    auto it = mesa2swr.find(format);
599    if (it == mesa2swr.end())
600       return (SWR_FORMAT)-1;
601    else
602       return it->second;
603 }
604 
605 static bool
swr_displaytarget_layout(struct swr_screen * screen,struct swr_resource * res)606 swr_displaytarget_layout(struct swr_screen *screen, struct swr_resource *res)
607 {
608    struct sw_winsys *winsys = screen->winsys;
609    struct sw_displaytarget *dt;
610 
611    const unsigned width = align(res->swr.width, res->swr.halign);
612    const unsigned height = align(res->swr.height, res->swr.valign);
613 
614    UINT stride;
615    dt = winsys->displaytarget_create(winsys,
616                                      res->base.bind,
617                                      res->base.format,
618                                      width, height,
619                                      64, NULL,
620                                      &stride);
621 
622    if (dt == NULL)
623       return false;
624 
625    void *map = winsys->displaytarget_map(winsys, dt, 0);
626 
627    res->display_target = dt;
628    res->swr.xpBaseAddress = (gfxptr_t)map;
629 
630    /* Clear the display target surface */
631    if (map)
632       memset(map, 0, height * stride);
633 
634    winsys->displaytarget_unmap(winsys, dt);
635 
636    return true;
637 }
638 
639 static bool
swr_texture_layout(struct swr_screen * screen,struct swr_resource * res,bool allocate)640 swr_texture_layout(struct swr_screen *screen,
641                    struct swr_resource *res,
642                    bool allocate)
643 {
644    struct pipe_resource *pt = &res->base;
645 
646    pipe_format fmt = pt->format;
647    const struct util_format_description *desc = util_format_description(fmt);
648 
649    res->has_depth = util_format_has_depth(desc);
650    res->has_stencil = util_format_has_stencil(desc);
651 
652    if (res->has_stencil && !res->has_depth)
653       fmt = PIPE_FORMAT_R8_UINT;
654 
655    /* We always use the SWR layout. For 2D and 3D textures this looks like:
656     *
657     * |<------- pitch ------->|
658     * +=======================+-------
659     * |Array 0                |   ^
660     * |                       |   |
661     * |        Level 0        |   |
662     * |                       |   |
663     * |                       | qpitch
664     * +-----------+-----------+   |
665     * |           | L2L2L2L2  |   |
666     * |  Level 1  | L3L3      |   |
667     * |           | L4        |   v
668     * +===========+===========+-------
669     * |Array 1                |
670     * |                       |
671     * |        Level 0        |
672     * |                       |
673     * |                       |
674     * +-----------+-----------+
675     * |           | L2L2L2L2  |
676     * |  Level 1  | L3L3      |
677     * |           | L4        |
678     * +===========+===========+
679     *
680     * The overall width in bytes is known as the pitch, while the overall
681     * height in rows is the qpitch. Array slices are laid out logically below
682     * one another, qpitch rows apart. For 3D surfaces, the "level" values are
683     * just invalid for the higher array numbers (since depth is also
684     * minified). 1D and 1D array surfaces are stored effectively the same way,
685     * except that pitch never plays into it. All the levels are logically
686     * adjacent to each other on the X axis. The qpitch becomes the number of
687     * elements between array slices, while the pitch is unused.
688     *
689     * Each level's sizes are subject to the valign and halign settings of the
690     * surface. For compressed formats that swr is unaware of, we will use an
691     * appropriately-sized uncompressed format, and scale the widths/heights.
692     *
693     * This surface is stored inside res->swr. For depth/stencil textures,
694     * res->secondary will have an identically-laid-out but R8_UINT-formatted
695     * stencil tree. In the Z32F_S8 case, the primary surface still has 64-bpp
696     * texels, to simplify map/unmap logic which copies the stencil values
697     * in/out.
698     */
699 
700    res->swr.width = pt->width0;
701    res->swr.height = pt->height0;
702    res->swr.type = swr_convert_target_type(pt->target);
703    res->swr.tileMode = SWR_TILE_NONE;
704    res->swr.format = mesa_to_swr_format(fmt);
705    res->swr.numSamples = std::max(1u, pt->nr_samples);
706 
707    if (pt->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) {
708       res->swr.halign = KNOB_MACROTILE_X_DIM;
709       res->swr.valign = KNOB_MACROTILE_Y_DIM;
710 
711       /* If SWR_MSAA_FORCE_ENABLE is set, turn on MSAA and override requested
712        * surface sample count. */
713       if (screen->msaa_force_enable) {
714          res->swr.numSamples = screen->msaa_max_count;
715          swr_print_info("swr_texture_layout: forcing sample count: %d\n",
716                  res->swr.numSamples);
717       }
718    } else {
719       res->swr.halign = 1;
720       res->swr.valign = 1;
721    }
722 
723    unsigned halign = res->swr.halign * util_format_get_blockwidth(fmt);
724    unsigned width = align(pt->width0, halign);
725    if (pt->target == PIPE_TEXTURE_1D || pt->target == PIPE_TEXTURE_1D_ARRAY) {
726       for (int level = 1; level <= pt->last_level; level++)
727          width += align(u_minify(pt->width0, level), halign);
728       res->swr.pitch = util_format_get_blocksize(fmt);
729       res->swr.qpitch = util_format_get_nblocksx(fmt, width);
730    } else {
731       // The pitch is the overall width of the texture in bytes. Most of the
732       // time this is the pitch of level 0 since all the other levels fit
733       // underneath it. However in some degenerate situations, the width of
734       // level1 + level2 may be larger. In that case, we use those
735       // widths. This can happen if, e.g. halign is 32, and the width of level
736       // 0 is 32 or less. In that case, the aligned levels 1 and 2 will also
737       // be 32 each, adding up to 64.
738       unsigned valign = res->swr.valign * util_format_get_blockheight(fmt);
739       if (pt->last_level > 1) {
740          width = std::max<uint32_t>(
741                width,
742                align(u_minify(pt->width0, 1), halign) +
743                align(u_minify(pt->width0, 2), halign));
744       }
745       res->swr.pitch = util_format_get_stride(fmt, width);
746 
747       // The qpitch is controlled by either the height of the second LOD, or
748       // the combination of all the later LODs.
749       unsigned height = align(pt->height0, valign);
750       if (pt->last_level == 1) {
751          height += align(u_minify(pt->height0, 1), valign);
752       } else if (pt->last_level > 1) {
753          unsigned level1 = align(u_minify(pt->height0, 1), valign);
754          unsigned level2 = 0;
755          for (int level = 2; level <= pt->last_level; level++) {
756             level2 += align(u_minify(pt->height0, level), valign);
757          }
758          height += std::max(level1, level2);
759       }
760       res->swr.qpitch = util_format_get_nblocksy(fmt, height);
761    }
762 
763    if (pt->target == PIPE_TEXTURE_3D)
764       res->swr.depth = pt->depth0;
765    else
766       res->swr.depth = pt->array_size;
767 
768    // Fix up swr format if necessary so that LOD offset computation works
769    if (res->swr.format == (SWR_FORMAT)-1) {
770       switch (util_format_get_blocksize(fmt)) {
771       default:
772          unreachable("Unexpected format block size");
773       case 1: res->swr.format = R8_UINT; break;
774       case 2: res->swr.format = R16_UINT; break;
775       case 4: res->swr.format = R32_UINT; break;
776       case 8:
777          if (util_format_is_compressed(fmt))
778             res->swr.format = BC4_UNORM;
779          else
780             res->swr.format = R32G32_UINT;
781          break;
782       case 16:
783          if (util_format_is_compressed(fmt))
784             res->swr.format = BC5_UNORM;
785          else
786             res->swr.format = R32G32B32A32_UINT;
787          break;
788       }
789    }
790 
791    for (int level = 0; level <= pt->last_level; level++) {
792       res->mip_offsets[level] =
793          ComputeSurfaceOffset<false>(0, 0, 0, 0, 0, level, &res->swr);
794    }
795 
796    size_t total_size = (uint64_t)res->swr.depth * res->swr.qpitch *
797                                  res->swr.pitch * res->swr.numSamples;
798 
799    // Let non-sampled textures (e.g. buffer objects) bypass the size limit
800    if (swr_resource_is_texture(&res->base) && total_size > SWR_MAX_TEXTURE_SIZE)
801       return false;
802 
803    if (allocate) {
804       res->swr.xpBaseAddress = (gfxptr_t)AlignedMalloc(total_size, 64);
805       if (!res->swr.xpBaseAddress)
806          return false;
807 
808       if (res->has_depth && res->has_stencil) {
809          res->secondary = res->swr;
810          res->secondary.format = R8_UINT;
811          res->secondary.pitch = res->swr.pitch / util_format_get_blocksize(fmt);
812 
813          for (int level = 0; level <= pt->last_level; level++) {
814             res->secondary_mip_offsets[level] =
815                ComputeSurfaceOffset<false>(0, 0, 0, 0, 0, level, &res->secondary);
816          }
817 
818          total_size = res->secondary.depth * res->secondary.qpitch *
819                       res->secondary.pitch * res->secondary.numSamples;
820 
821          res->secondary.xpBaseAddress = (gfxptr_t) AlignedMalloc(total_size, 64);
822          if (!res->secondary.xpBaseAddress) {
823             AlignedFree((void *)res->swr.xpBaseAddress);
824             return false;
825          }
826       }
827    }
828 
829    return true;
830 }
831 
832 static bool
swr_can_create_resource(struct pipe_screen * screen,const struct pipe_resource * templat)833 swr_can_create_resource(struct pipe_screen *screen,
834                         const struct pipe_resource *templat)
835 {
836    struct swr_resource res;
837    memset(&res, 0, sizeof(res));
838    res.base = *templat;
839    return swr_texture_layout(swr_screen(screen), &res, false);
840 }
841 
842 /* Helper function that conditionally creates a single-sample resolve resource
843  * and attaches it to main multisample resource. */
844 static bool
swr_create_resolve_resource(struct pipe_screen * _screen,struct swr_resource * msaa_res)845 swr_create_resolve_resource(struct pipe_screen *_screen,
846                             struct swr_resource *msaa_res)
847 {
848    struct swr_screen *screen = swr_screen(_screen);
849 
850    /* If resource is multisample, create a single-sample resolve resource */
851    if (msaa_res->base.nr_samples > 1 || (screen->msaa_force_enable &&
852             !(msaa_res->base.flags & SWR_RESOURCE_FLAG_ALT_SURFACE))) {
853 
854       /* Create a single-sample copy of the resource.  Copy the original
855        * resource parameters and set flag to prevent recursion when re-calling
856        * resource_create */
857       struct pipe_resource alt_template = msaa_res->base;
858       alt_template.nr_samples = 0;
859       alt_template.flags |= SWR_RESOURCE_FLAG_ALT_SURFACE;
860 
861       /* Note: Display_target is a special single-sample resource, only the
862        * display_target has been created already. */
863       if (msaa_res->base.bind & (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT
864                | PIPE_BIND_SHARED)) {
865          /* Allocate the multisample buffers. */
866          if (!swr_texture_layout(screen, msaa_res, true))
867             return false;
868 
869          /* Alt resource will only be bound as PIPE_BIND_RENDER_TARGET
870           * remove the DISPLAY_TARGET, SCANOUT, and SHARED bindings */
871          alt_template.bind = PIPE_BIND_RENDER_TARGET;
872       }
873 
874       /* Allocate single-sample resolve surface */
875       struct pipe_resource *alt;
876       alt = _screen->resource_create(_screen, &alt_template);
877       if (!alt)
878          return false;
879 
880       /* Attach it to the multisample resource */
881       msaa_res->resolve_target = alt;
882 
883       /* Hang resolve surface state off the multisample surface state to so
884        * StoreTiles knows where to resolve the surface. */
885       msaa_res->swr.xpAuxBaseAddress = (gfxptr_t)&swr_resource(alt)->swr;
886    }
887 
888    return true; /* success */
889 }
890 
891 static struct pipe_resource *
swr_resource_create(struct pipe_screen * _screen,const struct pipe_resource * templat)892 swr_resource_create(struct pipe_screen *_screen,
893                     const struct pipe_resource *templat)
894 {
895    struct swr_screen *screen = swr_screen(_screen);
896    struct swr_resource *res = CALLOC_STRUCT(swr_resource);
897    if (!res)
898       return NULL;
899 
900    res->base = *templat;
901    pipe_reference_init(&res->base.reference, 1);
902    res->base.screen = &screen->base;
903 
904    if (swr_resource_is_texture(&res->base)) {
905       if (res->base.bind & (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT
906                             | PIPE_BIND_SHARED)) {
907          /* displayable surface
908           * first call swr_texture_layout without allocating to finish
909           * filling out the SWR_SURFACE_STATE in res */
910          swr_texture_layout(screen, res, false);
911          if (!swr_displaytarget_layout(screen, res))
912             goto fail;
913       } else {
914          /* texture map */
915          if (!swr_texture_layout(screen, res, true))
916             goto fail;
917       }
918 
919       /* If resource was multisample, create resolve resource and attach
920        * it to multisample resource. */
921       if (!swr_create_resolve_resource(_screen, res))
922             goto fail;
923 
924    } else {
925       /* other data (vertex buffer, const buffer, etc) */
926       assert(util_format_get_blocksize(templat->format) == 1);
927       assert(templat->height0 == 1);
928       assert(templat->depth0 == 1);
929       assert(templat->last_level == 0);
930 
931       /* Easiest to just call swr_texture_layout, as it sets up
932        * SWR_SURFACE_STATE in res */
933       if (!swr_texture_layout(screen, res, true))
934          goto fail;
935    }
936 
937    return &res->base;
938 
939 fail:
940    FREE(res);
941    return NULL;
942 }
943 
944 static void
swr_resource_destroy(struct pipe_screen * p_screen,struct pipe_resource * pt)945 swr_resource_destroy(struct pipe_screen *p_screen, struct pipe_resource *pt)
946 {
947    struct swr_screen *screen = swr_screen(p_screen);
948    struct swr_resource *spr = swr_resource(pt);
949 
950    if (spr->display_target) {
951       /* If resource is display target, winsys manages the buffer and will
952        * free it on displaytarget_destroy. */
953       swr_fence_finish(p_screen, NULL, screen->flush_fence, 0);
954 
955       struct sw_winsys *winsys = screen->winsys;
956       winsys->displaytarget_destroy(winsys, spr->display_target);
957 
958       if (spr->swr.numSamples > 1) {
959          /* Free an attached resolve resource */
960          struct swr_resource *alt = swr_resource(spr->resolve_target);
961          swr_fence_work_free(screen->flush_fence, (void*)(alt->swr.xpBaseAddress), true);
962 
963          /* Free multisample buffer */
964          swr_fence_work_free(screen->flush_fence, (void*)(spr->swr.xpBaseAddress), true);
965       }
966    } else {
967       /* For regular resources, defer deletion */
968       swr_resource_unused(pt);
969 
970       if (spr->swr.numSamples > 1) {
971          /* Free an attached resolve resource */
972          struct swr_resource *alt = swr_resource(spr->resolve_target);
973          swr_fence_work_free(screen->flush_fence, (void*)(alt->swr.xpBaseAddress), true);
974       }
975 
976       swr_fence_work_free(screen->flush_fence, (void*)(spr->swr.xpBaseAddress), true);
977       swr_fence_work_free(screen->flush_fence,
978                           (void*)(spr->secondary.xpBaseAddress), true);
979 
980       /* If work queue grows too large, submit a fence to force queue to
981        * drain.  This is mainly to decrease the amount of memory used by the
982        * piglit streaming-texture-leak test */
983       if (screen->pipe && swr_fence(screen->flush_fence)->work.count > 64)
984          swr_fence_submit(swr_context(screen->pipe), screen->flush_fence);
985    }
986 
987    FREE(spr);
988 }
989 
990 
991 static void
swr_flush_frontbuffer(struct pipe_screen * p_screen,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * sub_box)992 swr_flush_frontbuffer(struct pipe_screen *p_screen,
993                       struct pipe_resource *resource,
994                       unsigned level,
995                       unsigned layer,
996                       void *context_private,
997                       struct pipe_box *sub_box)
998 {
999    struct swr_screen *screen = swr_screen(p_screen);
1000    struct sw_winsys *winsys = screen->winsys;
1001    struct swr_resource *spr = swr_resource(resource);
1002    struct pipe_context *pipe = screen->pipe;
1003    struct swr_context *ctx = swr_context(pipe);
1004 
1005    if (pipe) {
1006       swr_fence_finish(p_screen, NULL, screen->flush_fence, 0);
1007       swr_resource_unused(resource);
1008       ctx->api.pfnSwrEndFrame(ctx->swrContext);
1009    }
1010 
1011    /* Multisample resolved into resolve_target at flush with store_resource */
1012    if (pipe && spr->swr.numSamples > 1) {
1013       struct pipe_resource *resolve_target = spr->resolve_target;
1014 
1015       /* Once resolved, copy into display target */
1016       SWR_SURFACE_STATE *resolve = &swr_resource(resolve_target)->swr;
1017 
1018       void *map = winsys->displaytarget_map(winsys, spr->display_target,
1019                                             PIPE_MAP_WRITE);
1020       memcpy(map, (void*)(resolve->xpBaseAddress), resolve->pitch * resolve->height);
1021       winsys->displaytarget_unmap(winsys, spr->display_target);
1022    }
1023 
1024    debug_assert(spr->display_target);
1025    if (spr->display_target)
1026       winsys->displaytarget_display(
1027          winsys, spr->display_target, context_private, sub_box);
1028 }
1029 
1030 
1031 void
swr_destroy_screen_internal(struct swr_screen ** screen)1032 swr_destroy_screen_internal(struct swr_screen **screen)
1033 {
1034    struct pipe_screen *p_screen = &(*screen)->base;
1035 
1036    swr_fence_finish(p_screen, NULL, (*screen)->flush_fence, 0);
1037    swr_fence_reference(p_screen, &(*screen)->flush_fence, NULL);
1038 
1039    JitDestroyContext((*screen)->hJitMgr);
1040 
1041    if ((*screen)->pLibrary)
1042       util_dl_close((*screen)->pLibrary);
1043 
1044    FREE(*screen);
1045    *screen = NULL;
1046 }
1047 
1048 
1049 static void
swr_destroy_screen(struct pipe_screen * p_screen)1050 swr_destroy_screen(struct pipe_screen *p_screen)
1051 {
1052    struct swr_screen *screen = swr_screen(p_screen);
1053    struct sw_winsys *winsys = screen->winsys;
1054 
1055    swr_print_info("SWR destroy screen!\n");
1056 
1057    if (winsys->destroy)
1058       winsys->destroy(winsys);
1059 
1060    swr_destroy_screen_internal(&screen);
1061 }
1062 
1063 
1064 static void
swr_validate_env_options(struct swr_screen * screen)1065 swr_validate_env_options(struct swr_screen *screen)
1066 {
1067    /* The client_copy_limit sets a maximum on the amount of user-buffer memory
1068     * copied to scratch space on a draw.  Past this, the draw will access
1069     * user-buffer directly and then block.  This is faster than queuing many
1070     * large client draws. */
1071    screen->client_copy_limit = SWR_CLIENT_COPY_LIMIT;
1072    int client_copy_limit =
1073       debug_get_num_option("SWR_CLIENT_COPY_LIMIT", SWR_CLIENT_COPY_LIMIT);
1074    if (client_copy_limit > 0)
1075       screen->client_copy_limit = client_copy_limit;
1076 
1077    /* XXX msaa under development, disable by default for now */
1078    screen->msaa_max_count = 1; /* was SWR_MAX_NUM_MULTISAMPLES; */
1079 
1080    /* validate env override values, within range and power of 2 */
1081    int msaa_max_count = debug_get_num_option("SWR_MSAA_MAX_COUNT", 1);
1082    if (msaa_max_count != 1) {
1083       if ((msaa_max_count < 1) || (msaa_max_count > SWR_MAX_NUM_MULTISAMPLES)
1084             || !util_is_power_of_two_or_zero(msaa_max_count)) {
1085          fprintf(stderr, "SWR_MSAA_MAX_COUNT invalid: %d\n", msaa_max_count);
1086          fprintf(stderr, "must be power of 2 between 1 and %d" \
1087                          " (or 1 to disable msaa)\n",
1088                SWR_MAX_NUM_MULTISAMPLES);
1089          fprintf(stderr, "(msaa disabled)\n");
1090          msaa_max_count = 1;
1091       }
1092 
1093       swr_print_info("SWR_MSAA_MAX_COUNT: %d\n", msaa_max_count);
1094 
1095       screen->msaa_max_count = msaa_max_count;
1096    }
1097 
1098    screen->msaa_force_enable = debug_get_bool_option(
1099          "SWR_MSAA_FORCE_ENABLE", false);
1100    if (screen->msaa_force_enable)
1101       swr_print_info("SWR_MSAA_FORCE_ENABLE: true\n");
1102 }
1103 
1104 
1105 struct pipe_screen *
swr_create_screen_internal(struct sw_winsys * winsys)1106 swr_create_screen_internal(struct sw_winsys *winsys)
1107 {
1108    struct swr_screen *screen = CALLOC_STRUCT(swr_screen);
1109 
1110    if (!screen)
1111       return NULL;
1112 
1113    if (!lp_build_init()) {
1114       FREE(screen);
1115       return NULL;
1116    }
1117 
1118    screen->winsys = winsys;
1119    screen->base.get_name = swr_get_name;
1120    screen->base.get_vendor = swr_get_vendor;
1121    screen->base.is_format_supported = swr_is_format_supported;
1122    screen->base.context_create = swr_create_context;
1123    screen->base.can_create_resource = swr_can_create_resource;
1124 
1125    screen->base.destroy = swr_destroy_screen;
1126    screen->base.get_param = swr_get_param;
1127    screen->base.get_shader_param = swr_get_shader_param;
1128    screen->base.get_paramf = swr_get_paramf;
1129 
1130    screen->base.resource_create = swr_resource_create;
1131    screen->base.resource_destroy = swr_resource_destroy;
1132 
1133    screen->base.flush_frontbuffer = swr_flush_frontbuffer;
1134 
1135    // Pass in "" for architecture for run-time determination
1136    screen->hJitMgr = JitCreateContext(KNOB_SIMD_WIDTH, "", "swr");
1137 
1138    swr_fence_init(&screen->base);
1139 
1140    swr_validate_env_options(screen);
1141 
1142    return &screen->base;
1143 }
1144