gallium/swr: Resource management
[mesa.git] / src / gallium / drivers / swr / swr_clear.cpp
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_query.h"
26
27 static void
28 swr_clear(struct pipe_context *pipe,
29 unsigned buffers,
30 const union pipe_color_union *color,
31 double depth,
32 unsigned stencil)
33 {
34 struct swr_context *ctx = swr_context(pipe);
35 struct pipe_framebuffer_state *fb = &ctx->framebuffer;
36
37 UINT clearMask = 0;
38
39 if (!swr_check_render_cond(pipe))
40 return;
41
42 if (ctx->dirty)
43 swr_update_derived(pipe);
44
45 /* Update clearMask/targetMask */
46 #if 0 /* XXX SWR currently only clears SWR_ATTACHMENT_COLOR0, don't bother \
47 checking others yet. */
48 if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
49 UINT i;
50 for (i = 0; i < fb->nr_cbufs; ++i)
51 if (fb->cbufs[i])
52 clearMask |= (SWR_CLEAR_COLOR0 << i);
53 }
54 #else
55 if (buffers & PIPE_CLEAR_COLOR && fb->cbufs[0])
56 clearMask |= SWR_CLEAR_COLOR;
57 #endif
58
59 if (buffers & PIPE_CLEAR_DEPTH && fb->zsbuf)
60 clearMask |= SWR_CLEAR_DEPTH;
61
62 if (buffers & PIPE_CLEAR_STENCIL && fb->zsbuf)
63 clearMask |= SWR_CLEAR_STENCIL;
64
65 #if 0 // XXX HACK, override clear color alpha. On ubuntu, clears are
66 // transparent.
67 ((union pipe_color_union *)color)->f[3] = 1.0; /* cast off your const'd-ness */
68 #endif
69
70 /* Reset viewport to full framebuffer width/height before clear, then
71 * restore it */
72 /* Scissor affects clear, viewport should not */
73 ctx->dirty |= SWR_NEW_VIEWPORT;
74 SWR_VIEWPORT vp = {0};
75 vp.width = ctx->framebuffer.width;
76 vp.height = ctx->framebuffer.height;
77 SwrSetViewports(ctx->swrContext, 1, &vp, NULL);
78
79 swr_update_draw_context(ctx);
80 SwrClearRenderTarget(ctx->swrContext, clearMask, color->f, depth, stencil);
81 }
82
83
84 #if 0 // XXX, these don't get called. how to get these called? Do we need
85 // them? Docs?
86 static void
87 swr_clear_render_target(struct pipe_context *pipe, struct pipe_surface *ps,
88 const union pipe_color_union *color,
89 unsigned x, unsigned y, unsigned w, unsigned h)
90 {
91 struct swr_context *ctx = swr_context(pipe);
92 fprintf(stderr, "SWR swr_clear_render_target!\n");
93
94 ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
95 }
96
97 static void
98 swr_clear_depth_stencil(struct pipe_context *pipe, struct pipe_surface *ps,
99 unsigned buffers, double depth, unsigned stencil,
100 unsigned x, unsigned y, unsigned w, unsigned h)
101 {
102 struct swr_context *ctx = swr_context(pipe);
103 fprintf(stderr, "SWR swr_clear_depth_stencil!\n");
104
105 ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
106 }
107
108 static void
109 swr_clear_buffer(struct pipe_context *pipe,
110 struct pipe_resource *res,
111 unsigned offset, unsigned size,
112 const void *data, int data_size)
113 {
114 fprintf(stderr, "SWR swr_clear_buffer!\n");
115 struct swr_context *ctx = swr_context(pipe);
116 struct swr_resource *buf = swr_resource(res);
117 union pipe_color_union color;
118 enum pipe_format dst_fmt;
119 unsigned width, height, elements;
120
121 assert(res->target == PIPE_BUFFER);
122 assert(buf);
123 assert(size % data_size == 0);
124
125 SWR_SURFACE_STATE &swr_buffer = buf->swr;
126
127 ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
128 }
129 #endif
130
131
132 void
133 swr_clear_init(struct pipe_context *pipe)
134 {
135 pipe->clear = swr_clear;
136 #if 0 // XXX, these don't get called. how to get these called? Do we need
137 // them? Docs?
138 pipe->clear_render_target = swr_clear_render_target;
139 pipe->clear_depth_stencil = swr_clear_depth_stencil;
140 pipe->clear_buffer = swr_clear_buffer;
141 #endif
142 }