Skip to content

Problem with TextureData.Updates() #489

@neclepsio

Description

@neclepsio

I'm using cimgui-go with a custom backend. Now I'm updating to version 1.4.0, and it seems to work until imgui wants to update the texures. The C code I'm translating from imgui_impl_opengl3.cpp is:

for (ImTextureRect& r : tex->Updates)
{
    const int src_pitch = r.w * tex->BytesPerPixel;
    bd->TempBuffer.resize(r.h * src_pitch);
    char* out_p = bd->TempBuffer.Data;
    for (int y = 0; y < r.h; y++, out_p += src_pitch)
        memcpy(out_p, tex->GetPixelsAt(r.x, r.y + y), src_pitch);
    IM_ASSERT(out_p == bd->TempBuffer.end());
    GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, bd->TempBuffer.Data));
}

My translation is:

for _, r := range tex.Updates().Slice() {
	srcPitch := int(r.W()) * int(tex.BytesPerPixel())

	size := int(r.H()) * srcPitch
	if len(renderer.tempBuffer) < size {
		renderer.tempBuffer = append(renderer.tempBuffer, make([]byte, size-len(renderer.tempBuffer))...)
	}

	offset := 0
	for y := 0; y < int(r.H()); y++ {
		src := unsafe.Slice((*byte)(unsafe.Pointer(tex.PixelsAt(int32(r.X()), int32(r.Y())+int32(y)))), srcPitch)
		copy(renderer.tempBuffer[offset:offset+srcPitch], src)
		offset += srcPitch
	}
	gl.TexSubImage2D(gl.TEXTURE_2D, 0,
		int32(r.X()), int32(r.Y()), int32(r.W()), int32(r.H()),
		gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&renderer.tempBuffer[0]))
}

I get a fault panic on copy, which I think is due to strange rects from tex.Updates().Slice(): I get rectangles much larger than the texture or with width or height equal to 0.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions