13464ebd5Sriastradh#include "pipe/p_context.h"
2af69d88dSmrg#include "util/u_surface.h"
33464ebd5Sriastradh#include "util/u_inlines.h"
43464ebd5Sriastradh#include "util/u_transfer.h"
53464ebd5Sriastradh#include "util/u_memory.h"
63464ebd5Sriastradh
701e04c3fSmrgvoid u_default_buffer_subdata(struct pipe_context *pipe,
801e04c3fSmrg                              struct pipe_resource *resource,
901e04c3fSmrg                              unsigned usage, unsigned offset,
1001e04c3fSmrg                              unsigned size, const void *data)
113464ebd5Sriastradh{
123464ebd5Sriastradh   struct pipe_transfer *transfer = NULL;
1301e04c3fSmrg   struct pipe_box box;
143464ebd5Sriastradh   uint8_t *map = NULL;
15af69d88dSmrg
167ec681f3Smrg   assert(!(usage & PIPE_MAP_READ));
17af69d88dSmrg
1801e04c3fSmrg   /* the write flag is implicit by the nature of buffer_subdata */
197ec681f3Smrg   usage |= PIPE_MAP_WRITE;
20af69d88dSmrg
217ec681f3Smrg   /* buffer_subdata implicitly discards the rewritten buffer range.
227ec681f3Smrg    * PIPE_MAP_DIRECTLY supresses that.
237ec681f3Smrg    */
247ec681f3Smrg   if (!(usage & PIPE_MAP_DIRECTLY)) {
257ec681f3Smrg      if (offset == 0 && size == resource->width0) {
267ec681f3Smrg         usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
277ec681f3Smrg      } else {
287ec681f3Smrg         usage |= PIPE_MAP_DISCARD_RANGE;
297ec681f3Smrg      }
303464ebd5Sriastradh   }
313464ebd5Sriastradh
3201e04c3fSmrg   u_box_1d(offset, size, &box);
3301e04c3fSmrg
347ec681f3Smrg   map = pipe->buffer_map(pipe, resource, 0, usage, &box, &transfer);
3501e04c3fSmrg   if (!map)
3601e04c3fSmrg      return;
3701e04c3fSmrg
3801e04c3fSmrg   memcpy(map, data, size);
397ec681f3Smrg   pipe_buffer_unmap(pipe, transfer);
4001e04c3fSmrg}
4101e04c3fSmrg
4201e04c3fSmrgvoid u_default_texture_subdata(struct pipe_context *pipe,
4301e04c3fSmrg                               struct pipe_resource *resource,
4401e04c3fSmrg                               unsigned level,
4501e04c3fSmrg                               unsigned usage,
4601e04c3fSmrg                               const struct pipe_box *box,
4701e04c3fSmrg                               const void *data,
4801e04c3fSmrg                               unsigned stride,
4901e04c3fSmrg                               unsigned layer_stride)
5001e04c3fSmrg{
5101e04c3fSmrg   struct pipe_transfer *transfer = NULL;
5201e04c3fSmrg   const uint8_t *src_data = data;
5301e04c3fSmrg   uint8_t *map = NULL;
5401e04c3fSmrg
557ec681f3Smrg   assert(!(usage & PIPE_MAP_READ));
5601e04c3fSmrg
5701e04c3fSmrg   /* the write flag is implicit by the nature of texture_subdata */
587ec681f3Smrg   usage |= PIPE_MAP_WRITE;
5901e04c3fSmrg
6001e04c3fSmrg   /* texture_subdata implicitly discards the rewritten buffer range */
617ec681f3Smrg   usage |= PIPE_MAP_DISCARD_RANGE;
6201e04c3fSmrg
637ec681f3Smrg   map = pipe->texture_map(pipe,
64af69d88dSmrg                            resource,
65af69d88dSmrg                            level,
66af69d88dSmrg                            usage,
67af69d88dSmrg                            box, &transfer);
6801e04c3fSmrg   if (!map)
69af69d88dSmrg      return;
70af69d88dSmrg
7101e04c3fSmrg   util_copy_box(map,
7201e04c3fSmrg                 resource->format,
7301e04c3fSmrg                 transfer->stride, /* bytes */
7401e04c3fSmrg                 transfer->layer_stride, /* bytes */
7501e04c3fSmrg                 0, 0, 0,
7601e04c3fSmrg                 box->width,
7701e04c3fSmrg                 box->height,
7801e04c3fSmrg                 box->depth,
7901e04c3fSmrg                 src_data,
8001e04c3fSmrg                 stride,       /* bytes */
8101e04c3fSmrg                 layer_stride, /* bytes */
8201e04c3fSmrg                 0, 0, 0);
83af69d88dSmrg
847ec681f3Smrg   pipe_texture_unmap(pipe, transfer);
853464ebd5Sriastradh}
863464ebd5Sriastradh
8701e04c3fSmrgvoid u_default_transfer_flush_region(UNUSED struct pipe_context *pipe,
8801e04c3fSmrg                                     UNUSED struct pipe_transfer *transfer,
8901e04c3fSmrg                                     UNUSED const struct pipe_box *box)
903464ebd5Sriastradh{
913464ebd5Sriastradh   /* This is a no-op implementation, nothing to do.
923464ebd5Sriastradh    */
933464ebd5Sriastradh}
94