21 #include <OpenGL/gl.h>
22 #import <mach-o/dyld.h>
26 #include "resize_box.rgb.c"
28 TypeHandle osxGraphicsStateGuardian::_type_handle;
36 void *osxGraphicsStateGuardian::
37 do_get_extension_func(
const char *name) {
38 std::string fullname =
"_" + std::string(name);
39 NSSymbol symbol =
nullptr;
41 if (NSIsSymbolNameDefined(fullname.c_str())) {
42 symbol = NSLookupAndBindSymbol(fullname.c_str());
45 return symbol ? NSAddressOfSymbol(symbol) : nullptr;
51 osxGraphicsStateGuardian::
54 GLGraphicsStateGuardian(engine, pipe),
55 _share_with(share_with),
59 _shared_buffer = 1011;
66 osxGraphicsStateGuardian::
67 ~osxGraphicsStateGuardian() {
68 if (_aglcontext != (AGLContext)
nullptr) {
69 aglSetCurrentContext(
nullptr);
70 aglDestroyContext(_aglcontext);
71 report_agl_error(
"aglDestroyContext");
72 _aglcontext = (AGLContext)
nullptr;
90 GLGraphicsStateGuardian::reset();
92 if (_aglcontext != (AGLContext)
nullptr) {
94 GLint value = sync_video ? 1 : 0;
95 aglSetInteger(_aglcontext, AGL_SWAP_INTERVAL, &value);
109 if (state ==
nullptr) {
110 state = RenderState::make(TransparencyAttrib::make(TransparencyAttrib::M_alpha),
111 DepthWriteAttrib::make(DepthWriteAttrib::M_off),
112 DepthTestAttrib::make(DepthTestAttrib::M_none));
116 std::string resize_box_string((
const char *)resize_box, resize_box_len);
117 std::istringstream resize_box_strm(resize_box_string);
119 if (resize_box_pnm.
read(resize_box_strm,
"resize_box.rgb")) {
121 tex->set_name(
"resize_box.rgb");
122 tex->load(resize_box_pnm);
123 tex->set_minfilter(SamplerState::FT_linear);
124 tex->set_magfilter(SamplerState::FT_linear);
125 state = state->add_attrib(TextureAttrib::make(tex));
130 _projection_mat_inv = _projection_mat = TransformState::make_identity();
134 set_state_and_transform(state, TransformState::make_identity());
138 PN_stdfloat inner_x = 1.0f - (15.0f * 2.0f / _viewport_width);
139 PN_stdfloat inner_y = (15.0f * 2.0f / _viewport_height) - 1.0f;
145 glColor4f(1.0, 1.0, 1.0, 1.0);
146 glTexCoord2f(0.0, 0.0);
147 glVertex2f(inner_x, -1.0);
149 glTexCoord2f(0.9375, 0.0);
150 glVertex2f(1.0, -1.0);
152 glTexCoord2f(0.9375, 0.9375);
153 glVertex2f(1.0, inner_y);
155 glTexCoord2f(0.0, 0.9375);
156 glVertex2f(inner_x, inner_y);
167 describe_pixel_format(fb_props);
171 OSStatus err = noErr;
173 GDHandle display = GetMainDevice();
176 if (!fb_props.get_indexed_color()) {
177 attrib.push_back(AGL_RGBA);
178 int color_bits = fb_props.get_color_bits();
179 int alpha_bits = fb_props.get_alpha_bits();
180 attrib.push_back(AGL_BUFFER_SIZE);
181 attrib.push_back(color_bits + alpha_bits);
182 attrib.push_back(AGL_PIXEL_SIZE);
183 attrib.push_back(color_bits);
184 attrib.push_back(AGL_RED_SIZE);
185 attrib.push_back(fb_props.get_red_bits());
186 attrib.push_back(AGL_GREEN_SIZE);
187 attrib.push_back(fb_props.get_green_bits());
188 attrib.push_back(AGL_BLUE_SIZE);
189 attrib.push_back(fb_props.get_blue_bits());
190 attrib.push_back(AGL_ALPHA_SIZE);
191 attrib.push_back(alpha_bits);
193 attrib.push_back(AGL_DEPTH_SIZE);
194 attrib.push_back(fb_props.get_depth_bits());
195 attrib.push_back(AGL_STENCIL_SIZE);
196 attrib.push_back(fb_props.get_stencil_bits());
197 if (fb_props.get_multisamples() != 0) {
198 attrib.push_back(AGL_MULTISAMPLE);
199 attrib.push_back(AGL_SAMPLE_BUFFERS_ARB);
201 attrib.push_back(AGL_SAMPLES_ARB);
202 attrib.push_back(fb_props.get_multisamples());
205 if (fb_props.is_stereo()) {
206 attrib.push_back(AGL_STEREO);
209 if (!fb_props.is_single_buffered()) {
210 attrib.push_back(AGL_DOUBLEBUFFER);
213 attrib.push_back(AGL_FULLSCREEN);
216 attrib.push_back(AGL_PBUFFER);
219 if (fb_props.get_force_hardware()) {
220 attrib.push_back(AGL_ACCELERATED);
221 attrib.push_back(AGL_NO_RECOVERY);
226 attrib.push_back(AGL_MAXIMUM_POLICY);
229 attrib.push_back(AGL_NONE);
232 _aglcontext =
nullptr;
233 _aglPixFmt = aglChoosePixelFormat(&display, 1, &attrib[0]);
234 err = report_agl_error(
"aglChoosePixelFormat");
236 if(_share_with ==
nullptr) {
237 _aglcontext = aglCreateContext(_aglPixFmt,
nullptr);
241 err = report_agl_error(
"aglCreateContext");
243 if (_aglcontext ==
nullptr) {
244 osxdisplay_cat.error()
245 <<
"osxGraphicsStateGuardian::build_gl Error Getting GL Context \n" ;
250 aglSetInteger(_aglcontext, AGL_BUFFER_NAME, &_shared_buffer);
251 err = report_agl_error(
"aglSetInteger AGL_BUFFER_NAME");
255 osxdisplay_cat.error()
256 <<
"osxGraphicsStateGuardian::build_gl Error Getting Pixel Format\n" ;
257 osxdisplay_cat.error()
265 describe_pixel_format(fb_props);
268 if (osxdisplay_cat.is_debug()) {
269 osxdisplay_cat.debug()
270 <<
"osxGraphicsStateGuardian::build_gl Returning :" << err <<
"\n";
271 osxdisplay_cat.debug()
283 void osxGraphicsStateGuardian::
288 if (aglDescribePixelFormat(_aglPixFmt, AGL_RGBA, &value)) {
289 fb_props.set_indexed_color(!value);
290 fb_props.set_rgb_color(value);
292 if (aglDescribePixelFormat(_aglPixFmt, AGL_DEPTH_SIZE, &value)) {
293 fb_props.set_depth_bits(value);
296 if (aglDescribePixelFormat(_aglPixFmt, AGL_RED_SIZE, &value)) {
297 fb_props.set_red_bits(value);
300 if (aglDescribePixelFormat(_aglPixFmt, AGL_GREEN_SIZE, &value)) {
301 fb_props.set_green_bits(value);
304 if (aglDescribePixelFormat(_aglPixFmt, AGL_BLUE_SIZE, &value)) {
305 fb_props.set_blue_bits(value);
309 if (aglDescribePixelFormat(_aglPixFmt, AGL_ALPHA_SIZE, &value)) {
310 fb_props.set_alpha_bits(value);
313 if (aglDescribePixelFormat(_aglPixFmt, AGL_STENCIL_SIZE, &value)) {
314 fb_props.set_stencil_bits(value);
318 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_RED_SIZE, &value)) {
321 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_GREEN_SIZE, &value)) {
324 if (aglDescribePixelFormat(_aglPixFmt, AGL_ACCUM_BLUE_SIZE, &value)) {
328 if (aglDescribePixelFormat(_aglPixFmt, AGL_SAMPLES_ARB, &value)) {
329 fb_props.set_multisamples(value);
332 if (aglDescribePixelFormat(_aglPixFmt, AGL_DOUBLEBUFFER, &value)) {
334 fb_props.set_back_buffers(1);
336 fb_props.set_back_buffers(0);
340 if (aglDescribePixelFormat(_aglPixFmt, AGL_STEREO, &value)) {
341 fb_props.set_stereo(value);
346 fb_props.set_force_hardware(
true);
347 fb_props.set_force_software(
true);
350 AGLDevice *gdevs = aglDevicesOfPixelFormat(_aglPixFmt, &ndevs);
351 if (gdevs !=
nullptr) {
352 AGLRendererInfo rinfo = aglQueryRendererInfo(gdevs, ndevs);
353 if (rinfo !=
nullptr) {
354 if (aglDescribeRenderer(rinfo, AGL_ACCELERATED, &value)) {
356 fb_props.set_force_hardware(value);
357 fb_props.set_force_software(!value);
359 if (aglDescribeRenderer(rinfo, AGL_VIDEO_MEMORY, &value)) {
360 osxdisplay_cat.debug()
361 <<
"Reported video memory is " << value <<
"\n";
363 if (aglDescribeRenderer(rinfo, AGL_TEXTURE_MEMORY, &value)) {
364 osxdisplay_cat.debug()
365 <<
"Reported texture memory is " << value <<
"\n";
376 CGDisplayRestoreColorSyncSettings();
377 _cgErr = CGGetDisplayTransferByTable( 0, 256, _gOriginalRedTable, _gOriginalGreenTable, _gOriginalBlueTable, &_sampleCount);
390 CGDisplayRestoreColorSyncSettings();
404 CGGammaValue redTable[ 256 ];
405 CGGammaValue greenTable[ 256 ];
406 CGGammaValue blueTable[ 256 ];
411 for (j = 0; j < 3; j++) {
419 for (i = 0; i < 256; i++) {
420 redTable[i] = _gOriginalRedTable[ i ] * (y[ 0 ] ) / 256;
421 greenTable[ i ] = _gOriginalGreenTable[ i ] * (y[ 1 ] ) / 256;
422 blueTable[ i ] = _gOriginalBlueTable[ i ] * (y[ 2 ] ) / 256;
424 _cgErr = CGSetDisplayTransferByTable( 0, 256, redTable, greenTable, blueTable);