Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot obtain OpenGL version and compile shaders in Ubuntu/CentOS with OSMesa #167

Open
majiaxin110 opened this issue Mar 1, 2025 · 0 comments

Comments

@majiaxin110
Copy link

majiaxin110 commented Mar 1, 2025

What are you trying to do?

I am trying to use go-gl and osmesa to render and output a simple triangle to a png file. I try to build and run in a Docker container (images are based on Ubuntu/CentOS). The problem is obtaining the version of OpenGL and renderer has failed although there is no error in gl.Init(). Shader compilation also fails with no log. It seems no OpenGL functions are loaded correctly.

In addition, the same code has been built and runs as expected in Alpine docker container, which is so strange.

What did you do?

Here is a minimal example of the code I used:

package main

/*
#cgo linux LDFLAGS: -lOSMesa
#cgo pkg-config: osmesa
#include <GL/osmesa.h>
*/
import "C"
import (
	"fmt"
	"runtime"
	"unsafe"

	"github.com/go-gl/gl/v4.1-core/gl"
)

// keep same with https://github.com/go-gl/osmesa

type (
	Format    int
	PixelType int
	Context   C.OSMesaContext
)


func CreateContext(format Format, sharelist Context) Context {
	return Context(C.OSMesaCreateContext(C.GLenum(format), C.OSMesaContext(sharelist)))
}

func MakeCurrent(ctx Context, buffer unsafe.Pointer, type_ PixelType,
	width int, height int) bool {

	return 0 != C.OSMesaMakeCurrent(C.OSMesaContext(ctx), buffer, C.GLenum(type_),
		C.GLsizei(width), C.GLsizei(height))
}

func main() {
	runtime.LockOSThread()
	mesaCtx := CreateContext(gl.RGBA, nil)
	if mesaCtx == nil {
		panic("failed to create context")  // no panic when running
	}
	buffer := make([]byte, 800*600*4)
	if !MakeCurrent(mesaCtx, unsafe.Pointer(&buffer[0]), gl.UNSIGNED_BYTE, 800, 600) {
		panic("failed to create buffer")   // no panic when running
	}

	if err := gl.Init(); err != nil {
		panic(err)
	}
	version := gl.GoStr(gl.GetString(gl.VERSION))
	fmt.Println("OpenGL version", version)  // ![output empty]!

	renderer := gl.GoStr(gl.GetString(gl.RENDERER))
	fmt.Println("Renderer", renderer) // output empty
        // ... shaders compilation also failed, gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) set zero to logLength
}

Here are the Dockerfile for building and running.

FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y build-essential cmake git wget pkg-config software-properties-common libx11-dev libxext-dev libxrender-dev \
    libxrandr-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libosmesa6-dev python3 python3-pip

RUN wget https://mirrors.aliyun.com/golang/go1.24.0.linux-amd64.tar.gz

RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gz

RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile

WORKDIR /app
FROM centos:7

RUN echo go version

RUN sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo && \
    sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo && \
    sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo && \
    sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-* && \
    sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

RUN yum update -y && yum install mesa-libGL-devel -y && yum install wget -y

RUN wget https://mirrors.aliyun.com/golang/go1.24.0.linux-amd64.tar.gz

RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gz

RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile && source /etc/profile

RUN yum install -y gcc make mesa-libGLU-devel mesa-libOSMesa-devel

ENV PATH=$PATH:/usr/local/go/bin

The only success is Alpine

FROM alpine:3

RUN apk add --no-cache build-base mesa-dev mesa-egl mesa-gles libx11-dev wget

RUN wget https://mirrors.aliyun.com/golang/go1.24.0.linux-amd64.tar.gz

RUN rm -rf /usr/local/go && tar -C /usr/local -xzf go1.24.0.linux-amd64.tar.gz

RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile

What did you expect to happen?

The program runs as in Alpine. Here is the output in Alpine

OpenGL version 4.5 (Compatibility Profile) Mesa 24.2.8
Renderer llvmpipe (LLVM 19.1.4, 128 bits)

What actually happened?

The program outputs empty version and renderer string in CentOS 7.9, Ubuntu 24.04 and 22.04. Shader compilation and arrays drawing will also fail.

OpenGL version 
Renderer

What version of Go and go-gl are you using?

Go 1.24
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71

I am new to OpenGL. Could you please help me with the below problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant