summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_wm_constant_buffer.c
blob: 7d2533b1048bdc29d06355b9c5fab04b8c264de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* XXX: Constant buffers disabled
 */


/**
 * Create the constant buffer surface.  Vertex/fragment shader constants will be
 * read from this buffer with Data Port Read instructions/messages.
 */
struct brw_winsys_buffer *
brw_create_constant_surface( struct brw_context *brw,
                             struct brw_surface_key *key )
{
   const GLint w = key->width - 1;
   struct brw_winsys_buffer *bo;

   memset(&surf, 0, sizeof(surf));

   surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
   surf.ss0.surface_type = BRW_SURFACE_BUFFER;
   surf.ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;

   assert(key->bo);
   surf.ss1.base_addr = key->bo->offset; /* reloc */

   surf.ss2.width = w & 0x7f;            /* bits 6:0 of size or width */
   surf.ss2.height = (w >> 7) & 0x1fff;  /* bits 19:7 of size or width */
   surf.ss3.depth = (w >> 20) & 0x7f;    /* bits 26:20 of size or width */
   surf.ss3.pitch = (key->pitch * key->cpp) - 1; /* ignored?? */
   brw_set_surface_tiling(&surf, key->tiling); /* tiling now allowed */
 
   bo = brw_upload_cache(&brw->surface_cache, BRW_SS_SURFACE,
			 key, sizeof(*key),
			 &key->bo, key->bo ? 1 : 0,
			 &surf, sizeof(surf),
			 NULL, NULL);

   if (key->bo) {
      /* Emit relocation to surface contents */
      brw->sws->bo_emit_reloc(bo,
			      I915_GEM_DOMAIN_SAMPLER, 0,
			      0,
			      offsetof(struct brw_surface_state, ss1),
			      key->bo);
   }

   return bo;
}



/**
 * Update the surface state for a WM constant buffer.
 * The constant buffer will be (re)allocated here if needed.
 */
static void
brw_update_wm_constant_surface( struct brw_context *brw,
                                GLuint surf)
{
   struct brw_surface_key key;
   struct brw_fragment_shader *fp = brw->curr.fragment_shader;
   struct pipe_buffer *cbuf = brw->curr.fragment_constants;
   int pitch = cbuf->size / (4 * sizeof(float));

   /* If we're in this state update atom, we need to update WM constants, so
    * free the old buffer and create a new one for the new contents.
    */
   brw->sws->bo_unreference(fp->const_buffer);
   fp->const_buffer = brw_wm_update_constant_buffer(brw);

   /* If there's no constant buffer, then no surface BO is needed to point at
    * it.
    */
   if (cbuf == NULL) {
      drm_intel_bo_unreference(brw->wm.surf_bo[surf]);
      brw->wm.surf_bo[surf] = NULL;
      return;
   }

   memset(&key, 0, sizeof(key));

   key.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
   key.ss0.surface_type = BRW_SURFACE_BUFFER;
   key.ss0.surface_format = BRW_SURFACEFORMAT_R32G32B32A32_FLOAT;

   key.bo = brw_buffer(cbuf)->bo;

   key.ss2.width = (pitch-1) & 0x7f;            /* bits 6:0 of size or width */
   key.ss2.height = ((pitch-1) >> 7) & 0x1fff;  /* bits 19:7 of size or width */
   key.ss3.depth = ((pitch-1) >> 20) & 0x7f;    /* bits 26:20 of size or width */
   key.ss3.pitch = (pitch * 4 * sizeof(float)) - 1; /* ignored?? */
   brw_set_surface_tiling(&surf, key->tiling); /* tiling now allowed */


   /*
   printf("%s:\n", __FUNCTION__);
   printf("  width %d  height %d  depth %d  cpp %d  pitch %d\n",
          key.width, key.height, key.depth, key.cpp, key.pitch);
   */

   brw->sws->bo_unreference(brw->wm.surf_bo[surf]);
   brw->wm.surf_bo[surf] = brw_search_cache(&brw->surface_cache,
                                            BRW_SS_SURFACE,
                                            &key, sizeof(key),
                                            &key.bo, 1,
                                            NULL);
   if (brw->wm.surf_bo[surf] == NULL) {
      brw->wm.surf_bo[surf] = brw_create_constant_surface(brw, &key);
   }
   brw->state.dirty.brw |= BRW_NEW_WM_SURFACES;
}

/**
 * Updates surface / buffer for fragment shader constant buffer, if
 * one is required.
 *
 * This consumes the state updates for the constant buffer, and produces
 * BRW_NEW_WM_SURFACES to get picked up by brw_prepare_wm_surfaces for
 * inclusion in the binding table.
 */
static void prepare_wm_constant_surface(struct brw_context *brw )
{
   struct brw_fragment_program *fp =
      (struct brw_fragment_program *) brw->fragment_program;
   GLuint surf = SURF_INDEX_FRAG_CONST_BUFFER;

   drm_intel_bo_unreference(fp->const_buffer);
   fp->const_buffer = brw_wm_update_constant_buffer(brw);

   /* If there's no constant buffer, then no surface BO is needed to point at
    * it.
    */
   if (fp->const_buffer == 0) {
      if (brw->wm.surf_bo[surf] != NULL) {
	 drm_intel_bo_unreference(brw->wm.surf_bo[surf]);
	 brw->wm.surf_bo[surf] = NULL;
	 brw->state.dirty.brw |= BRW_NEW_WM_SURFACES;
      }
      return;
   }

   brw_update_wm_constant_surface(ctx, surf);
}

const struct brw_tracked_state brw_wm_constant_surface = {
   .dirty = {
      .mesa = (_NEW_PROGRAM_CONSTANTS),
      .brw = (BRW_NEW_FRAGMENT_PROGRAM),
      .cache = 0
   },
   .prepare = prepare_wm_constant_surface,
};