1b8e80941Smrg/****************************************************************************
2b8e80941Smrg * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg ***************************************************************************/
23b8e80941Smrg
24b8e80941Smrg#include "swr_context.h"
25b8e80941Smrg#include "swr_query.h"
26b8e80941Smrg
27b8e80941Smrgstatic void
28b8e80941Smrgswr_clear(struct pipe_context *pipe,
29b8e80941Smrg          unsigned buffers,
30b8e80941Smrg          const union pipe_color_union *color,
31b8e80941Smrg          double depth,
32b8e80941Smrg          unsigned stencil)
33b8e80941Smrg{
34b8e80941Smrg   struct swr_context *ctx = swr_context(pipe);
35b8e80941Smrg   struct pipe_framebuffer_state *fb = &ctx->framebuffer;
36b8e80941Smrg
37b8e80941Smrg   UINT clearMask = 0;
38b8e80941Smrg   unsigned layers = 0;
39b8e80941Smrg
40b8e80941Smrg   if (!swr_check_render_cond(pipe))
41b8e80941Smrg      return;
42b8e80941Smrg
43b8e80941Smrg   swr_update_derived(pipe);
44b8e80941Smrg
45b8e80941Smrg   if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
46b8e80941Smrg      for (unsigned i = 0; i < fb->nr_cbufs; ++i)
47b8e80941Smrg         if (fb->cbufs[i] && (buffers & (PIPE_CLEAR_COLOR0 << i))) {
48b8e80941Smrg            clearMask |= (SWR_ATTACHMENT_COLOR0_BIT << i);
49b8e80941Smrg            layers = std::max(layers, fb->cbufs[i]->u.tex.last_layer -
50b8e80941Smrg                                      fb->cbufs[i]->u.tex.first_layer + 1u);
51b8e80941Smrg         }
52b8e80941Smrg   }
53b8e80941Smrg
54b8e80941Smrg   if (buffers & PIPE_CLEAR_DEPTH && fb->zsbuf) {
55b8e80941Smrg      clearMask |= SWR_ATTACHMENT_DEPTH_BIT;
56b8e80941Smrg      layers = std::max(layers, fb->zsbuf->u.tex.last_layer -
57b8e80941Smrg                                fb->zsbuf->u.tex.first_layer + 1u);
58b8e80941Smrg   }
59b8e80941Smrg
60b8e80941Smrg   if (buffers & PIPE_CLEAR_STENCIL && fb->zsbuf) {
61b8e80941Smrg      clearMask |= SWR_ATTACHMENT_STENCIL_BIT;
62b8e80941Smrg      layers = std::max(layers, fb->zsbuf->u.tex.last_layer -
63b8e80941Smrg                                fb->zsbuf->u.tex.first_layer + 1u);
64b8e80941Smrg   }
65b8e80941Smrg
66b8e80941Smrg#if 0 // XXX HACK, override clear color alpha. On ubuntu, clears are
67b8e80941Smrg      // transparent.
68b8e80941Smrg   ((union pipe_color_union *)color)->f[3] = 1.0; /* cast off your const'd-ness */
69b8e80941Smrg#endif
70b8e80941Smrg
71b8e80941Smrg   SWR_RECT clear_rect;
72b8e80941Smrg   /* If enabled, clear to scissor; otherwise clear full surface */
73b8e80941Smrg   if (ctx->rasterizer && ctx->rasterizer->scissor) {
74b8e80941Smrg      clear_rect = ctx->swr_scissor;
75b8e80941Smrg   } else {
76b8e80941Smrg      clear_rect = {0, 0, (int32_t)fb->width, (int32_t)fb->height};
77b8e80941Smrg   }
78b8e80941Smrg
79b8e80941Smrg   for (unsigned i = 0; i < layers; ++i) {
80b8e80941Smrg      swr_update_draw_context(ctx);
81b8e80941Smrg      ctx->api.pfnSwrClearRenderTarget(ctx->swrContext, clearMask, i,
82b8e80941Smrg                                       color->f, depth, stencil,
83b8e80941Smrg                                       clear_rect);
84b8e80941Smrg
85b8e80941Smrg      // Mask out the attachments that are out of layers.
86b8e80941Smrg      if (fb->zsbuf &&
87b8e80941Smrg          (fb->zsbuf->u.tex.last_layer <= fb->zsbuf->u.tex.first_layer + i))
88b8e80941Smrg         clearMask &= ~(SWR_ATTACHMENT_DEPTH_BIT | SWR_ATTACHMENT_STENCIL_BIT);
89b8e80941Smrg      for (unsigned c = 0; c < fb->nr_cbufs; ++c) {
90b8e80941Smrg         const struct pipe_surface *sf = fb->cbufs[c];
91b8e80941Smrg         if (sf && (sf->u.tex.last_layer <= sf->u.tex.first_layer + i))
92b8e80941Smrg            clearMask &= ~(SWR_ATTACHMENT_COLOR0_BIT << c);
93b8e80941Smrg      }
94b8e80941Smrg   }
95b8e80941Smrg}
96b8e80941Smrg
97b8e80941Smrg
98b8e80941Smrg#if 0 // XXX, these don't get called. how to get these called?  Do we need
99b8e80941Smrg      // them?  Docs?
100b8e80941Smrgstatic void
101b8e80941Smrgswr_clear_render_target(struct pipe_context *pipe, struct pipe_surface *ps,
102b8e80941Smrg                        const union pipe_color_union *color,
103b8e80941Smrg                        unsigned x, unsigned y, unsigned w, unsigned h,
104b8e80941Smrg                        bool render_condition_enabled)
105b8e80941Smrg{
106b8e80941Smrg   struct swr_context *ctx = swr_context(pipe);
107b8e80941Smrg   fprintf(stderr, "SWR swr_clear_render_target!\n");
108b8e80941Smrg
109b8e80941Smrg   ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
110b8e80941Smrg}
111b8e80941Smrg
112b8e80941Smrgstatic void
113b8e80941Smrgswr_clear_depth_stencil(struct pipe_context *pipe, struct pipe_surface *ps,
114b8e80941Smrg                        unsigned buffers, double depth, unsigned stencil,
115b8e80941Smrg                        unsigned x, unsigned y, unsigned w, unsigned h,
116b8e80941Smrg                        bool render_condition_enabled)
117b8e80941Smrg{
118b8e80941Smrg   struct swr_context *ctx = swr_context(pipe);
119b8e80941Smrg   fprintf(stderr, "SWR swr_clear_depth_stencil!\n");
120b8e80941Smrg
121b8e80941Smrg   ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
122b8e80941Smrg}
123b8e80941Smrg
124b8e80941Smrgstatic void
125b8e80941Smrgswr_clear_buffer(struct pipe_context *pipe,
126b8e80941Smrg                 struct pipe_resource *res,
127b8e80941Smrg                 unsigned offset, unsigned size,
128b8e80941Smrg                 const void *data, int data_size)
129b8e80941Smrg{
130b8e80941Smrg   fprintf(stderr, "SWR swr_clear_buffer!\n");
131b8e80941Smrg   struct swr_context *ctx = swr_context(pipe);
132b8e80941Smrg   struct swr_resource *buf = swr_resource(res);
133b8e80941Smrg   union pipe_color_union color;
134b8e80941Smrg   enum pipe_format dst_fmt;
135b8e80941Smrg   unsigned width, height, elements;
136b8e80941Smrg
137b8e80941Smrg   assert(res->target == PIPE_BUFFER);
138b8e80941Smrg   assert(buf);
139b8e80941Smrg   assert(size % data_size == 0);
140b8e80941Smrg
141b8e80941Smrg   SWR_SURFACE_STATE &swr_buffer = buf->swr;
142b8e80941Smrg
143b8e80941Smrg   ctx->dirty |= SWR_NEW_FRAMEBUFFER | SWR_NEW_SCISSOR;
144b8e80941Smrg}
145b8e80941Smrg#endif
146b8e80941Smrg
147b8e80941Smrg
148b8e80941Smrgvoid
149b8e80941Smrgswr_clear_init(struct pipe_context *pipe)
150b8e80941Smrg{
151b8e80941Smrg   pipe->clear = swr_clear;
152b8e80941Smrg#if 0 // XXX, these don't get called. how to get these called?  Do we need
153b8e80941Smrg      // them?  Docs?
154b8e80941Smrg   pipe->clear_render_target = swr_clear_render_target;
155b8e80941Smrg   pipe->clear_depth_stencil = swr_clear_depth_stencil;
156b8e80941Smrg   pipe->clear_buffer = swr_clear_buffer;
157b8e80941Smrg#endif
158b8e80941Smrg}
159