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