Skip to content

Commit

Permalink
WIP: glretrace: use samples value recorded in trace to set MSAA samples
Browse files Browse the repository at this point in the history
Check the calls that configure the framebuffer format and use the
requested samples value unless this value was set from the command
line.

v2: Store the samples value per fbconfig and not globally

v3: * Use 1 as sample number if none were given.
    * Also check glXGetFBConfigAttrib for sample count

Signed-off-by: Gert Wollny <[email protected]>
  • Loading branch information
gerddie authored and jrfonseca committed Apr 20, 2021
1 parent 7110229 commit 81dfede
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 14 deletions.
2 changes: 2 additions & 0 deletions helpers/glfeatures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum Api {
struct Profile {
unsigned major:8;
unsigned minor:8;
unsigned samples:8;
unsigned api:1;
unsigned core:1;
unsigned forwardCompatible:1;
Expand All @@ -64,6 +65,7 @@ struct Profile {
api = _api;
major = _major;
minor = _minor;
samples = 1;
core = _core;
forwardCompatible = _forwardCompatible;
}
Expand Down
3 changes: 3 additions & 0 deletions retrace/glretrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ getCurrentContext(void) {
int
parseAttrib(const trace::Value *attribs, int param, int default_ = 0, int terminator = 0);

void
setSamples(trace::Call& call, int samples);

glfeatures::Profile
parseContextAttribList(const trace::Value *attribs);

Expand Down
16 changes: 14 additions & 2 deletions retrace/glretrace_cgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ using namespace glretrace;

typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
typedef std::map<unsigned long long, Context *> ContextMap;
typedef std::map<unsigned long long, int> FBConfigMap;

// sid -> Drawable* map
static DrawableMap drawable_map;

static DrawableMap pbuffer_map;

static FBConfigMap fbconfig_map;

// ctx -> Context* map
static ContextMap context_map;

Expand Down Expand Up @@ -170,6 +173,8 @@ static void retrace_CGLChoosePixelFormat(trace::Call &call) {

bool singleBuffer = true;
int profile = 0;
int sample_buffers = 0;
int samples = 0;

const trace::Array * attribs = call.arg(0).toArray();
if (attribs) {
Expand Down Expand Up @@ -226,14 +231,18 @@ static void retrace_CGLChoosePixelFormat(trace::Call &call) {
case kCGLPFAStencilSize:
case kCGLPFAAuxBuffers:
case kCGLPFAAccumSize:
case kCGLPFASampleBuffers:
case kCGLPFASamples:
case kCGLPFARendererID:
case kCGLPFADisplayMask:
case kCGLPFAVirtualScreenCount:
++i;
break;

case kCGLPFASampleBuffers:
sample_buffers = attribs->values[i++]->toSInt();
break;
case kCGLPFASamples:
samples = attribs->values[i++]->toSInt();
break;
case kCGLPFAOpenGLProfile:
profile = attribs->values[i++]->toSInt();
break;
Expand Down Expand Up @@ -270,6 +279,9 @@ static void retrace_CGLChoosePixelFormat(trace::Call &call) {
// XXX: Generalize this, don't override command line options.
retrace::doubleBuffer = !singleBuffer;

if (sample_buffers && samples > 0)
pixelFormat->profile.samples = samples;

retrace::addObj(call, pix, pixelFormat);
}

Expand Down
3 changes: 3 additions & 0 deletions retrace/glretrace_egl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ static void retrace_eglChooseConfig(trace::Call &call) {
}
}

if (parseAttrib(attrib_array, EGL_SAMPLE_BUFFERS))
profile.samples = parseAttrib(attrib_array, EGL_SAMPLES);

unsigned num_config = num_config_ptr->values[0]->toUInt();
for (unsigned i = 0; i < num_config; ++i) {
unsigned long long orig_config = config_array->values[i]->toUIntPtr();
Expand Down
52 changes: 47 additions & 5 deletions retrace/glretrace_glx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#define GLX_PBUFFER_HEIGHT 0x8040
#define GLX_PBUFFER_WIDTH 0x8041
#define GLX_SAMPLE_BUFFERS_ARB 100000
#define GLX_SAMPLES_ARB 100001

#endif /* !HAVE_X11 */

Expand All @@ -41,9 +43,11 @@ using namespace glretrace;

typedef std::map<unsigned long, glws::Drawable *> DrawableMap;
typedef std::map<unsigned long long, Context *> ContextMap;
typedef std::map<unsigned long long, int> FBConfigMap;

static DrawableMap drawable_map;
static ContextMap context_map;

static FBConfigMap fbconfig_map;

static glws::Drawable *
getDrawable(unsigned long drawable_id, Context *ctx) {
Expand Down Expand Up @@ -99,6 +103,9 @@ static void retrace_glXCreateContextAttribsARB(trace::Call &call) {
const trace::Value * attrib_list = &call.arg(4);
glfeatures::Profile profile = parseContextAttribList(attrib_list);

auto it = fbconfig_map.find(call.arg(1).toUInt());
if (it != fbconfig_map.end())
profile.samples = it->second;
Context *context = glretrace::createContext(share_context, profile);
context_map[orig_context] = context;
}
Expand Down Expand Up @@ -172,7 +179,14 @@ static void retrace_glXCreateNewContext(trace::Call &call) {

Context *share_context = getContext(call.arg(3).toUIntPtr());

Context *context = glretrace::createContext(share_context);
glfeatures::Profile profile = defaultProfile;

auto it = fbconfig_map.find(call.arg(1).toUInt());
if (it != fbconfig_map.end())
profile.samples = it->second;


Context *context = glretrace::createContext(share_context, profile);
context_map[orig_context] = context;
}

Expand Down Expand Up @@ -217,14 +231,42 @@ static void retrace_glXMakeContextCurrent(trace::Call &call) {
glretrace::makeCurrent(call, new_drawable, new_readable, new_context);
}

static void retrace_glXChooseFBConfig(trace::Call &call) {
auto attrib_list = call.arg(2).toArray();
if (!parseAttrib(attrib_list, GLX_SAMPLE_BUFFERS_ARB))
return;

int samples = parseAttrib(attrib_list, GLX_SAMPLES_ARB);

if (samples > 0) {
const auto ids = call.ret->toArray();
for (auto& v : ids->values)
fbconfig_map[v->toUInt()] = samples;
}
}

static void retrace_glXGetFBConfigAttrib(trace::Call &call) {
if (call.arg(2).toUInt() == GLX_SAMPLES_ARB) {
auto retval = call.arg(3).toArray();
if (retval->size() != 1) {
std::cerr << "Warning: got more than one samples value from glXGetFBConfigAttrib\n";
}
if (retval->size() > 0) {
fbconfig_map[call.arg(1).toUInt()] = retval->values[0]->toUInt();
} else {
std::cerr << "Warning: glXGetFBConfigAttrib didn't provide any samples value\n";
}
}
}

const retrace::Entry glretrace::glx_callbacks[] = {
//{"glXBindChannelToWindowSGIX", &retrace_glXBindChannelToWindowSGIX},
//{"glXBindSwapBarrierNV", &retrace_glXBindSwapBarrierNV},
//{"glXBindSwapBarrierSGIX", &retrace_glXBindSwapBarrierSGIX},
{"glXBindTexImageEXT", &retrace::ignore},
//{"glXChannelRectSGIX", &retrace_glXChannelRectSGIX},
//{"glXChannelRectSyncSGIX", &retrace_glXChannelRectSyncSGIX},
{"glXChooseFBConfig", &retrace::ignore},
{"glXChooseFBConfig", &retrace_glXChooseFBConfig},
{"glXChooseFBConfigSGIX", &retrace::ignore},
{"glXChooseVisual", &retrace::ignore},
//{"glXCopyContext", &retrace_glXCopyContext},
Expand Down Expand Up @@ -258,8 +300,8 @@ const retrace::Entry glretrace::glx_callbacks[] = {
{"glXGetCurrentDrawable", &retrace::ignore},
{"glXGetCurrentReadDrawable", &retrace::ignore},
{"glXGetCurrentReadDrawableSGI", &retrace::ignore},
{"glXGetFBConfigAttrib", &retrace::ignore},
{"glXGetFBConfigAttribSGIX", &retrace::ignore},
{"glXGetFBConfigAttrib", &retrace_glXGetFBConfigAttrib},
{"glXGetFBConfigAttribSGIX", &retrace_glXGetFBConfigAttrib},
{"glXGetFBConfigFromVisualSGIX", &retrace::ignore},
{"glXGetFBConfigs", &retrace::ignore},
{"glXGetMscRateOML", &retrace::ignore},
Expand Down
25 changes: 21 additions & 4 deletions retrace/glretrace_wgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ using namespace glretrace;

typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
typedef std::map<unsigned long long, Context *> ContextMap;
typedef std::map<unsigned long long, int> FBConfigMap;

static DrawableMap drawable_map;
static DrawableMap pbuffer_map;
static ContextMap context_map;

static FBConfigMap fbconfig_map;

static glws::Drawable *
getDrawable(unsigned long long hdc) {
Expand Down Expand Up @@ -226,6 +228,8 @@ static void retrace_wglSwapLayerBuffers(trace::Call &call) {
#define WGL_AUX7_ARB 0x208E
#define WGL_AUX8_ARB 0x208F
#define WGL_AUX9_ARB 0x2090
#define WGL_SAMPLE_BUFFERS_ARB 0x2041
#define WGL_SAMPLES_ARB 0x2042

static void retrace_wglCreatePbufferARB(trace::Call &call) {
unsigned long long orig_pbuffer = call.ret->toUIntPtr();
Expand Down Expand Up @@ -309,6 +313,10 @@ static void retrace_wglCreateContextAttribsARB(trace::Call &call) {
const trace::Value * attribList = &call.arg(2);
glfeatures::Profile profile = parseContextAttribList(attribList);

auto it = fbconfig_map.find(call.arg(0).toUInt());
if (it != fbconfig_map.end())
profile.samples = it->second;

Context *context = glretrace::createContext(share_context, profile);
context_map[orig_context] = context;
}
Expand Down Expand Up @@ -452,14 +460,23 @@ static void retrace_wglUseFontOutlinesAW(trace::Call &call)
}
}

static void retrace_wglChoosePixelFormat(trace::Call &call) {
auto attrib_list = call.arg(1).toArray();
if (!parseAttrib(attrib_list, WGL_SAMPLE_BUFFERS_ARB))
return;

int samples = parseAttrib(attrib_list, WGL_SAMPLES_ARB);

if (samples > 0)
fbconfig_map[call.arg(0).toUInt()] = samples;
}

const retrace::Entry glretrace::wgl_callbacks[] = {
{"glAddSwapHintRectWIN", &retrace::ignore},
{"wglBindTexImageARB", &retrace_wglBindTexImageARB},
{"wglChoosePixelFormat", &retrace::ignore},
{"wglChoosePixelFormatARB", &retrace::ignore},
{"wglChoosePixelFormatEXT", &retrace::ignore},
{"wglChoosePixelFormat", &retrace_wglChoosePixelFormat},
{"wglChoosePixelFormatARB", &retrace_wglChoosePixelFormat},
{"wglChoosePixelFormatEXT", &retrace_wglChoosePixelFormat},
{"wglCreateContext", &retrace_wglCreateContext},
{"wglCreateContextAttribsARB", &retrace_wglCreateContextAttribsARB},
{"wglCreateLayerContext", &retrace_wglCreateLayerContext},
Expand Down
22 changes: 19 additions & 3 deletions retrace/glretrace_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ getVisual(glfeatures::Profile profile) {
std::map<glfeatures::Profile, glws::Visual *>::iterator it = visuals.find(profile);
if (it == visuals.end()) {
glws::Visual *visual = NULL;
unsigned samples = retrace::samples;
unsigned requested_samples = retrace::samples > 1 ? retrace::samples :
(profile.samples ? profile.samples : 1);
unsigned samples = requested_samples;
/* The requested number of samples might not be available, try fewer until we succeed */
while (!visual && samples > 0) {
visual = glws::createVisual(retrace::doubleBuffer, samples, profile);
Expand All @@ -65,8 +67,8 @@ getVisual(glfeatures::Profile profile) {
std::cerr << "error: failed to create OpenGL visual\n";
exit(1);
}
if (samples != retrace::samples) {
std::cerr << "warning: Using " << samples << " samples instead of the requested " << retrace::samples << "\n";
if (samples != requested_samples) {
std::cerr << "warning: Using " << samples << " samples instead of the requested " << requested_samples << "\n";
}
visuals[profile] = visual;
return visual;
Expand Down Expand Up @@ -300,6 +302,20 @@ parseAttrib(const trace::Value *attribs, int param, int default_, int terminator
return default_;
}

void
setSamples(trace::Call& call, int samples)
{
if (samples > 0) {
if (retrace::samples == 1)
retrace::samples = samples;
else {
retrace::warning(call) << "Overriding samples value " << samples
<< " found in trace with command line parameter "
<< retrace::samples << "\n";
}
}
}


/**
* Parse GLX/WGL_ARB_create_context attribute list.
Expand Down

0 comments on commit 81dfede

Please sign in to comment.