summaryrefslogtreecommitdiff
path: root/cogl/cogl-pipeline-cache.c
blob: eb2fe4597c2c982176c2cae359e08391dcae83bb (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Cogl
 *
 * An object oriented GL/GLES Abstraction/Utility Layer
 *
 * Copyright (C) 2011 Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *
 * Authors:
 *   Neil Roberts <neil@linux.intel.com>
 *   Robert Bragg <robert@linux.intel.com>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "cogl-context-private.h"
#include "cogl-pipeline-private.h"
#include "cogl-pipeline-cache.h"

struct _CoglPipelineCache
{
  GHashTable *fragment_hash;
  GHashTable *vertex_hash;
  GHashTable *combined_hash;
};

static unsigned int
pipeline_fragment_hash (const void *data)
{
  unsigned int fragment_state;
  unsigned int layer_fragment_state;

  _COGL_GET_CONTEXT (ctx, 0);

  fragment_state =
    _cogl_pipeline_get_state_for_fragment_codegen (ctx);
  layer_fragment_state =
    _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx);

  return _cogl_pipeline_hash ((CoglPipeline *)data,
                              fragment_state, layer_fragment_state,
                              0);
}

static CoglBool
pipeline_fragment_equal (const void *a, const void *b)
{
  unsigned int fragment_state;
  unsigned int layer_fragment_state;

  _COGL_GET_CONTEXT (ctx, 0);

  fragment_state =
    _cogl_pipeline_get_state_for_fragment_codegen (ctx);
  layer_fragment_state =
    _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx);

  return _cogl_pipeline_equal ((CoglPipeline *)a, (CoglPipeline *)b,
                               fragment_state, layer_fragment_state,
                               0);
}

static unsigned int
pipeline_vertex_hash (const void *data)
{
  unsigned long vertex_state =
    COGL_PIPELINE_STATE_AFFECTS_VERTEX_CODEGEN;
  unsigned long layer_vertex_state =
    COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN;

  return _cogl_pipeline_hash ((CoglPipeline *)data,
                              vertex_state, layer_vertex_state,
                              0);
}

static CoglBool
pipeline_vertex_equal (const void *a, const void *b)
{
  unsigned long vertex_state =
    COGL_PIPELINE_STATE_AFFECTS_VERTEX_CODEGEN;
  unsigned long layer_vertex_state =
    COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN;

  return _cogl_pipeline_equal ((CoglPipeline *)a, (CoglPipeline *)b,
                               vertex_state, layer_vertex_state,
                               0);
}

static unsigned int
pipeline_combined_hash (const void *data)
{
  unsigned int combined_state;
  unsigned int layer_combined_state;

  _COGL_GET_CONTEXT (ctx, 0);

  combined_state =
    _cogl_pipeline_get_state_for_fragment_codegen (ctx) |
    COGL_PIPELINE_STATE_AFFECTS_VERTEX_CODEGEN;
  layer_combined_state =
    _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx) |
    COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN;

  return _cogl_pipeline_hash ((CoglPipeline *)data,
                              combined_state, layer_combined_state,
                              0);
}

static CoglBool
pipeline_combined_equal (const void *a, const void *b)
{
  unsigned int combined_state;
  unsigned int layer_combined_state;

  _COGL_GET_CONTEXT (ctx, 0);

  combined_state =
    _cogl_pipeline_get_state_for_fragment_codegen (ctx) |
    COGL_PIPELINE_STATE_AFFECTS_VERTEX_CODEGEN;
  layer_combined_state =
    _cogl_pipeline_get_layer_state_for_fragment_codegen (ctx) |
    COGL_PIPELINE_LAYER_STATE_AFFECTS_VERTEX_CODEGEN;

  return _cogl_pipeline_equal ((CoglPipeline *)a, (CoglPipeline *)b,
                               combined_state, layer_combined_state,
                               0);
}

