Subversion Repositories Kolibri OS

Rev

Rev 3280 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright © 2011 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21.  * SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *    Chris Wilson <chris@chris-wilson.co.uk>
  25.  *
  26.  */
  27.  
  28. #include "sna.h"
  29. #include "sna_render.h"
  30. #include "brw/brw.h"
  31.  
  32. int sna_static_stream_init(struct sna_static_stream *stream)
  33. {
  34.         stream->used = 0;
  35.         stream->size = 64*1024;
  36.  
  37.         stream->data = malloc(stream->size);
  38.         return stream->data != NULL;
  39. }
  40.  
  41. static uint32_t sna_static_stream_alloc(struct sna_static_stream *stream,
  42.                                         uint32_t len, uint32_t align)
  43. {
  44.         uint32_t offset = ALIGN(stream->used, align);
  45.         uint32_t size = offset + len;
  46.  
  47.         if (size > stream->size) {
  48.                 do
  49.                         stream->size *= 2;
  50.                 while (stream->size < size);
  51.  
  52.                 stream->data = realloc(stream->data, stream->size);
  53.         }
  54.  
  55.         stream->used = size;
  56.         return offset;
  57. }
  58.  
  59. uint32_t sna_static_stream_add(struct sna_static_stream *stream,
  60.                                const void *data, uint32_t len, uint32_t align)
  61. {
  62.         uint32_t offset = sna_static_stream_alloc(stream, len, align);
  63.         memcpy(stream->data + offset, data, len);
  64.         return offset;
  65. }
  66.  
  67. void *sna_static_stream_map(struct sna_static_stream *stream,
  68.                             uint32_t len, uint32_t align)
  69. {
  70.         uint32_t offset = sna_static_stream_alloc(stream, len, align);
  71.         return memset(stream->data + offset, 0, len);
  72. }
  73.  
  74. uint32_t sna_static_stream_offsetof(struct sna_static_stream *stream, void *ptr)
  75. {
  76.         return (uint8_t *)ptr - stream->data;
  77. }
  78.  
  79. struct kgem_bo *sna_static_stream_fini(struct sna *sna,
  80.                                        struct sna_static_stream *stream)
  81. {
  82.         struct kgem_bo *bo;
  83.  
  84.         DBG(("uploaded %d bytes of static state\n", stream->used));
  85.  
  86.         bo = kgem_create_linear(&sna->kgem, stream->used, 0);
  87.         if (bo && !kgem_bo_write(&sna->kgem, bo, stream->data, stream->used)) {
  88.         kgem_bo_destroy(&sna->kgem, bo);
  89.                 return NULL;
  90.         }
  91.  
  92.         free(stream->data);
  93.  
  94.         return bo;
  95. }
  96.  
  97. unsigned
  98. sna_static_stream_compile_sf(struct sna *sna,
  99.                              struct sna_static_stream *stream,
  100.                              bool (*compile)(struct brw_compile *))
  101. {
  102.         struct brw_compile p;
  103.  
  104.         brw_compile_init(&p, sna->kgem.gen,
  105.                          sna_static_stream_map(stream,
  106.                                                64*sizeof(uint32_t), 64));
  107.  
  108.         if (!compile(&p)) {
  109.                 stream->used -= 64*sizeof(uint32_t);
  110.                 return 0;
  111.         }
  112.  
  113.         assert(p.nr_insn*sizeof(struct brw_instruction) <= 64*sizeof(uint32_t));
  114.  
  115.         stream->used -= 64*sizeof(uint32_t) - p.nr_insn*sizeof(struct brw_instruction);
  116.         return sna_static_stream_offsetof(stream, p.store);
  117. }
  118.  
  119. unsigned
  120. sna_static_stream_compile_wm(struct sna *sna,
  121.                              struct sna_static_stream *stream,
  122.                              bool (*compile)(struct brw_compile *, int),
  123.                              int dispatch_width)
  124. {
  125.         struct brw_compile p;
  126.  
  127.         brw_compile_init(&p, sna->kgem.gen,
  128.                          sna_static_stream_map(stream,
  129.                                                256*sizeof(uint32_t), 64));
  130.  
  131.         if (!compile(&p, dispatch_width)) {
  132.                 stream->used -= 256*sizeof(uint32_t);
  133.                 return 0;
  134.         }
  135.  
  136.         assert(p.nr_insn*sizeof(struct brw_instruction) <= 256*sizeof(uint32_t));
  137.  
  138.         stream->used -= 256*sizeof(uint32_t) - p.nr_insn*sizeof(struct brw_instruction);
  139.         return sna_static_stream_offsetof(stream, p.store);
  140. }
  141.