From 6506c20f4ebc9eb7951a6342b0600d065805d0fb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 May 2020 16:43:08 +0900 Subject: [PATCH] graphicscommand: Use an image in the uniform variables --- internal/graphicscommand/command.go | 36 ++++++++++++++++++++++------- internal/graphicscommand/image.go | 7 ++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index 4428dda93..99130a493 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -92,7 +92,7 @@ type commandQueue struct { var theCommandQueue = &commandQueue{} // appendVertices appends vertices to the queue. -func (q *commandQueue) appendVertices(vertices []float32, width, height float32) { +func (q *commandQueue) appendVertices(vertices []float32, src *Image) { if len(q.vertices) < q.nvertices+len(vertices) { n := q.nvertices + len(vertices) - len(q.vertices) q.vertices = append(q.vertices, make([]float32, n)...) @@ -102,6 +102,16 @@ func (q *commandQueue) appendVertices(vertices []float32, width, height float32) n := len(vertices) / graphics.VertexFloatNum base := q.nvertices / graphics.VertexFloatNum + + // If src is nil, elements for texels (the index in between 2 and 7) in the vertices are not used. + // Then, giving the size 1 is fine. + width := float32(1) + height := float32(1) + if src != nil { + w, h := src.InternalSize() + width = float32(w) + height = float32(h) + } for i := 0; i < n; i++ { idx := base + i q.srcSizes[idx].width = width @@ -134,17 +144,27 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst, src *Image, vertices []f split = true } - n := len(vertices) / graphics.VertexFloatNum + const ( + maxUint = ^uint(0) + maxInt = int(maxUint >> 1) + ) + if src != nil { - iw, ih := src.InternalSize() - q.appendVertices(vertices, float32(iw), float32(ih)) + q.appendVertices(vertices, src) } else { - // TODO: Use the image's size in the uniform variables. - // When there are multiple images, the smallest ID's image should be adopted. - q.appendVertices(vertices, 1, 1) + var img *Image + id := maxInt + for k, v := range uniforms { + if i, ok := v.(*Image); ok && id > k { + img = i + id = k + continue + } + } + q.appendVertices(vertices, img) } q.appendIndices(indices, uint16(q.nextIndex)) - q.nextIndex += n + q.nextIndex += len(vertices) / graphics.VertexFloatNum q.tmpNumIndices += len(indices) // TODO: If dst is the screen, reorder the command to be the last. diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 68f267aeb..0b3e681cd 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -144,6 +144,13 @@ func (i *Image) InternalSize() (int, int) { // 11: Color Y // // src and shader are exclusive and only either is non-nil. +// +// The elements that index is in between 2 and 7 are used for the source images. +// The source image is 1) src argument if non-nil, or 2) an image value in the uniform variables if it exists. +// If there are multiple images in the uniform variables, the smallest ID's value is adopted. +// +// If the source image is not specified, i.e., src is nil and there is no image in the uniform variables, these are +// not used. func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, clr *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, shader *Shader, uniforms map[int]interface{}) { if src != nil && src.screen { panic("graphicscommand: the screen image cannot be the rendering source")