CoglPipelineCache *
cogl_pipeline_cache_new (void)
{
  CoglPipelineCache *cache = g_new (CoglPipelineCache, 1);

  cache->fragment_hash = g_hash_table_new_full (pipeline_fragment_hash,
                                                pipeline_fragment_equal,
                                                cogl_object_unref,
                                                cogl_object_unref);
  cache->vertex_hash = g_hash_table_new_full (pipeline_vertex_hash,
                                              pipeline_vertex_equal,
                                              cogl_object_unref,
                                              cogl_object_unref);
  cache->combined_hash = g_hash_table_new_full (pipeline_combined_hash,
                                                pipeline_combined_equal,
                                                cogl_object_unref,
                                                cogl_object_unref);

  return cache;
}

void
cogl_pipeline_cache_free (CoglPipelineCache *cache)
{
  g_hash_table_destroy (cache->fragment_hash);
  g_hash_table_destroy (cache->vertex_hash);
  g_hash_table_destroy (cache->combined_hash);
  g_free (cache);
}

CoglPipeline *
_cogl_pipeline_cache_get_fragment_template (CoglPipelineCache *cache,
                                            CoglPipeline *key_pipeline)
{
  CoglPipeline *template =
    g_hash_table_lookup (cache->fragment_hash, key_pipeline);

  if (template == NULL)
    {
      /* XXX: I wish there was a way to insert into a GHashTable with
       * a pre-calculated hash value since there is a cost to
       * calculating the hash of a CoglPipeline and in this case we
       * know we have already called _cogl_pipeline_hash during the
       * lookup so we could pass the value through to here to avoid
       * hashing it again.
       */

      /* XXX: Any keys referenced by the hash table need to remain
       * valid all the while that there are corresponding values,
       * so for now we simply make a copy of the current authority
       * pipeline.
       *
       * FIXME: A problem with this is that our key into the cache may
       * hold references to some arbitrary user textures which will
       * now be kept alive indefinitly which is a shame. A better
       * solution will be to derive a special "key pipeline" from the
       * authority which derives from the base Cogl pipeline (to avoid
       * affecting the lifetime of any other pipelines) and only takes
       * a copy of the state that relates to the fragment shader and
       * references small dummy textures instead of potentially large
       * user textures. */
      template = cogl_pipeline_copy (key_pipeline);

      g_hash_table_insert (cache->fragment_hash,
                           template,
                           cogl_object_ref (template));

      if (G_UNLIKELY (g_hash_table_size (cache->fragment_hash) > 50))
        {
          static CoglBool seen = FALSE;
          if (!seen)
            g_warning ("Over 50 separate fragment shaders have been "
                       "generated which is very unusual, so something "
                       "is probably wrong!\n");
          seen = TRUE;
        }
    }

  return template;
}

CoglPipeline *
_cogl_pipeline_cache_get_vertex_template (CoglPipelineCache *cache,
                                          CoglPipeline *key_pipeline)
{
  CoglPipeline *template =
    g_hash_table_lookup (cache->vertex_hash, key_pipeline);

  if (template == NULL)
    {
      template = cogl_pipeline_copy (key_pipeline);

      g_hash_table_insert (cache->vertex_hash,
                           template,
                           cogl_object_ref (template));

      if (G_UNLIKELY (g_hash_table_size (cache->vertex_hash) > 50))
        {
          static CoglBool seen = FALSE;
          if (!seen)
            g_warning ("Over 50 separate vertex shaders have been "
                       "generated which is very unusual, so something "
                       "is probably wrong!\n");
          seen = TRUE;
        }
    }

  return template;
}

CoglPipeline *
_cogl_pipeline_cache_get_combined_template (CoglPipelineCache *cache,
                                            CoglPipeline *key_pipeline)
{
  CoglPipeline *template =
    g_hash_table_lookup (cache->combined_hash, key_pipeline);

  if (template == NULL)
    {
      template = cogl_pipeline_copy (key_pipeline);

      g_hash_table_insert (cache->combined_hash,
                           template,
                           cogl_object_ref (template));

      if (G_UNLIKELY (g_hash_table_size (cache->combined_hash) > 50))
        {
          static CoglBool seen = FALSE;
          if (!seen)
            g_warning ("Over 50 separate programs have been "
                       "generated which is very unusual, so something "
                       "is probably wrong!\n");
          seen = TRUE;
        }
    }

  return template;
}