1/*
2 * Copyright 2018 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/**
28 * TGSI utility transforms the shader to write imm(0, 0, 0, 1) to vertex
29 * position
30 */
31
32#include "util/u_debug.h"
33#include "util/u_math.h"
34#include "tgsi_vpos.h"
35#include "tgsi_transform.h"
36#include "tgsi_dump.h"
37#include "pipe/p_state.h"
38
39
40struct write_vpos_context
41{
42   struct tgsi_transform_context base;
43   unsigned imm_index;
44};
45
46
47static inline struct write_vpos_context *
48write_vpos_context(struct tgsi_transform_context *ctx)
49{
50   return (struct write_vpos_context *) ctx;
51}
52
53
54/**
55 * TGSI transform prolog callback.
56 */
57static void
58write_vpos_prolog(struct tgsi_transform_context *ctx)
59{
60   struct write_vpos_context *vc = write_vpos_context(ctx);
61   struct tgsi_full_instruction inst;
62
63   tgsi_transform_immediate_decl(ctx, 0.0, 0.0, 0.0, 1.0);
64   tgsi_transform_output_decl(ctx, 0,
65                             TGSI_SEMANTIC_POSITION, 0,
66                             0);
67   inst = tgsi_default_full_instruction();
68   inst.Instruction.Opcode = TGSI_OPCODE_MOV;
69   inst.Instruction.NumDstRegs = 1;
70   tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_OUTPUT,
71                          0, TGSI_WRITEMASK_XYZW);
72   inst.Instruction.NumSrcRegs = 1;
73   tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_IMMEDIATE,
74                          vc->imm_index, TGSI_SWIZZLE_X,
75                          TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_W);
76   ctx->emit_instruction(ctx, &inst);
77}
78
79
80/**
81 * TGSI utility writes imm(0, 0, 0, 1) to vertex position
82 */
83struct tgsi_token *
84tgsi_write_vpos(const struct tgsi_token *tokens_in,
85                unsigned num_immediates)
86{
87   struct write_vpos_context transform;
88   const uint num_new_tokens = 1000; /* should be enough */
89   const uint new_len = tgsi_num_tokens(tokens_in) + num_new_tokens;
90   struct tgsi_token *new_tokens;
91
92   /* setup transformation context */
93   memset(&transform, 0, sizeof(transform));
94   transform.base.prolog = write_vpos_prolog;
95
96   transform.imm_index = num_immediates;
97
98   /* allocate new tokens buffer */
99   new_tokens = tgsi_alloc_tokens(new_len);
100   if (!new_tokens)
101      return NULL;
102
103   /* transform the shader */
104   tgsi_transform_shader(tokens_in, new_tokens, new_len, &transform.base);
105
106   return new_tokens;
107}
108
109
110