19490d86808300e5819941a40784e272c290e05eeEric Anholt/*
29490d86808300e5819941a40784e272c290e05eeEric Anholt Copyright (C) Intel Corp.  2006.  All Rights Reserved.
39490d86808300e5819941a40784e272c290e05eeEric Anholt Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
49490d86808300e5819941a40784e272c290e05eeEric Anholt develop this 3D driver.
59490d86808300e5819941a40784e272c290e05eeEric Anholt
69490d86808300e5819941a40784e272c290e05eeEric Anholt Permission is hereby granted, free of charge, to any person obtaining
79490d86808300e5819941a40784e272c290e05eeEric Anholt a copy of this software and associated documentation files (the
89490d86808300e5819941a40784e272c290e05eeEric Anholt "Software"), to deal in the Software without restriction, including
99490d86808300e5819941a40784e272c290e05eeEric Anholt without limitation the rights to use, copy, modify, merge, publish,
109490d86808300e5819941a40784e272c290e05eeEric Anholt distribute, sublicense, and/or sell copies of the Software, and to
119490d86808300e5819941a40784e272c290e05eeEric Anholt permit persons to whom the Software is furnished to do so, subject to
129490d86808300e5819941a40784e272c290e05eeEric Anholt the following conditions:
139490d86808300e5819941a40784e272c290e05eeEric Anholt
149490d86808300e5819941a40784e272c290e05eeEric Anholt The above copyright notice and this permission notice (including the
159490d86808300e5819941a40784e272c290e05eeEric Anholt next paragraph) shall be included in all copies or substantial
169490d86808300e5819941a40784e272c290e05eeEric Anholt portions of the Software.
179490d86808300e5819941a40784e272c290e05eeEric Anholt
189490d86808300e5819941a40784e272c290e05eeEric Anholt THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
199490d86808300e5819941a40784e272c290e05eeEric Anholt EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209490d86808300e5819941a40784e272c290e05eeEric Anholt MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
219490d86808300e5819941a40784e272c290e05eeEric Anholt IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
229490d86808300e5819941a40784e272c290e05eeEric Anholt LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
239490d86808300e5819941a40784e272c290e05eeEric Anholt OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
249490d86808300e5819941a40784e272c290e05eeEric Anholt WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
259490d86808300e5819941a40784e272c290e05eeEric Anholt
269490d86808300e5819941a40784e272c290e05eeEric Anholt **********************************************************************/
279490d86808300e5819941a40784e272c290e05eeEric Anholt /*
289490d86808300e5819941a40784e272c290e05eeEric Anholt  * Authors:
299490d86808300e5819941a40784e272c290e05eeEric Anholt  *   Keith Whitwell <keith@tungstengraphics.com>
309490d86808300e5819941a40784e272c290e05eeEric Anholt  */
319490d86808300e5819941a40784e272c290e05eeEric Anholt
329490d86808300e5819941a40784e272c290e05eeEric Anholt#include "main/mtypes.h"
33ec2b92f98c2e7f161521b447cc1d9a36bce3707cBrian Paul#include "program/prog_parameter.h"
349490d86808300e5819941a40784e272c290e05eeEric Anholt
359490d86808300e5819941a40784e272c290e05eeEric Anholt#include "brw_context.h"
369490d86808300e5819941a40784e272c290e05eeEric Anholt#include "brw_state.h"
379490d86808300e5819941a40784e272c290e05eeEric Anholt
389490d86808300e5819941a40784e272c290e05eeEric Anholt/* Creates a new VS constant buffer reflecting the current VS program's
399490d86808300e5819941a40784e272c290e05eeEric Anholt * constants, if needed by the VS program.
409490d86808300e5819941a40784e272c290e05eeEric Anholt *
419490d86808300e5819941a40784e272c290e05eeEric Anholt * Otherwise, constants go through the CURBEs using the brw_constant_buffer
429490d86808300e5819941a40784e272c290e05eeEric Anholt * state atom.
439490d86808300e5819941a40784e272c290e05eeEric Anholt */
44118a47623a11a374df371d52ed0294224e6a62dcEric Anholtstatic void
45395a8605fa80aeef48f6d9e90d9a7c38c259120dEric Anholtbrw_upload_vs_pull_constants(struct brw_context *brw)
469490d86808300e5819941a40784e272c290e05eeEric Anholt{
47f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
489490d86808300e5819941a40784e272c290e05eeEric Anholt   struct intel_context *intel = &brw->intel;
498654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt   /* BRW_NEW_VERTEX_PROGRAM */
509490d86808300e5819941a40784e272c290e05eeEric Anholt   struct brw_vertex_program *vp =
519490d86808300e5819941a40784e272c290e05eeEric Anholt      (struct brw_vertex_program *) brw->vertex_program;
529490d86808300e5819941a40784e272c290e05eeEric Anholt   const struct gl_program_parameter_list *params = vp->program.Base.Parameters;
531220aba99bc78290bb89ade649719508e3031e4bEric Anholt   int i;
549490d86808300e5819941a40784e272c290e05eeEric Anholt
55118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   if (vp->program.IsNVProgram)
56118a47623a11a374df371d52ed0294224e6a62dcEric Anholt      _mesa_load_tracked_matrices(ctx);
571220aba99bc78290bb89ade649719508e3031e4bEric Anholt
581220aba99bc78290bb89ade649719508e3031e4bEric Anholt   /* Updates the ParamaterValues[i] pointers for all parameters of the
591220aba99bc78290bb89ade649719508e3031e4bEric Anholt    * basic type of PROGRAM_STATE_VAR.
601220aba99bc78290bb89ade649719508e3031e4bEric Anholt    */
611220aba99bc78290bb89ade649719508e3031e4bEric Anholt   _mesa_load_state_parameters(&brw->intel.ctx, vp->program.Base.Parameters);
621220aba99bc78290bb89ade649719508e3031e4bEric Anholt
638654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt   /* CACHE_NEW_VS_PROG */
648654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt   if (!brw->vs.prog_data->nr_pull_params) {
65118a47623a11a374df371d52ed0294224e6a62dcEric Anholt      if (brw->vs.const_bo) {
66118a47623a11a374df371d52ed0294224e6a62dcEric Anholt	 drm_intel_bo_unreference(brw->vs.const_bo);
67118a47623a11a374df371d52ed0294224e6a62dcEric Anholt	 brw->vs.const_bo = NULL;
6807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt	 brw->vs.surf_offset[SURF_INDEX_VERT_CONST_BUFFER] = 0;
69118a47623a11a374df371d52ed0294224e6a62dcEric Anholt	 brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
70118a47623a11a374df371d52ed0294224e6a62dcEric Anholt      }
71118a47623a11a374df371d52ed0294224e6a62dcEric Anholt      return;
72118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   }
73118a47623a11a374df371d52ed0294224e6a62dcEric Anholt
74118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   /* _NEW_PROGRAM_CONSTANTS */
75118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   drm_intel_bo_unreference(brw->vs.const_bo);
76118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   brw->vs.const_bo = drm_intel_bo_alloc(intel->bufmgr, "vp_const_buffer",
778654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt					 brw->vs.prog_data->nr_pull_params * 4,
788654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt					 64);
79118a47623a11a374df371d52ed0294224e6a62dcEric Anholt
80118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   drm_intel_gem_bo_map_gtt(brw->vs.const_bo);
818654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt   for (i = 0; i < brw->vs.prog_data->nr_pull_params; i++) {
828654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt      memcpy(brw->vs.const_bo->virtual + i * 4,
838654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt	     brw->vs.prog_data->pull_param[i],
848654931d117f178fa575d3fdc5bbff9f8bf9e816Eric Anholt	     4);
851220aba99bc78290bb89ade649719508e3031e4bEric Anholt   }
86bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt
87bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt   if (0) {
88bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt      for (i = 0; i < params->NumParameters; i++) {
89bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt	 float *row = (float *)brw->vs.const_bo->virtual + i * 4;
90bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt	 printf("vs const surface %3d: %4.3f %4.3f %4.3f %4.3f\n",
91bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt		i, row[0], row[1], row[2], row[3]);
92bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt      }
93bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt   }
94bf15ad37824652b2df61dac5991aa0d4eaa0c436Eric Anholt
95118a47623a11a374df371d52ed0294224e6a62dcEric Anholt   drm_intel_gem_bo_unmap_gtt(brw->vs.const_bo);
969490d86808300e5819941a40784e272c290e05eeEric Anholt
9791043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke   const int surf = SURF_INDEX_VERT_CONST_BUFFER;
985bffbd7ba2ba2ff21469b2a69a0ed67f0802fec7Eric Anholt   intel->vtbl.create_constant_surface(brw, brw->vs.const_bo, 0,
99c3e3903a9089043b280c461a72dab5158dc25d32Kenneth Graunke				       params->NumParameters,
10007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt				       &brw->vs.surf_offset[surf]);
101c933b33dc8e45c0ae01e901930842e615960677eEric Anholt
10291043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke   brw->state.dirty.brw |= BRW_NEW_VS_CONSTBUF;
1039490d86808300e5819941a40784e272c290e05eeEric Anholt}
1049490d86808300e5819941a40784e272c290e05eeEric Anholt
10591043c21f9b82054060311aabb617dd6e5058602Kenneth Graunkeconst struct brw_tracked_state brw_vs_pull_constants = {
1069490d86808300e5819941a40784e272c290e05eeEric Anholt   .dirty = {
10791043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke      .mesa = (_NEW_PROGRAM_CONSTANTS),
10891043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke      .brw = (BRW_NEW_BATCH | BRW_NEW_VERTEX_PROGRAM),
10991043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke      .cache = CACHE_NEW_VS_PROG,
1109490d86808300e5819941a40784e272c290e05eeEric Anholt   },
11191043c21f9b82054060311aabb617dd6e5058602Kenneth Graunke   .emit = brw_upload_vs_pull_constants,
1129490d86808300e5819941a40784e272c290e05eeEric Anholt};
11307e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
11425d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholtstatic void
11525d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholtbrw_upload_vs_ubo_surfaces(struct brw_context *brw)
11625d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt{
11725d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   struct gl_context *ctx = &brw->intel.ctx;
11825d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   /* _NEW_PROGRAM */
11925d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   struct gl_shader_program *prog = ctx->Shader.CurrentVertexProgram;
12025d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt
12125d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   if (!prog)
12225d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt      return;
12325d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt
12425d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   brw_upload_ubo_surfaces(brw, prog->_LinkedShaders[MESA_SHADER_VERTEX],
12525d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt			   &brw->vs.surf_offset[SURF_INDEX_VS_UBO(0)]);
12625d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt}
12725d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt
12825d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholtconst struct brw_tracked_state brw_vs_ubo_surfaces = {
12925d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   .dirty = {
13025d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt      .mesa = (_NEW_PROGRAM |
13125d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt	       _NEW_BUFFER_OBJECT),
13225d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt      .brw = BRW_NEW_BATCH,
13325d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt      .cache = 0,
13425d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   },
13525d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt   .emit = brw_upload_vs_ubo_surfaces,
13625d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt};
13725d2bf3845e9a6faaef8d808c1255ec57dc71dbaEric Anholt
13807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt/**
13907e00b3040d6da381595c65db5afe597f20d99fcEric Anholt * Constructs the binding table for the WM surface state, which maps unit
14007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt * numbers to surface state objects.
14107e00b3040d6da381595c65db5afe597f20d99fcEric Anholt */
14207e00b3040d6da381595c65db5afe597f20d99fcEric Anholtstatic void
14307e00b3040d6da381595c65db5afe597f20d99fcEric Anholtbrw_vs_upload_binding_table(struct brw_context *brw)
14407e00b3040d6da381595c65db5afe597f20d99fcEric Anholt{
14507e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   uint32_t *bind;
14607e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   int i;
14707e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
14807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   /* CACHE_NEW_VS_PROG: Skip making a binding table if we don't use textures or
14907e00b3040d6da381595c65db5afe597f20d99fcEric Anholt    * pull constants.
15007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt    */
15107e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   if (brw->vs.prog_data->num_surfaces == 0) {
15207e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      if (brw->vs.bind_bo_offset != 0) {
15307e00b3040d6da381595c65db5afe597f20d99fcEric Anholt	 brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
15407e00b3040d6da381595c65db5afe597f20d99fcEric Anholt	 brw->vs.bind_bo_offset = 0;
15507e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      }
15607e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      return;
15707e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   }
15807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
15907e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   /* Might want to calculate nr_surfaces first, to avoid taking up so much
16007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt    * space for the binding table.
16107e00b3040d6da381595c65db5afe597f20d99fcEric Anholt    */
16207e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   bind = brw_state_batch(brw, AUB_TRACE_BINDING_TABLE,
1636ca50f381c9cbc87bbb864d2710f3cfa46a95eadEric Anholt			  sizeof(uint32_t) * BRW_MAX_VS_SURFACES,
16407e00b3040d6da381595c65db5afe597f20d99fcEric Anholt			  32, &brw->vs.bind_bo_offset);
16507e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
16607e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   /* BRW_NEW_SURFACES and BRW_NEW_VS_CONSTBUF */
16707e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   for (i = 0; i < BRW_MAX_VS_SURFACES; i++) {
16807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      bind[i] = brw->vs.surf_offset[i];
16907e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   }
17007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
17107e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   brw->state.dirty.brw |= BRW_NEW_VS_BINDING_TABLE;
17207e00b3040d6da381595c65db5afe597f20d99fcEric Anholt}
17307e00b3040d6da381595c65db5afe597f20d99fcEric Anholt
17407e00b3040d6da381595c65db5afe597f20d99fcEric Anholtconst struct brw_tracked_state brw_vs_binding_table = {
17507e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   .dirty = {
17607e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      .mesa = 0,
17707e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      .brw = (BRW_NEW_BATCH |
17807e00b3040d6da381595c65db5afe597f20d99fcEric Anholt	      BRW_NEW_VS_CONSTBUF |
17907e00b3040d6da381595c65db5afe597f20d99fcEric Anholt	      BRW_NEW_SURFACES),
18007e00b3040d6da381595c65db5afe597f20d99fcEric Anholt      .cache = CACHE_NEW_VS_PROG
18107e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   },
18207e00b3040d6da381595c65db5afe597f20d99fcEric Anholt   .emit = brw_vs_upload_binding_table,
18307e00b3040d6da381595c65db5afe597f20d99fcEric Anholt};
184